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 
#include 
#include 
#include 
#include 
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;icin>>h[i];
        }
        if(abs(h[n-1]-h[0])>(n-1)*d)
        {
            printf("impossible\n");
            continue;
        }
        int nx=0;
        for(int i=0;ifor(int j=-n+1;jint t=0;
        for(int i=0;i0][i]=INF;
            if(x[i]==h[0])dp[0][i]=0;
        }
        for(int i=1;i//
        {
            int k=0;
            for(int j=0;jwhile(kwhile(k+11]<=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;iif(x[i]==h[n-1])cout<return 0;
}

你可能感兴趣的:(UVa,动态规划)