House Man
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2889 Accepted Submission(s): 1212
Problem Description
In Fuzhou, there is a crazy super man. He can’t fly, but he could jump from housetop to housetop. Today he plans to use N houses to hone his house hopping skills. He will start at the shortest house and make N-1 jumps, with each jump taking him to a taller house than the one he is jumping from. When finished, he will have been on every house exactly once, traversing them in increasing order of height, and ending up on the tallest house.
The man can travel for at most a certain horizontal distance D in a single jump. To make this as much fun as possible, the crazy man want to maximize the distance between the positions of the shortest house and the tallest house.
The crazy super man have an ability—move houses. So he is going to move the houses subject to the following constraints:
1. All houses are to be moved along a one-dimensional path.
2. Houses must be moved at integer locations along the path, with no two houses at the same location.
3. Houses must be arranged so their moved ordering from left to right is the same as their ordering in the input. They must NOT be sorted by height, or reordered in any way. They must be kept in their stated order.
4. The super man can only jump so far, so every house must be moved close enough to the next taller house. Specifically, they must be no further than D apart on the ground (the difference in their heights doesn't matter).
Given N houses, in a specified order, each with a distinct integer height, help the super man figure out the maximum possible distance they can put between the shortest house and the tallest house, and be able to use the houses for training.
The man can travel for at most a certain horizontal distance D in a single jump. To make this as much fun as possible, the crazy man want to maximize the distance between the positions of the shortest house and the tallest house.
The crazy super man have an ability—move houses. So he is going to move the houses subject to the following constraints:
1. All houses are to be moved along a one-dimensional path.
2. Houses must be moved at integer locations along the path, with no two houses at the same location.
3. Houses must be arranged so their moved ordering from left to right is the same as their ordering in the input. They must NOT be sorted by height, or reordered in any way. They must be kept in their stated order.
4. The super man can only jump so far, so every house must be moved close enough to the next taller house. Specifically, they must be no further than D apart on the ground (the difference in their heights doesn't matter).
Given N houses, in a specified order, each with a distinct integer height, help the super man figure out the maximum possible distance they can put between the shortest house and the tallest house, and be able to use the houses for training.
Input
In the first line there is an integer T, indicates the number of test cases.(T<=500)
Each test case begins with a line containing two integers N (1 ≤ N ≤ 1000) and D (1 ≤ D ≤1000000). The next line contains N integer, giving the heights of the N houses, in the order that they should be moved. Within a test case, all heights will be unique.
Each test case begins with a line containing two integers N (1 ≤ N ≤ 1000) and D (1 ≤ D ≤1000000). The next line contains N integer, giving the heights of the N houses, in the order that they should be moved. Within a test case, all heights will be unique.
Output
For each test case , output “Case %d: “first where d is the case number counted from one, then output a single integer representing the maximum distance between the shortest and tallest house, subject to the constraints above, or -1 if it is impossible to lay out the houses. Do not print any blank lines between answers.
Sample Input
3
4 4
20 30 10 40
5 6
20 34 54 10 15
4 2
10 20 16 13
Sample Output
Case 1: 3
Case 2: 3
Case 3: -1
Author
jyd
Source
题意:给我们n座房子,房子的高度各不相同, 从最低的房子开始, 每次跳到更高的房子, 跳n-1次最能跳到最高的房子了,但是每次跳跃的距离不能超过d
将这些房子在一维的方向上重新摆放(但是保持输入时的相对位置不变) , 使得最矮的房子和最高的房子水平距离最大
差分约束系统建图。
1 //2017-08-29 2 #include <cstdio> 3 #include <cstring> 4 #include <iostream> 5 #include <algorithm> 6 #include <queue> 7 #include <stack> 8 9 using namespace std; 10 11 const int N = 300100; 12 const int M = 2500100; 13 const int INF = 0x3f3f3f3f; 14 15 int head[N], tot; 16 struct Edge{ 17 int to, next, w; 18 }edge[M]; 19 20 void init(){ 21 tot = 0; 22 memset(head, -1, sizeof(head)); 23 } 24 25 void add_edge(int u, int v, int w){ 26 edge[tot].w = w; 27 edge[tot].to = v; 28 edge[tot].next = head[u]; 29 head[u] = tot++; 30 } 31 32 int n, m, c; 33 bool vis[N]; 34 int dis[N], cnt[N]; 35 36 bool spfa(int s, int n){ 37 memset(vis, 0, sizeof(vis)); 38 memset(dis, INF, sizeof(dis)); 39 memset(cnt, 0, sizeof(cnt)); 40 vis[s] = 1; 41 dis[s] = 0; 42 cnt[s] = 1; 43 deque<int> dq; 44 dq.push_back(s); 45 int sum = 0, len = 1; 46 while(!dq.empty()){ 47 // LLL 优化 48 while(dis[dq.front()]*len > sum){ 49 dq.push_back(dq.front()); 50 dq.pop_front(); 51 } 52 int u = dq.front(); 53 sum -= dis[u]; 54 len--; 55 dq.pop_front(); 56 vis[u] = 0; 57 for(int i = head[u]; i != -1; i = edge[i].next){ 58 int v = edge[i].to; 59 if(dis[v] > dis[u] + edge[i].w){ 60 dis[v] = dis[u] + edge[i].w; 61 if(!vis[v]){ 62 vis[v] = 1; 63 // SLF 优化 64 if(!dq.empty() && dis[v] < dis[dq.front()]) 65 dq.push_front(v); 66 else dq.push_back(v); 67 sum += dis[v]; 68 len++; 69 if(++cnt[v] > n)return false; 70 } 71 } 72 } 73 } 74 return true; 75 } 76 77 struct Node{ 78 int id, height; 79 bool operator<(const Node a) const{ 80 return height < a.height; 81 } 82 }house[N]; 83 84 int main() 85 { 86 std::ios::sync_with_stdio(false); 87 //freopen("input.txt", "r", stdin); 88 int T, n, d, kase = 0; 89 cin>>T; 90 while(T--){ 91 init(); 92 cin>>n>>d; 93 for(int i = 0; i < n; i++){ 94 cin>>house[i].height; 95 house[i].id = i; 96 } 97 sort(house, house+n); 98 //令d[i]表示i到最左边点的距离。 99 for(int i = 0; i < n-1; i++){ 100 // d[i+1] - d[i] >= 1 约束1 101 // d[i] - d[i+1] <= -1 102 // i+1 -> i : -1 103 add_edge(i+1, i, -1); 104 // d[v] - d[u] <= d | v > u 约束2 105 if(house[i+1].id < house[i].id) 106 add_edge(house[i+1].id, house[i].id, d); 107 else 108 add_edge(house[i].id, house[i+1].id, d); 109 } 110 int s, t; 111 //有向边都是 标号小的点 ---> 标号大的点(除开边权为-1的边),所以找最短路的时候也要约定从标号小的点到标号大的点 112 if(house[0].id < house[n-1].id){ 113 s = house[0].id; 114 t = house[n-1].id; 115 }else{ 116 s = house[n-1].id; 117 t = house[0].id; 118 } 119 cout<<"Case "<<++kase<<": "; 120 if(spfa(s, n)) 121 cout<<dis[t]<<endl; 122 else cout<<-1<<endl; 123 } 124 125 return 0; 126 }