hdu 3440 House Man(差分约束)
2016-07-22 23:44
411 查看
House Man
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 2471 Accepted Submission(s): 1007
Problem 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
Author
jyd
题意:
有N个在一条直线上的房子, 每个房子有着不同的高度, 一个超人可以将这些房子左右移动 但不能改变房子之间的相对位置. 现在超人要从最矮的房子跳到刚好比他高的房子上面, 且每次跳的房子都要比当前房子要高. 那么最后超人肯定会跳到最高的房子上面, 现在给出超人能够跳的最远距离, 问: 如何摆放 这些房子, 使得超人能够经过所有的房子跳到最高的房子, 又要使最矮的房子和最高的房子 之间的距离最远??
思路:此题其实不难,不过跟一般的差分约束的区别在于一般的差分约束都是排队模型,也就是从一段到另一端的关系
而此题有可能来回跳,中间会出现一个|a[i+1].id-a[i].id|<=D
此时出现了绝对值怎么办呢?
注意差分约束的起点和终点必须按照从小到大的顺序,因为边都是有向边
我们约定有向边都是从编号小的到编号大的,然后我们最后起点和终点也换成从编号小的找编号大的即可
注意INF要大于1000000000
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
using namespace std;
#define N 1050
#define INF 1000000009
struct Edge
{
int v,next,w;
} edge[N*N];
struct Node
{
int id,v;
} p
;
int cnt,head
,n;
int d
,vis
,num
;
void init()
{
cnt=0;
memset(head,-1,sizeof(head));
}
void addedge(int u,int v,int w)
{
edge[cnt].v=v;
edge[cnt].w=w;
edge[cnt].next=head[u];
head[u]=cnt++;
}
int spfa(int s,int t)
{
memset(vis,0,sizeof(vis));
memset(num,0,sizeof(num));
for(int i=1; i<=n; i++)
d[i]=i==s?0:INF;
vis[s]=1;
queue<int>que;
que.push(s);
while(!que.empty())
{
int u=que.front();
que.pop();
vis[u]=0;
num[u]++;
if(num[u]>n) return -1;
for(int i=head[u]; i!=-1; i=edge[i].next)
{
int v=edge[i].v,w=edge[i].w;
if(d[v]>d[u]+w)
{
d[v]=d[u]+w;
if(!vis[v])
{
vis[v]=1;
que.push(v);
}
}
}
}
return d[t];
}
bool cmp(Node a,Node b)
{
return a.v<b.v;
}
int main()
{
int T,q=1;
int s,t,w,dis;
scanf("%d",&T);
while(T--)
{
init();
scanf("%d %d",&n,&dis);
int s,t,maxn=-INF,minn=INF;
for(int i=1; i<=n; i++)
{
scanf("%d",&p[i].v);
p[i].id=i;
if(p[i].v<minn)
{
minn=p[i].v;
s=i;
}
if(p[i].v>maxn)
{
maxn=p[i].v;
t=i;
}
}
for(int i=2; i<=n; i++)
addedge(i,i-1,-1);
sort(p+1,p+1+n,cmp);
for(int i=2; i<=n; i++)
{
if(p[i].id>p[i-1].id)
addedge(p[i-1].id,p[i].id,dis);
else
addedge(p[i].id,p[i-1].id,dis);
}
if(s>t) swap(s,t);
printf("Case %d: ",q++);
printf("%d\n",spfa(s,t));
}
return 0;
}
相关文章推荐
- 合并两个排序的链表 C++
- 【杭电 1789 】Doing Homework again
- tomcat启动startup.bat一闪而过
- 内核打印杂谈
- 20 个 CSS 高级技巧汇总
- hdu 5744(想法题)
- javaee之cookie和session的见解和练习
- 15个你不可不知的cmd命令
- Codeforces 698A: Vacations(贪心)
- 为什么要内存对齐
- Morphia 学习一 注解
- 剑指offer——和为s的连续正数序列
- 7月22日实习日志及总结
- Tomcat单机多实例配置
- OGRE的安装与配置(windows)
- Zabbix监控Mysql数据库性能
- CodeForces 610A Pasha and Stick (拆数字组成长方形)
- 棋盘分割
- 如何快速打开刚刚关闭的网页?
- Java 内存区域和GC机制