Easy Climb - UVa 12170 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 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

题意:给你一些台阶的高度,使得相邻两个台阶之间的高度差不大于d,问至少需要增加或减少的高度和是多少。

思路:首先,对于h1,h2,h3,h2的高度范围为[maxn(h1,h3)-d,min(h1,h3)+d],那么我们可以假定h2只会取3个值(左右端点及它自身不变),那么这样的话,所有台阶高度的取值为h[i]+k*d(-n<k<n),总共有n^2种可能性,然后就是简单的dp转移,其中用优先队列优化,达到n^3的复杂度。

AC代码如下:

#include<cstdio>
#include<cstring>
#include<queue>
#include<map>
#include<cstdlib>
#include<algorithm>
using namespace std;
typedef long long ll;
struct node
{
    ll val;
    int pos;
}qu[50010];
ll dp[2][20010],point[20010],d,INF=1e15;
int T,t,n,h[110],tot,Head,Tail;
node A;
void solve()
{
    int i,j,k=1;
    for(i=2;i<=tot;i++)
       if(point[k]!=point[i])
         point[++k]=point[i];
    tot=k;
}
int find(ll ret)
{
    int l=1,r=tot,mi;
    while(l<r)
    {
        mi=(l+r)/2;
        if(point[mi]<ret)
          l=mi+1;
        else
          r=mi;
    }
    return l;
}
int main()
{
    int i,j,k,a,b;
    ll p,ans;
    scanf("%d",&T);
    for(t=1;t<=T;t++)
    {
        scanf("%d%lld",&n,&d);
        tot=0;
        for(i=1;i<=n;i++)
        {
            scanf("%lld",&h[i]);
            for(k=-n;k<=n;k++)
            {
                p=h[i]+k*d;
                if(p>=0)
                  point[++tot]=p;
            }
        }
        sort(point+1,point+1+tot);
        solve();
        for(i=1;i<=tot;i++)
           dp[0][i]=INF;
        dp[0][find(h[1])]=0;
        for(i=2;i<=n;i++)
        {
            if(i&1)
              a=1,b=0;
            else
              a=0,b=1;
            k=1;
            Head=Tail=20000;
            for(j=1;j<=tot;j++)
            {
                dp[b][j]=INF;
                while(point[k]-point[j]<=d && k<=tot)
                {
                    while(Tail>Head && dp[a][k]<=qu[Tail-1].val)
                         Tail--;
                    qu[Tail].val=dp[a][k];
                    qu[Tail].pos=k;
                    k++;Tail++;
                }
                while(point[qu[Head].pos]-point[j]<-d)
                     Head++;
                dp[b][j]=abs(h[i]-point[j])+qu[Head].val;
            }
        }
        ans=dp[b][find(h[n])];
        if(ans>=INF)
          printf("impossible\n");
        else
          printf("%lld\n",ans);
    }
}



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值