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. 

InputIn 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. 
OutputFor 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

题意:某胡建人,从最矮的楼依此按楼的高度,一个个的跳完所有楼,最后停在最高的楼。问最高的楼(终点)和最矮的楼(起点)最远可以隔多远。需要满足每一次跳跃不超过D。注意,起点在终点左边才成立。所以,必须保证位置关系,不然是错的。

思路:假设起点再终点左边。那么向左边走(负),越近越好(负数的绝对值越小,即越大);向右走(正),越远越好(越大越好)。

即是求最大值,用最短路算法,建图:T <= S + dist。

#include<cmath>
#include<queue>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
const int maxn=;
const int inf=;
struct in { int ht,op; } s[maxn];
bool cmp(in a,in b){ return a.ht<b.ht;}
int Laxt[maxn],Next[maxn],To[maxn],Len[maxn],cnt,n;
int dis[maxn],vis[maxn],inq[maxn];
int S,E;
void update()
{
cnt=;
memset(Laxt,,sizeof(Laxt));
memset(vis,,sizeof(vis));
memset(inq,,sizeof(inq));
}
void add(int u,int v,int d)
{
Next[++cnt]=Laxt[u];
Laxt[u]=cnt;
To[cnt]=v;
Len[cnt]=d;
}
bool spfa()
{
for(int i=;i<=n+;i++) dis[i]=inf;
queue<int>q;
q.push(S); dis[S]=; inq[S]=;
while(!q.empty()){
int u=q.front(); q.pop(); inq[u]=;
for(int i=Laxt[u];i;i=Next[i]){
int v=To[i];
if(dis[v]>dis[u]+Len[i]){
dis[v]=dis[u]+Len[i];
if(!inq[v]){
inq[v]=;
vis[v]++;
q.push(v);
if(vis[v]>n+) return false;
}
}
}
} return true;
}
int main()
{
int T,i,d,Case=;
scanf("%d",&T);
while(T--){
scanf("%d%d",&n,&d);
update();
for(i=;i<=n;i++) scanf("%d",&s[i].ht), s[i].op=i;
sort(s+,s+n+,cmp);
for(i=;i<n;i++){
add(i+,i,-);
if(s[i+].op>s[i].op) add(s[i].op,s[i+].op,d);
else add(s[i+].op,s[i].op,d);
}
S=s[].op; E=s[n].op;
if(S>E) swap(S,E); //才符合最远,左起右终。
printf("Case %d: ",++Case);
if(spfa()) printf("%d\n",dis[E]);
else printf("-1\n");
} return ;
}
//求最长,小于,最短路算法。

最新文章

  1. Keepalived双机热备
  2. (转)Ehcache 整合Spring 使用页面、对象缓存
  3. IE6-9中tbody的innerHTML不能赋值bug
  4. Reducejoin sample
  5. PLSQL_闪回操作2_Fashback Version Query
  6. 又是一道水题 hdu背包
  7. ubuntu vim YouComlpeteMe配置
  8. C++的第一天
  9. Kvm虚拟化的一种打包及部署方案(采用tar包,lvm方式)
  10. python+selenium 输出2种样式的测试报告
  11. Python基础之字符串拼接简单介绍
  12. Class实例在堆中还是方法区中?
  13. 论一个蒟蒻的脑子里可以有多少坑(貌似咕了……目前更新保持在noip阶段)
  14. JAVA基础之复识二
  15. DevExpress07、DataNavigator、 ControlNavigator
  16. 20145318《网络对抗》Web应用
  17. C# WEB.API 多图上传
  18. Servlet 思维导图
  19. 深度学习数据集Deep Learning Datasets
  20. [POI2015]LOG

热门文章

  1. JS里面的call, apply以及bind
  2. 客户端svn出现authorization failed异常
  3. [Guava源代码阅读笔记]-Basic Utilities篇-1
  4. apk 签名
  5. G 全然背包
  6. json解析神器 jsonpath的使用
  7. PS 如何制作眼泪效果
  8. selector在手机上或浏览器显示各种姿势(虚拟下拉菜单)
  9. git 操作远程仓库地址
  10. jsp页面设置绝对路径