House Man
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2256 Accepted Submission(s): 896
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个屋子,超人从最矮的屋子开始,依次跳下比当前屋子高且最接近当前高度的屋子(即按照屋子高度增序来跳),但超人跳跃还有一个水平距离限制D,他每次跳的水平距离<=D。现在给你每个屋子的高度是它们的相对位置,你不能改变屋子的相对位置,但是可以水平移动屋子,使得最矮的屋子和最高的屋子的水平距离最大。如果无论怎样移动,超人都无法跳到最后那个屋子则输出-1
思路:用一个结构体记录房子的高度以及位置,id存房子的编号,high存房子的高度
建边一:相邻的房子至少距离为1(未排序之前)
建边二:将房子按高度递增排序后,选择 最低房子 和 最高房子中位置较小的为起点,另一个为终点。那么对于相邻的房子,我们可以建一条 位置小的指向位置大的边且权值为D;
(位置相邻的房子距离至少差1,相邻高度的两间房子最多差d来建边)
#include <cstdio>
#include <cstring>
#include <iostream>
#include <queue>
#include <algorithm>
#define INF 0x3f3f3f3f
#define maxn 1100
using namespace std;
struct node {
int u, v, w, next;
};
struct node1{
int id;
int high;
};
node edge[maxn * 4];
node1 str[maxn];
int dist[maxn], head[maxn], used[maxn], cnt;
int N, D, st, ed, k;
bool vis[maxn];
int cmp(node1 a, node1 b){
return a.high > b.high;
}
void init (){
cnt = 0;
memset(head, -1, sizeof(head));
}
void add(int u, int v, int w){
edge[cnt] = {u, v, w, head[u]};
head[u] = cnt++;
}
void getmap(){
scanf("%d%d", &N, &D);
for(int i = 1; i <= N; ++i){
scanf("%d", &str[i].high);
str[i].id = i;
}
sort(str + 1, str + 1 + N, cmp);
st = min(str[1].id, str[N].id); //编号小的为起点
ed = max(str[1].id, str[N].id); //编号大的为终点
for(int i = 1; i <= N - 1; ++i){
if(str[i].id > str[i + 1].id)
add(str[i + 1].id, str[i].id, D);//小的指向大的(大的指向小的也可以)
else // 相邻高度的两间房子最多差D
add(str[i].id, str[i + 1].id, D);
add(i + 1, i, -1);//位置相邻的房子距离至少差1
}
}
void SPFA(){
printf("Case %d: ", k++);
for(int i = 1; i <= N; ++i){
dist[i] = INF;
vis[i] = 0;
used[i] = 0;
}
queue<int>q;
vis[st] = 1;
used[st] = 1;
dist[st] = 0;
q.push(st);
while(!q.empty()){
int u = q.front();
q.pop();
vis[u] = 0;
for(int i = head[u]; i != -1; i = edge[i].next){
int v = edge[i].v;
int w = edge[i].w;
if(dist[v] > dist[u] + w){
dist[v] = dist[u] + w;
if(!vis[v]){
vis[v] = 1;
used[v]++;
if(used[v] > N){
printf("-1\n");
return ;
}
q.push(v);
}
}
}
}
printf("%d\n", dist[ed]);
}
int main (){
int T;
scanf("%d", &T);
k = 1;
while(T--){
init();
getmap();
SPFA();
}
return 0;
}