12170 - Easy Climb

https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=847&page=show_problem&problem=3322
这里写图片描述
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 hi high. The successive height differences are therefore hi+1-hi (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 ≤ 109): the number of stacks of stones and the maximum allowed height difference.
One line with n integers hi (0 ≤ hi ≤ 109): 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

The 2008 ACM Northwestern European Programming Contest

我有话说:
这道题我们先看一个简单的例子:n=3的时候,那么能够修改的只有h2。修改之后的h我们设为x,那么h1-d< x2 < h1+d 且h3-d< x2 < h3+d;即max( h1,h3)-d< x2 < min(h1,h3)+d;所以h2修改之后的值只有三种选择。max( h1,h3)-d,x2,min(h1,h3)+d。要么符合范围不变。要么取端点才可以得到最小值。所以我们可以推测每一的h修改后的值为一个集合{x|x=h[i]+k*d,0<= i < n,-n+1< k < n-1};这样就可一不用每次h+1或-1的枚举,时间复杂度太大了。
状态转移方程为dp(i,j)=min{abs(h[i]-x[j])+dp(i-1,k)},x[j]-d < x[k]< x[j]+d. i为修改的第i个h值,修改过后的值为x[j];

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <vector>
#include <cmath>
using namespace std;

typedef long long LL;
const int maxn=100+5;
const int maxx=maxn*maxn*2;
const LL INF=(1LL<<60);
LL h[maxn],x[maxx],dp[2][maxx];

int main()
{
    int T;
    cin>>T;
    while(T--)
    {
        int n;
        LL d;
        cin>>n>>d;
        for(int i=0;i<n;i++)
        {
            cin>>h[i];
        }
        if(abs(h[n-1]-h[0])>(n-1)*d)
        {
            printf("impossible\n");
            continue;
        }
        int nx=0;
        for(int i=0;i<n;i++)
            for(int j=-n+1;j<n;j++)
                x[nx++]=h[i]+d*j;
        sort(x,x+nx);
        nx=unique(x,x+nx)-x;

        int t=0;
        for(int i=0;i<nx;i++)
        {
            dp[0][i]=INF;
            if(x[i]==h[0])dp[0][i]=0;
        }
        for(int i=1;i<n;i++)//
        {
            int k=0;
            for(int j=0;j<nx;j++)
            {
                while(k<nx&&x[k]<x[j]-d)k++;
                while(k+1<nx&&x[k+1]<=x[j]+d&&dp[t][k+1]<=dp[t][k])k++;
                if(dp[t][k]==INF)dp[t^1][j]=INF;
                else dp[t^1][j]=dp[t][k]+abs(x[j]-h[i]);
            }
            t^=1;
        }
        for(int i=0;i<nx;i++)
            if(x[i]==h[n-1])cout<<dp[t][i]<<endl;

    }
    return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值