House Man
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2056 Accepted Submission(s): 811
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.
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.
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
题意:有N个在一条直线上的房子, 每个房子有着不同的高度, 一个超人可以将这些房子左右移动但不能改变房子之间的相对位置.现在超人要从最矮的房子跳到刚好比他高的房子上面, 且每次跳的房子都要比当前房子要高.那么最后超人肯定会跳到最高的房子上面,
现在给出超人能够跳的最远距离, 问: 如何摆放这些房子, 使得超人能够经过所有的房子跳到最高的房子, 又要使最矮的房子和最高的房子之间的距离最远??(摘自讨论区)
题解:这题关键在建图,建图时让横坐标大的点指向横坐标小的点。
队列:546ms
#include <stdio.h> #include <string.h> #include <algorithm> #include <queue> #define inf 0x7fffffff #define maxn 1002 using namespace std; int head[maxn], dist[maxn], id, out[maxn]; struct Node2{ int v, num; } V[maxn]; struct Node{ int to, d, next; } E[maxn * maxn]; bool vis[maxn]; bool cmp(Node2 a, Node2 b){ return a.v < b.v; } void addEdge(int u, int v, int d) { E[id].to = v; E[id].d = d; E[id].next = head[u]; head[u] = id++; } int SPFA(int s, int t, int n) { int i, u, v, tmp; for(i = 0; i <= n; ++i){ dist[i] = inf; vis[i] = 0; out[i] = 0; } u = s; dist[u] = 0; vis[u] = 1; queue<int> Q; Q.push(u); while(!Q.empty()){ u = Q.front(); Q.pop(); vis[u] = 0; if(++out[u] > n) return -1; for(i = head[u]; i != -1; i = E[i].next){ tmp = dist[u] + E[i].d; v = E[i].to; if(tmp < dist[v]){ dist[v] = tmp; if(!vis[v]){ vis[v] = 1; Q.push(v); } } } } return dist[t]; } int main() { int t, n, m, i, cas = 1, u, v; scanf("%d", &t); while(t--){ scanf("%d%d", &n, &m); memset(head, -1, sizeof(head)); for(i = 1, id = 0; i <= n; ++i){ scanf("%d", &V[i].v); V[i].num = i; if(i != n) addEdge(i + 1, i, -1); } sort(V + 1, V + n + 1, cmp); for(i = 1; i < n; ++i){ u = V[i].num; v = V[i+1].num; if(u > v) swap(u, v); addEdge(u, v, m); } printf("Case %d: ", cas++); u = V[1].num; v = V[n].num; if(u > v) swap(u, v); printf("%d\n", SPFA(u, v, n)); } return 0; }
栈:93ms
#include <stdio.h> #include <string.h> #include <algorithm> #define inf 0x7fffffff #define maxn 1002 using namespace std; int head[maxn], dist[maxn]; int sta[maxn], id, out[maxn]; struct Node2{ int v, num; } V[maxn]; struct Node{ int to, d, next; } E[maxn * maxn]; bool vis[maxn]; bool cmp(Node2 a, Node2 b){ return a.v < b.v; } void addEdge(int u, int v, int d) { E[id].to = v; E[id].d = d; E[id].next = head[u]; head[u] = id++; } int SPFA(int s, int t, int n) { int i, u, v, tmp, id2; for(i = 0; i <= n; ++i){ dist[i] = inf; vis[i] = 0; out[i] = 0; } u = s; dist[u] = 0; vis[u] = 1; id2 = 0; sta[id2++] = s; while(id2){ u = sta[--id2]; vis[u] = 0; if(++out[u] > n) return -1; for(i = head[u]; i != -1; i = E[i].next){ tmp = dist[u] + E[i].d; v = E[i].to; if(tmp < dist[v]){ dist[v] = tmp; if(!vis[v]){ vis[v] = 1; sta[id2++] = v; } } } } return dist[t]; } int main() { int t, n, m, i, cas = 1, u, v; scanf("%d", &t); while(t--){ scanf("%d%d", &n, &m); memset(head, -1, sizeof(head)); for(i = 1, id = 0; i <= n; ++i){ scanf("%d", &V[i].v); V[i].num = i; if(i != n) addEdge(i + 1, i, -1); } sort(V + 1, V + n + 1, cmp); for(i = 1; i < n; ++i){ u = V[i].num; v = V[i+1].num; if(u > v) swap(u, v); addEdge(u, v, m); } printf("Case %d: ", cas++); u = V[1].num; v = V[n].num; if(u > v) swap(u, v); printf("%d\n", SPFA(u, v, n)); } return 0; }
HDU3440 House Man 【差分约束系统】