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 ;
}
//求最长,小于,最短路算法。

HDU3440 House Man (差分约束)的更多相关文章

  1. 【HDU3440】House Man (差分约束)

    题目: Description In Fuzhou, there is a crazy super man. He can’t fly, but he could jump from housetop ...

  2. Candies-POJ3159差分约束

    Time Limit: 1500MS Memory Limit: 131072K Description During the kindergarten days, flymouse was the ...

  3. poj3159 差分约束 spfa

    //Accepted 2692 KB 1282 ms //差分约束 -->最短路 //TLE到死,加了输入挂,手写queue #include <cstdio> #include & ...

  4. ZOJ 2770火烧连营——差分约束

    偶尔做了一下差分约束. 题目大意:给出n个军营,每个军营最多有ci个士兵,且[ai,bi]之间至少有ki个士兵,问最少有多少士兵. ---------------------------------- ...

  5. POJ 2983 Is the Information Reliable? 差分约束

    裸差分约束. //#pragma comment(linker, "/STACK:1024000000,1024000000") #include<cstdio> #i ...

  6. 2014 Super Training #6 B Launching the Spacecraft --差分约束

    原题:ZOJ 3668 http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3668 典型差分约束题. 将sum[0] ~ sum ...

  7. POJ 1364 King --差分约束第一题

    题意:求给定的一组不等式是否有解,不等式要么是:SUM(Xi) (a<=i<=b) > k (1) 要么是 SUM(Xi) (a<=i<=b) < k (2) 分析 ...

  8. [USACO2005][POJ3169]Layout(差分约束)

    题目:http://poj.org/problem?id=3169 题意:给你一组不等式了,求满足的最小解 分析: 裸裸的差分约束. 总结一下差分约束: 1.“求最大值”:写成"<=& ...

  9. ShortestPath:Layout(POJ 3169)(差分约束的应用)

                布局 题目大意:有N头牛,编号1-N,按编号排成一排准备吃东西,有些牛的关系比较好,所以希望他们不超过一定的距离,也有一些牛的关系很不好,所以希望彼此之间要满足某个关系,牛可以 ...

  10. 【BZOJ】2330: [SCOI2011]糖果(差分约束+spfa)

    http://www.lydsy.com/JudgeOnline/problem.php?id=2330 差分约束运用了最短路中的三角形不等式,即d[v]<=d[u]+w(u, v),当然,最长 ...

随机推荐

  1. 网页Tab控件

    网页Tab控件 找到:http://www.open-open.com/ajax/2_Tabs.htm 页面,查看了若干Tab控件, 找到了:http://www.open-open.com/ajax ...

  2. 我对Lamport Logical Clock的理解

    建议先看论文原文再来看这篇文章,我不会对论文中的各个点都具体说明.仅仅是写一些我自己的想法,帮助理解. 大家都知道.分布式环境下.确定各个事件发生的顺序非常重要,不然就会发生一些麻烦的问题. 考虑一下 ...

  3. Solaris 下解决上网问题以及远程登录问题

    解决乱码问题 参考文章 http://www.jb51.net/os/Solaris/1656.html solaris 显示乱码的解决方法 现象: 利用命令 : LANG=zh; export LA ...

  4. 【环境配置】Linux的经常使用命令

    系统信息 arch 显示机器的处理器架构uname -m 显示机器的处理器架构uname -r 显示正在使用的内核版本号 dmidecode -q 显示硬件系统部件 - (SMBIOS / DMI)  ...

  5. logstash+es+kibana+redis搭建

    环境信息: CentOS 6.5 redis 3.0.4 logstash elasticsearch kibana 服务端ip:192.168.0.65 客户端ip:192.168.0.66 关系结 ...

  6. [未完结]数字微分分析法的直线绘制(DDA)

    注意! 本文被第1次更新,可能存在后续更新 直线画法 直线的斜截式方程 在二维空间下,一条直线的方程可以被描述为若干种形式,其中比较常见的一种是斜截式方程: \[y=kx+b\] 其中\(k\)称为直 ...

  7. RDLC报表 报表数据 栏 快捷键

    血淋淋的教训啊,这段时间用RDLC报表,创建报表后会在右边加载[服务器资源管理器][工具栏][报表数据]一些栏目 如图 [服务器资源管理器][工具栏]还好[报表数据]栏在开发的时候不小心点了X给关掉了 ...

  8. Java中的枚举类为何不能有public构造器

    声明:本博客为原创博客.未经同意.不得转载!原文链接为http://blog.csdn.net/bettarwang/article/details/27262809. 从Java 5開始有了枚举类, ...

  9. (转) 实现wince datagrid 上下滑屏数据浏览

    开发 基于wince 手持设备数据库应用时 由于是触摸屏 当datagrid 数据过多 往往用户烦于去控制又窄又细的上下滚动条 尤其是高分辨率的屏上 (如魅族M8系统 720×480) 而且datag ...

  10. ThreadPoolTaskExecutor

    我们在开发过程中经常要用到线程池,线程池应该统一管理起来,而不是随用随建.ThreadPoolTaskExecutor——将线程池交给spring管理 1. ThreadPoolTaskExecuto ...