博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
HDU 3440 House Man
阅读量:5325 次
发布时间:2019-06-14

本文共 3969 字,大约阅读时间需要 13 分钟。

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
超人只能水平移动并且只能从当前最低的调到比较高的。问超人跳的最长可能是多少。
//差分约束系统#include 
using namespace std;#define INF 1044266558struct node{ int to,w,next;}e[5006];map
m;int head[1006],in[1006],vis[1006],dis[1006];int a[1006],ans,t,n,d;void add_edge(int u,int v,int w){ e[ans].to=v; e[ans].w=w; e[ans].next=head[u]; head[u]=ans++;}int bfs(int s){ dis[s]=0; in[s]=1; queue
q; q.push(s); while(!q.empty()){ int v=q.front(); q.pop(); if(vis[v]>n) return 1; in[v]=0; for(int i=head[v];i!=-1;i=e[i].next){ int u=e[i].to; int w=e[i].w; if(dis[u]>dis[v]+w){ dis[u]=dis[v]+w; vis[u]++; if(!in[u]){ in[u]=1; q.push(u); } } } } return 0;}int main(){ scanf("%d",&t); int o=t; while(t--){ scanf("%d%d",&n,&d); memset(dis,62,sizeof(dis)); memset(head,-1,sizeof(head)); memset(in,0,sizeof(in)); memset(vis,0,sizeof(vis)); m.clear(); ans=0; for(int i=0;i
v) swap(u,v); add_edge(u,v,d); } for(int i=1;i<=n-1;i++) add_edge(i+1,i,-1); printf("Case %d: ",o-t); if(bfs(min(m[a[0]],m[a[n-1]]))) printf("-1\n"); else printf("%d\n",dis[max(m[a[n-1]],m[a[0]])]); } return 0;}

 

 
 
 

转载于:https://www.cnblogs.com/shinianhuanniyijuhaojiubujian/p/9890966.html

你可能感兴趣的文章
移动敏捷测试方法和工具
查看>>
Dubbo 支持哪些序列化协议?
查看>>
vue hash模式转为history模式(二级路由)
查看>>
java 路径的不同写法
查看>>
计算机网络笔记2
查看>>
LuoguP2055 [ZJOI2009]假期的宿舍【二分图最大匹配】By cellur925
查看>>
sql中exists和not exists的用法
查看>>
mysql并发更新问题
查看>>
如何设计专业的PPT
查看>>
[BZOJ1031] 字符加密Cipher
查看>>
用CADisplayLink制作一个仿余额宝数字跳动动画
查看>>
React Native设置图片全屏背景显示
查看>>
第02章-装配Bean
查看>>
《PHP扩展学习系列》系列分享专栏
查看>>
后缀运算符与前缀运算符的区别
查看>>
spring boot 系列学习记录
查看>>
Python break 语句
查看>>
邁向IT專家成功之路的三十則鐵律 鐵律二十四:IT人歲月增長之道-智慧
查看>>
解决VS2010警告unsuccessfulbuild”,因为已指定“AlwaysCreate”
查看>>
[ext]form.submit()相关说明
查看>>