HDU3440 House Man【差分约束】

House Man

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 3620    Accepted Submission(s): 1526


 

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

Source

2010 ACM-ICPC Multi-University Training Contest(1)——Host by FZU

问题链接:HDU3440 House Man

问题描述:有N个在一条直线上的房子, 每个房子有着不同的高度, 一个超人可以将这些房子左右移动但不能改变房子之间的相对位置.现在超人要从最矮的房子跳到刚好比他高的房子上面, 且每次跳的房子都要比当前房子要高.那么最后超人肯定会跳到最高的房子上面, 现在给出超人能够跳的最远距离, 问: 如何摆放这些房子, 使得超人能够经过所有的房子跳到最高的房子, 又要使最矮的房子和最高的房子之间的距离最远?
题解思路:这题关键在建图,建图时让横坐标大的点指向横坐标小的点,具体看程序。

AC的C++程序:

#include<iostream>
#include<cstring>
#include<vector>
#include<queue>
#include<algorithm>

using namespace std;

const int N=1010;
const int INF=0x3f3f3f3f;

struct Node{
	int h,id;
	bool operator <(const Node &p)const
	{
		return h<p.h;//按房屋的高度排序 
	}
}a[N];

struct Edge{
	int v,w;
	Edge(int v,int w):v(v),w(w){}
};

vector<Edge>g[N];

int dist[N];
int cnt[N];
bool vis[N];
int n;

bool spfa(int s)
{
	memset(vis,false,sizeof(vis));
	memset(cnt,0,sizeof(cnt));
	fill(dist,dist+n+1,INF);
	queue<int>q;
	q.push(s);
	vis[s]=true;
	cnt[s]=1;
	dist[s]=0;
	while(!q.empty())
	{
		int u=q.front();
		vis[u]=false;
		q.pop();
		for(int i=0;i<g[u].size();i++)
		{
			int v=g[u][i].v;
			int w=g[u][i].w;
			if(dist[v]>dist[u]+w)
			{
				dist[v]=dist[u]+w;
				if(!vis[v])
				{
					q.push(v);
					vis[v]=true;
					if(cnt[v]++>n)//存在负环 
					  return false;
				}
			}
		}
	 }
	 return true; 
}

int main()
{
	int T,d;
	scanf("%d",&T);
	for(int t=1;t<=T;t++)
	{
		scanf("%d%d",&n,&d);
		for(int i=0;i<=n;i++)
		  g[i].clear();
		for(int i=1;i<=n;i++)
		{
			scanf("%d",&a[i].h);
			a[i].id=i;
		}
		sort(a+1,a+n+1);//排序
//从较矮的房顶跳到次矮的房顶,那么两个房顶的距离必须小于等于d
//设dist[i]表示第i的房子的坐标位置,设较矮的房子为i,次矮的房子是j。
//那么有 abs(dist[i]-dist[j])<=d,由于编号越大(即输入越靠后的),坐标位置越大 
//因此可以想办法去掉绝对值 
		for(int i=2;i<=n;i++)
		{
			int x=a[i-1].id;//较矮房子的编号
			int y=a[i].id;//次矮房子的编号
			if(x>y)
			  swap(x,y);
			g[x].push_back(Edge(y,d)); 
		}
//隐含限制条件,后输入的房子的位置坐标越大 
//因此有,dist[i]-dist[i-1]>=1 (>=1是由于房子都在整数位置,因此最少大于1)
		for(int i=2;i<=n;i++)
		{
			//dist[i-1]-dist[i]<=-1 从i到i-1建一条权值为-1的有向边
			g[i].push_back(Edge(i-1,-1));
		}
		int s=a[1].id;//最矮的房子的编号
		int e=a[n].id;//最高的房子的编号
		if(s>e)
		  swap(s,e); 
		bool flag=spfa(s); 
		printf("Case %d: ",t);
		if(flag)
		  printf("%d\n",dist[e]);
		else
		  printf("-1\n");//存在负环,不存在最大值 
	}
	return 0; 
}

 

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值