# HDU3440 House Man

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 3044    Accepted Submission(s): 1275

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

Author
jyd

Source

Recommend
We have carefully selected several similar problems for you:  3439 3433 3442 3438 3437

#include <bits/stdc++.h>
using namespace std;

const int N = 1010, M = 10000;
const int INF = 0x3f3f3f3f;

struct house{
int he, id;
bool operator < (const house& x)const { return he < x.he; }
}h[N];
struct edge{
int v, d, next;
edge(int v, int d, int n):v(v), d(d), next(n){}
edge(){}
}ed[M];
int head[N], d[N], vis[N], cnt[N];
int n, s, e, k;
queue<int> q;
void init() {
k = 0;
memset(head, -1, sizeof(int) * n);
memset(d, INF, sizeof(int) * n);
memset(vis, 0, sizeof(int) * n);
memset(cnt, 0, sizeof(int) * n);
for (int i = 0;  i < n; i++) h[i].id = i;
while (!q.empty()) q.pop();
}
void add(int u, int v, int d) {
ed[k] = edge(v, d, head[u]);
}
int spfa() {
d[s] = 0; cnt[s]++;
q.push(s);
while (!q.empty()) {
int x = q.front(); q.pop();
vis[x] = 0;
for (int i = head[x]; i != -1; i = ed[i].next) {
int t = ed[i].v;
if (d[t] > d[x] + ed[i].d) {
d[t] = d[x] + ed[i].d;
if (!vis[t]) {
vis[t] = 1; q.push(t);
if (++cnt[t] > n) return -1;
}
}
}
}
return d[e];
}
int main() {
int t, ca = 0;
scanf("%d", &t);
while (t--) {
int d;
scanf("%d %d", &n, &d);
init();
for (int i = 0; i < n; i++) scanf("%d", &h[i].he);
sort(h, h+n);
int flag = 1;
for (int i = 0; i < n-1 && flag; i++) {
int u = min(h[i].id, h[i+1].id), v = max(h[i].id, h[i+1].id);
if (v - u > d) flag = 0;
}
s = min(h[0].id, h[n-1].id), e = max(h[0].id, h[n-1].id);
printf("Case %d: %d\n", ++ca, flag ? spfa() : -1);
}
return 0;
}  

posted @ 2018-03-06 17:12 自为风月马前卒 阅读(...) 评论(...) 编辑 收藏

……