【HDU3440】House Man (差分约束)
题目:
|
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 (差分约束)的更多相关文章
- 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),当然,最长 ...
随机推荐
- Oracle11g - dos 命令 sqlplus/nolog 提示 不是内部命令解决办法
继安装Oracle 11g后,解锁SCOtt时发现 dos 命令 sqlplus/nolog 提示 不是内部命令解决办法 通过实际验证现整理有效方法步骤如下: 步骤一:开始>>找到Or ...
- asp.net 使用IHttpModule 做权限检查 登录超时检查(转)
IHttpModule 权限 检查 登录超时检查 这样就不需要每个页面都做一次检查 也不需要继承任何父类. using System;using System.Collections.Generic; ...
- javascript实现继承的6种方式
/*1.原型链继承*/ function SuperType() { this.property = true; } SuperType.prototype.getSuperValue = funct ...
- H TML5 之 (2) 小试牛刀
基本的HTML都认识到了,就开始运用下了,大的程序的开始,都是从一个一个表达式慢慢的堆积起来的 想开始玩HTML5,就开始理解,它所提供的画布函数各有什么作用,API的具体使用,才能完成自己想要完成的 ...
- C++ 常见问题
1:保证编译后方法名不被修改: The: extern "C" { function declarations here in h file } will disable C++ ...
- 浅谈C#关于AOP编程的学习总结
难得在这样一个节日里给写出一篇博客,却没有佳人相约,没办法,这就是一个程(dan)序(shen)猿(gou)的真实生活情景,每天除了coding还是coding.唉..污染各位看官的眼了.好吧,进入正 ...
- WindowsServer2003SP2EnterpriseEdition在Virtual上的安装
下载镜像(迅雷): http://192.168.0.101/WindowsServer2003SP2EnterpriseEdition.iso?fid=fWljwnwNgumTtRIy- *BY*a ...
- Sql解锁 数据库死锁检测
USE [DataBaseName]GO/****** Object: StoredProcedure [dbo].[sp_check_deadlock] Script Date: 07/04 ...
- C# - linq查询现有的DataTable
可以通过linq对现有的DataTable进行查询,并将结果拷贝至新的DataTable中例如: // Query the SalesOrderHeader table for orders plac ...
- oracle编译 失效对象方式
如果procedure 所使用的表结构发生了改变等其它情况,在相应的xxx_objects表的status字段会变为invalid状态,但是如果在调用时procedure会自动编译,grant失效对象 ...