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. win10 安装git

    http://jingyan.baidu.com/article/a3a3f811d4cd308da2eb8ad1.html 双击exe安装包,在弹出的安全提示中点击“允许”.   安装向导的头两步都 ...

  2. jquery的ajax的success和fail用法

    $.ajax({ type:"POST", url: url, contentType: 'application/json;charset=utf-8', data: JSON. ...

  3. DICOM医学图像处理:深入剖析Orthanc的SQLite,了解WADO & RESTful API

    背景: 上一篇博文简单翻译了Orthanc官网给出的CodeProject上“利用Orthanc Plugin SDK开发WADO插件”的博文,其中提到了Orthanc从0.8.0版本之后支持快速查询 ...

  4. 表现层 JSP 页面实现

    一.实验介绍 1.1 实验内容 本节课程主要利用 easyUI 实现系统的前端页面. 1.2 实验知识点 easyUI JavaScript html 1.3 实验环境 JDK1.8 Eclipse ...

  5. 浅析 rand7生成rand10 方法 之 思想篇(一)

    [问题描写叙述] rand7是一个能生成1-7的随机数.要求利用rand7生成1-10的随机数. [算法思想] 1.组合数学方法 第1次 1 2 3 4 5 6 7 之中用rand7取一个数 第2次从 ...

  6. Oracle oledb 打包并集成到程序安装包(VC ADO訪问Oracle)

    近期有一个项目和oracle交互,我用的是ADO的方式进行试问操作. 首先把数据库连接的东东记录一下,老了记性不好啊! 操作例如以下: //连接串这么写的话就能够不用配置 tnsnames.ora配置 ...

  7. man gitworkflows

    gitworkflows(7) Manual Page NAME gitworkflows - An overview of recommended workflows with Git SYNOPS ...

  8. JSTL简单介绍

    1.JSTL简单介绍: JSTL(JSP Standard Tag Library.JSP标准标签库)是一个不断完好的开放源码的JSP标签库.其提供两组标签,一组使用 EL(Expression La ...

  9. live555直播

    http://www.cppblog.com/tx7do/archive/2014/05/31/207155.aspx http://blog.csdn.net/sunkwei/article/det ...

  10. MVC5中使用jQuery Post 二维数组和一维数组到Action

    很久没有写了,最近在做一个MVC项目,这是我做的第一个MVC项目.之前可以说多MVC一点都不了解,今天把昨天遇到的一个问题记录下来.MVC大神就请飘过吧,跟我遇到同样问题的可以进来看看.遇到的第一个问 ...