POJ 3815 The Ninja Way(差分约束)

思路:和HDU3440基本一样....


#include<iostream>
#include<cstdio>
#include<vector>
#include<queue>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn = 1005;
#define inf 1e9
struct Node
{
	int h,id;
}hou[maxn];
bool cmp(Node a,Node b){return a.h<b.h;}
vector<pair<int,int> >e[maxn];
int inq[maxn],d[maxn],cnt[maxn],n,m,s,t;
int spfa()
{
	for(int i = 0;i<=n;i++)
		d[i]=inf;
	memset(inq,0,sizeof(inq));
	memset(cnt,0,sizeof(cnt));
    queue<int>q;
	q.push(s);
	d[s]=0;
	while(!q.empty())
	{
		int u = q.front();
		q.pop();
		inq[u]=0;
		for(int i = 0;i<e[u].size();i++)
		{
            int v = e[u][i].first;
			if(d[v]>d[u]+e[u][i].second)
			{
				d[v]=d[u]+e[u][i].second;
				if(!inq[v])
				{
					inq[v]=1;
					q.push(v);
					if(++cnt[v]>n)
						return -1;
				}
			}
		}
	}
	return d[t];
}
int main()
{
    //int T,cas=1;
	//scanf("%d",&T);
	int d;
	while(scanf("%d%d",&n,&d)!=EOF && n+d)
	{
      //  scanf("%d%d",&n,&d);
		for(int i = 0;i<=n;i++)
			e[i].clear();
		for(int i = 1;i<=n;i++)
		{
			scanf("%d",&hou[i].h);
			hou[i].id = i;
			if(i!=n)
				e[i+1].push_back(make_pair(i,-1));
		}
		sort(hou+1,hou+1+n,cmp);
		for(int i = 1;i<n;i++)
		{
			int u = hou[i].id;
			int v = hou[i+1].id;
			if(u>v)swap(u,v);
			e[u].push_back(make_pair(v,d));
		}
	//	printf("Case %d: ",cas++);
		s = hou[1].id;
		t = hou[n].id;
		if(s>t)swap(s,t);
		printf("%d\n",spfa());
	}
}


Description

As we all know, Ninjas travel by jumping from treetop to treetop. A clan of Ninjas plans to use N trees to hone their tree hopping skills. They will start at the shortest tree and make N-1 jumps, with each jump taking them to a taller tree than the one they?re jumping from. When finished, they will have been on every tree exactly once, traversing them in increasing order of height, and ending up on the tallest tree. 

The ninjas can travel for at most a certain horizontal distance D in a single jump. To make this as much fun as possible, the Ninjas want to maximize the distance between the positions of the shortest tree and the tallest tree. 



The ninjas are going to plant the trees subject to the following constraints. 

All trees are to be planted along a one-dimensional path. 
Trees must be planted at integer locations along the path, with no two trees at the same location. 
Trees must be arranged so their planted 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. 
The Ninjas can only jump so far, so every tree must be planted close enough to the next taller tree. Specifically, they must be no further than D apart on the ground (the difference in their heights doesn?t matter). 

Given N trees, in a specified order, each with a distinct integer height, help the ninjas figure out the maximum possible distance they can put between the shortest tree and the tallest tree, and be able to use the trees for training.

Input

There will be multiple test cases. Each test case begins with a line containing two integers N (1 ≤ N ≤ 1000) and D (1 ≤ D ≤10  6). The next N lines each contain a single integer, giving the heights of the N trees, in the order that they should be planted. Within a test case, all heights will be unique. The last test case is followed by a line with two 0's.

Output

For each test case, output a line with a single integer representing the maximum distance between the shortest and tallest tree, subject to the constraints above, or -1 if it is impossible to lay out the trees. Do not print any blank lines between answers.

Sample Input

4 4
20
30
10
40
5 6
20
34
54
10
15
4 2
10
20
16
13
0 0

Sample Output

3
3
-1


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值