Uva--12170(动规,单调队列优化)

2014-08-30 12:34:46

12170 - Easy Climb

Time limit: 3.000 seconds

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
 
思路:小白书例题。A了一个下午。。。参考白书的讲解。。。。QAQ
首先看看最简子问题:n = 3时,只有h2是可以修改的,而且修改之后同时在[h1-d,h1+d],[h3+d,h3+d]内,综合一下就是[max(h1,h3)-d,min(h1,h3)+d]。如果该区间为空,则无解。否则h2取区间左值或者右值,或者不改变。 再思考,每个数在修改后一定可以写成hp+k * d, 其中1<=p<=n,-n<k<n。 用dp[i][x]表示已经修改了前i个,且第i个修改为x,x有n^2中可能,状态总数n^2。

状态转移方程:dp[i][x] = |h[i] – x| + min{dp[i – 1][y]) | x – d <= y <= x + d}。如果按照x从小到大排序,满足x-d <= y <= x + d的dp[i – 1][y]就是i-1阶段状态值序列的一个滑动窗口,使用单调队列,这样平均下来每个dp[i][x]时间复杂度为O(1),总时间复杂度:O(n^3)
优化前:O(n^4)

 
 1 #include <cstdio>
 2 #include <cstring>
 3 #include <cmath>
 4 #include <iostream>
 5 #include <algorithm>
 6 using namespace std;
 7 typedef long long ll;
 8 const ll INF = (ll)1 << 50;
 9 const int RA = 100;
10 
11 ll Q[30005];
12 ll P[30005];
13 ll h[105];
14 ll Th[30005];
15 ll dp[105][30005];
16 ll cnt;
17 ll Case,n,d;
18 ll head,tail;
19 
20 inline ll Read(){
21     int x = 0; char ch = getchar();
22     while(ch < '0' || ch > '9') ch = getchar();
23     while(ch >= '0' && '9'){x = x*10 + ch - '0'; ch = getchar();}
24     return x;
25 }
26 
27 void Update_queue(ll limit,ll pos,ll val){
28     while(head <= tail && Q[tail] >= val)
29         --tail;
30     Q[++tail] = val;
31     P[tail] = pos;
32     while(head <= tail && Th[P[head]] < limit) ++head;
33 }
34 
35 void Dp(){
36     ll x;
37     for(ll i = 2; i <= n; ++i){
38         head = 1;
39         tail = 0;
40         ll p = 1;
41         for(ll j = 1; j <= cnt; ++j){
42             x = Th[j];
43             while(p <= cnt && Th[p] - d <= x){
44                 Update_queue(x - d,p,dp[i - 1][p]);
45                 ++p;
46             }
47             dp[i][j] = ((x > h[i]) ? (x - h[i]) : (h[i] - x))  + (head <= tail ? Q[head] : INF);
48         }
49     }
50 }
51 
52 void Init(){
53     for(ll i = 1; i <= cnt; ++i)
54         if(Th[i] == h[1])    dp[1][i] = 0;
55         else    dp[1][i] = INF;
56 }
57 
58 int main(){
59     ll v;
60     Case = Read();
61     while(Case--){
62         cnt = 0;
63         n = Read();
64         d = Read();
65         for(ll i = 1; i <= n; ++i){
66             h[i] = Read();
67             for(ll k = -n + 1; k < n; ++k){
68                 v = h[i] + k * d;
69                 if(v < 0) continue;
70                 Th[++cnt] = v;
71             }
72         }
73         sort(Th + 1,Th + cnt + 1);
74         ll tmp = cnt;
75         cnt = 1;
76         for(ll i = 2; i <= tmp; ++i)
77             if(Th[i] != Th[cnt]) Th[++cnt] = Th[i];
78         Init();
79         Dp();
80         ll ans = INF;
81         for(ll i = 1; i <= cnt; ++i)
82             if(Th[i] == h[n]){
83                 ans = dp[n][i];
84                 break;
85             }
86         if(ans >= INF) printf("impossible\n");
87         else printf("%I64d\n",ans);
88     }
89     return 0;
90 }

 

转载于:https://www.cnblogs.com/naturepengchen/articles/3946304.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值