HDU3440 House Man (差分约束)
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 (差分约束)的更多相关文章
- 【HDU3440】House Man (差分约束)
题目: Description In Fuzhou, there is a crazy super man. He can’t fly, but he could jump from housetop ...
- Candies-POJ3159差分约束
Time Limit: 1500MS Memory Limit: 131072K Description During the kindergarten days, flymouse was the ...
- poj3159 差分约束 spfa
//Accepted 2692 KB 1282 ms //差分约束 -->最短路 //TLE到死,加了输入挂,手写queue #include <cstdio> #include & ...
- ZOJ 2770火烧连营——差分约束
偶尔做了一下差分约束. 题目大意:给出n个军营,每个军营最多有ci个士兵,且[ai,bi]之间至少有ki个士兵,问最少有多少士兵. ---------------------------------- ...
- POJ 2983 Is the Information Reliable? 差分约束
裸差分约束. //#pragma comment(linker, "/STACK:1024000000,1024000000") #include<cstdio> #i ...
- 2014 Super Training #6 B Launching the Spacecraft --差分约束
原题:ZOJ 3668 http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3668 典型差分约束题. 将sum[0] ~ sum ...
- POJ 1364 King --差分约束第一题
题意:求给定的一组不等式是否有解,不等式要么是:SUM(Xi) (a<=i<=b) > k (1) 要么是 SUM(Xi) (a<=i<=b) < k (2) 分析 ...
- [USACO2005][POJ3169]Layout(差分约束)
题目:http://poj.org/problem?id=3169 题意:给你一组不等式了,求满足的最小解 分析: 裸裸的差分约束. 总结一下差分约束: 1.“求最大值”:写成"<=& ...
- ShortestPath:Layout(POJ 3169)(差分约束的应用)
布局 题目大意:有N头牛,编号1-N,按编号排成一排准备吃东西,有些牛的关系比较好,所以希望他们不超过一定的距离,也有一些牛的关系很不好,所以希望彼此之间要满足某个关系,牛可以 ...
- 【BZOJ】2330: [SCOI2011]糖果(差分约束+spfa)
http://www.lydsy.com/JudgeOnline/problem.php?id=2330 差分约束运用了最短路中的三角形不等式,即d[v]<=d[u]+w(u, v),当然,最长 ...
随机推荐
- 主流浏览器js 引擎内核市场份额attialx总结vOa9
原文: http://blog.csdn.net/attilax/article/details/40016... 时间: 2014-10-12 atitit.. 主流浏览器 js 引擎 内核 市场份 ...
- Leanote 二进制版详细安装教程 Windows
https://github.com/leanote/leanote/wiki 本教程适合 Windows 用户的二进制版安装. Windows 用户的源码版安装,参见这里. Mac, Linux 用 ...
- 怎样在ubuntu 系统上为 php 加上 redis 扩展
近期一个项目.,想用redis 作为数据库,php是不待redis 扩展,必须安装,怎么安装呢?我在网上找的非常多资料发现都是预编译的.但都没成功.于是就找了第二种方法是不须要编译直接安装就能够了. ...
- 2016 第七届蓝桥杯 c/c++ B组省赛真题及解题报告
2016 第七届蓝桥杯 c/c++ B组省赛真题及解题报告 勘误1:第6题第4个 if最后一个条件粗心写错了,答案应为1580. 条件应为abs(a[3]-a[7])!=1,宝宝心理苦啊.!感谢zzh ...
- PHP compact() 函数
Compact ---- 创建一个包含变量名和它们的值的数组: <?php $firstname = "Bill"; $lastname = "Gates" ...
- NoSQL数据库-MongoDB和Redis
http://blog.csdn.net/tea_wu/article/details/19050277 http://www.uml.org.cn/sjjm/201212205.asp
- Hadoop 服务器配置的副本数量 管不了客户端
副本数由客户端的参数dfs.replication决定(优先级: conf.set > 自定义配置文件 > jar包中的hdfs-default.xml)如果前两个都没有,就用最后一个ja ...
- bootstrap之鼠标操作
TouchLongClick package io.appium.android.bootstrap.handler; import android.os.SystemClock; import co ...
- hdu1028(母函数+DP)
题目信息:求分解整数n的个数q(n);能够母函数或者DP http://acm.hdu.edu.cn/showproblem.php?pid=1028 AC代码: /***************** ...
- React Native学习(二)之View
React Native组件解析(二)之View 0.JSX React的核心机制之一就是虚拟DOM:可以在内存中创建的虚拟DOM元素.React利用虚拟DOM来减少对实际DOM的操作从而提升性能. ...