【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),当然,最长 ...
随机推荐
- android js 互调
public class BoatsActivity extends Activity { Handler mHandler = new Handler();//处理消息的handler @Suppr ...
- HttpModule,HttpHandler,HttpHandlerFactory
HttpModule:Http模块,可以在页面处理前后.应用程序初始化.出错等时候加入自己的事件处理程序. HttpHandler:Http处理程序,处理页面请求 HttpHandlerFactory ...
- Linux ssh/scp连接时避免输入yes(公钥验证)并防止出现POSSIBLE BREAK-IN ATTEM
方法一:连接时加入StrictHostKeyChecking=no ssh -o StrictHostKeyChecking=no root@192.168.1.100 方法二:修改/etc/ssh/ ...
- python 入门1
python的历史 Python是一种解释型.面向对象.动态数据类型的高级程序设计语言. Python由Guido van Rossum于1989年底发明,第一个公开发行版发行于1991年. 像Per ...
- Frequent Pattern 挖掘之一(Aprior算法)(转)
数据挖掘中有一个很重要的应用,就是Frequent Pattern挖掘,翻译成中文就是频繁模式挖掘.这篇博客就想谈谈频繁模式挖掘相关的一些算法. 定义 何谓频繁模式挖掘呢?所谓频繁模式指的是在样本数据 ...
- css标准导航代码
<!-- 例子解析: --> --> <!-- list-style-type:none - 移除列表前小标志.一个导航栏并不需要列表标记 --> <!-- 移除浏 ...
- Spring JdbcTemplate批量操作数据库
个人总结,转载请注明出处:http://www.cnblogs.com/lidabnu/p/5769732.html 还是分两部分:解决什么问题和怎么做. 解决什么问题 提升数据操作性能,因为批量操作 ...
- 百度,人人,新浪,腾讯等分享Js代码
<!-- Baidu Button BEGIN --> <div id="bdshare" class=" ...
- QL查询案例:取得分组 TOP-N
[转]SQL查询案例:取得分组 TOP-N CREATE TABLE TopnTest ( name VARCHAR(10), --姓名 procDate DATETIME, ...
- MYSQL使用指南(下)
在上篇我们讲了登录.增加用户.密码更改等问题.下篇我们来看看MySQL中有关数据库方面的操作.注意:你必须首先登录到MYSQL中,以下操作都是在MYSQL的提示符下进行的,而且每个命令以分号结束. 一 ...