题目:

Description

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

题意:

  有n个屋子,超人从最矮的屋子开始,依次跳下比当前屋子高且最接近当前高度的屋子(即按照屋子高度增序来跳),但超人跳跃还有一个水平距离限制D,他每次跳的水平距离<=D。现在给你每个屋子的高度是它们的相对位置,你不能改变屋子的相对位置,但是可以水平移动屋子,使得最矮的屋子和最高的屋子的水平距离最大。如果无论怎样移动,超人都无法跳到最后那个屋子则输出-1。

分析:

  还是差分约束,而且这题很好推,排个序然后找不等关系就好了。那么主要说几个坑点:

  1、一开始我是用二分的,然而TLE,后来发现固定了某个点的值之后用最短路解差分约束,得到的点的解都是它所有取值的可能性中数值最大的。所以我们可以固定起点或者终点的值(哪个在左边就固定哪个),然后用一次差分约束就能求解了。

  2、当n=1时,要特判答案为零。(很坑啊~~)

  3、为什么每次数组开小了都说我TLE...搞到我一直再找哪里打错了导致死循环。判我RE不就好了嘛TAT~~

代码如下:

 #include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
#include<queue>
#define Maxn 1100
#define Maxm 1000010 struct node
{
int x,y,c,next;
}t[*Maxn];int len; struct hp
{
int x,id;
}a[Maxn]; int first[Maxn],dis[Maxn],cnt[Maxn];
int n;
bool inq[Maxn]; bool cmp(hp x,hp y) {return x.x<y.x;}
int myabs(int x) {return x<?-x:x;} void ins(int x,int y,int c)
{
t[++len].x=x;t[len].y=y;t[len].c=c;
t[len].next=first[x];first[x]=len;
} queue<int > q;
bool spfa(int s)
{
while(!q.empty()) q.pop();
memset(dis,,sizeof(dis));
memset(cnt,,sizeof(cnt));
memset(inq,,sizeof(inq));
q.push(s);inq[s]=;dis[s]=;
while(!q.empty())
{
int x=q.front();q.pop();inq[x]=;
for(int i=first[x];i;i=t[i].next)
{
int y=t[i].y;
if(dis[y]>dis[x]+t[i].c)
{
dis[y]=dis[x]+t[i].c;
if(!inq[y])
{
inq[y]=;
q.push(y);
if(++cnt[y]>n) return ;
}
}
}
}
return ;
} int main()
{
int T,kase=;
scanf("%d",&T);
while(T--)
{
int d;
scanf("%d%d",&n,&d);
for(int i=;i<=n;i++)
{
scanf("%d",&a[i].x);
a[i].id=i;
}
printf("Case %d: ",++kase);
if(n==) {printf("0\n");continue;}
sort(a+,a++n,cmp);
len=;
memset(first,,sizeof(first));
for(int i=;i<n;i++)
{
ins(a[i].id,a[i+].id,d);
ins(a[i+].id,a[i].id,d);
}
for(int i=;i<=n;i++) ins(i,i-,-); if(a[].id<a[n].id)
{
if(spfa(a[].id)) printf("%d\n",dis[a[n].id]);
else printf("-1\n");
}
else
{
if(spfa(a[n].id)) printf("%d\n",dis[a[].id]);
else printf("-1\n");
}
}
return ;
}

[HDU3440]

2016-04-14 13:50:22

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

  1. Candies-POJ3159差分约束

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

  2. poj3159 差分约束 spfa

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

随机推荐

  1. linux shell less 命令---转

    less 工具也是对文件或其它输出进行分页显示的工具,应该说是linux正统查看文件内容的工具,功能极其强大.less 的用法比起 more 更加的有弹性.在 more 的时候,我们并没有办法向前面翻 ...

  2. iOS在xib或storyboard里为控件添加圆角、外框和外框颜色

    如果要在xib和storyboard里为控件添加圆角和外框宽度,只要这样做就可以: layer.borderWidth 设置外框宽度属性 layer.cornerRadius 设置圆角属性 只要为属性 ...

  3. mysql导出部分数据的几种方法(摘录)

    mysql虽然可以使用mysqldump来进行数据的到处,可是在很多场合的需求都不一样,比如我只要导出某个字段呢?只要导出某些我需要的数据呢? 这个时候mysqldump可能就不大好使了 方法一. i ...

  4. Installing the .NET Framework 4.5, 4.5.1

    This article provides links for installing the .NET Framework 4.5 and 4.5.1 on your computer.  If yo ...

  5. git使用备忘

    简单的记录下现在使用git的时候,暂时或者常用到的命令,纯粹自己备忘,没用到过的命令就不写了,日后有用到了在补充. 1.把远程仓库克隆到本地:git clone 远程地址 2.分支操作 新建分支:gi ...

  6. Bootstrap 样式定制-lessc编译源码

    1.github上下载源码:解压:如目录bootstrap 2.新建同级目录custom-bootstrap ,在该目录下新建 如下三个文件:其实就是bootstrap下面的bootstrap.les ...

  7. Promise的用简要使用方式

    Promise用法 在项目中用到异步请求ajax,想到用promise来解决,之前用过但是已经很久了,还是忘了一些,重新熟悉了一下整理一份简要文档. Promise,就是一个对象,用来传递异步操作的消 ...

  8. 学习java随笔第八篇:封装、继承、多态

    java和c#一样都是面向对象的语言. 面向对象的语言有三大特征:封装.继承.多态 封装 封装:隐藏对象的属性和实现细节,仅对外公开接口,控制在程序中属性的读和修改的访问级别. class Perso ...

  9. 浅谈C#关于AOP编程的学习总结

    难得在这样一个节日里给写出一篇博客,却没有佳人相约,没办法,这就是一个程(dan)序(shen)猿(gou)的真实生活情景,每天除了coding还是coding.唉..污染各位看官的眼了.好吧,进入正 ...

  10. Microsoft SQL Server 管理 (常用管理及维护命令)

    --查询当前连接的实例名 select @@servername --察看任何数据库属性 sp_helpdb master --设置单用户模式,同时立即断开所有用户 alter database No ...