Hdu-2770 Easy Climb(DP优化)

Somewhere in the neighborhood we have a very nice mountain that gives a splendid view over the surrounding area. There is one problem though: climbing this mountain is very difficult, because of rather large height differences. To make more people able to climb the mountain and enjoy the view, we would like to make the climb easier. 

To do so, we will model the mountain as follows: the mountain consists of n adjacent stacks of stones, and each of the stacks is h(i) high. The successive height differences are therefore h(i+1)-h(i) (for 1 ≤ i ≤ n-1). We would like all absolute values of these height differences to be smaller than or equal to some number d. 

We can do this by increasing or decreasing the height of some of the stacks. The first stack (the starting point) and the last stack (the ending point) should remain at the same height as they are initially. Since adding and removing stones requires a lot of effort, we would like to minimize the total number of added stones plus the total number of removed stones. What is this minimum number?
Input
On the first line one positive number: the number of testcases, at most 100. After that per testcase: 

* One line with two integers n (2 ≤ n ≤ 100) and d (0 ≤ d ≤ 10^9): the number of stacks of stones and the maximum allowed height difference. 
* One line with n integers h(i) (0 ≤ h(i) ≤ 10^9): the heights of the stacks.
Output
Per testcase: 

* One line with the minimum number of stones that have to be added or removed or ``impossible'' if it is impossible to achieve the goal.
Sample Input
3
10 2
4 5 10 6 6 9 4 7 9 8
3 1
6 4 0
4 2
3 0 6 3
Sample Output
6
impossible

4

分析:直接dp状态有很多,但其实只有n^2个状态是有效的(h[k] + j*d的形式),状态总数为n^3,转移复杂度

n^2,依然不能接受,但是可以发现这个转移是可以用单调队列优化的,总复杂度O(n^3).

#include <bits/stdc++.h>
#define mask 100
#define INF 214748364700ll
using namespace std;
int T,cnt;
long long tot_num[20103],n,d,h[105],f[102][20103],q[20103][2],Begin,End;
int main()
{
	cin.sync_with_stdio(0);
	cin>>T;
	while(T--)
	{
		cnt = 0;
		memset(f[1],0x3f,sizeof(f[1]));
		cin>>n>>d;
		for(int i = 1;i <= n;i++) cin>>h[i];
		for(int i = 1;i <= n;i++)
		 for(int j = -n;j <= n;j++)
		   if(h[i] + j*d <= 1e9 && h[i] + j*d >= 0)
		    tot_num[++cnt] = h[i] + j*d;
		sort(tot_num+1,tot_num+cnt+1);
		cnt = unique(tot_num+1,tot_num+cnt+1) - tot_num - 1;
		for(int i = 1;i <= cnt;i++)
		{
			if(tot_num[i] == h[1]) Begin = i;
			if(tot_num[i] == h[n]) End = i;
		}
		f[1][Begin] = 0;
		for(int i = 2;i <= n;i++)
		{
			int s = 0,t = 0,l = 1;
	    	for(int j = 1;j <= cnt;j++)
	     	{	
	     		while(s != t && abs(q[s][1]-tot_num[j]) > d) s++;
				while(l <= cnt && tot_num[l] <= tot_num[j] + d)
				{
					if(abs(tot_num[l] - tot_num[j]) <= d)
					{
						q[t][0] = f[i-1][l],q[t][1] = tot_num[l];	
						t++;
						while(s <= t-2 && q[t-1][0] < q[t-2][0]) 
						{
							q[t-2][0] = q[t-1][0];
							q[t-2][1] = q[t-1][1];
							t--;
						}
					}
					l++;
				}
				if(s != t) f[i][j] = q[s][0] + abs(tot_num[j]-h[i]); 
				else f[i][j] = INF;
 		 	}
 		}
		if(f[n][End] > INF) cout<<"impossible"<<endl;
		else cout<<f[n][End]<<endl;
	} 
 } 


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值