『HDU 4053』The Last Puzzle

Posted fang-hao

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了『HDU 4053』The Last Puzzle相关的知识,希望对你有一定的参考价值。

传送门

题目描述

There is one last gate between the hero and the dragon. But opening the gate isn‘t an easy task.

There were n buttons list in a straight line in front of the gate and each with an integer on it. Like other puzzles the hero had solved before, if all buttons had been pressed
down in any moment, the gate would open. So, in order to solve the puzzle, the hero must press all the button one by one.

After some trials, the hero found that those buttons he had pressed down would pop up after a while before he could press all the buttons down. He soon realized that
the integer on the button is the time when the button would automatic pop up after pressing it, in units of second. And he measured the distance between every button
and the first button, in units of maximum distance the hero could reach per second. Even with this information, the hero could not figure out in what order he should
press the buttons. So you talent programmers, are assigned to help him solve the puzzle.

To make the puzzle easier, assuming that the hero always took integral seconds to go from one button to another button and he took no time turnning around or pressing
a button down. And the hero could begin from any button.

 

解题思路

区间dp,用dp[ l ][ r ][ 0/1 ] 表示,按完编号为l - r之间的开关后处在l或r的时候最少需要花费多少时间,但是这样我们发现一个问题,我们很难维护开关有没有弹起,-所以我们要换一种思路。

我们将时间看做一条线,那么没在一个时间点按下一盏灯,那么他会覆盖后面的一段时间,所以我们不如倒着做dp,只要dp值加上到达下一个灯的路程小于等于灯亮的时间就可以保证灯能覆盖到了最后的时刻。

用path[ l ][ r ][ 0/1 ] 表示按完l-r之间的灯,处在l还是r的时候是由哪个方向转移过来的。

 

 

代码

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<algorithm>
 4 #include<cstring>
 5 using namespace std;
 6 const int INF=0x3f3f3f3f;
 7 const int maxn=205;
 8 int n;
 9 int t[maxn],a[maxn];
10 int dp[maxn][maxn][2],path[maxn][maxn][2];
11 int main(){
12     while(~scanf("%d",&n)){
13         for(register int i=1;i<=n;i++){
14             scanf("%d",&t[i]);
15         }
16         for(register int i=1;i<=n;i++){
17             scanf("%d",&a[i]);
18         }
19         memset(path,0,sizeof(path));
20         memset(dp,0,sizeof(dp));
21         for(register int len=2;len<=n;len++){
22             for(register int i=1;i+len-1<=n;i++){
23                 int j=i+len-1;
24                 int disl=dp[i+1][j][0]+a[i+1]-a[i];
25                 int disr=dp[i+1][j][1]+a[j]-a[i];
26                 if(disl<=disr){
27                     dp[i][j][0]=disl;
28                     path[i][j][0]=0;
29                 }
30                 else {
31                     dp[i][j][0]=disr;
32                     path[i][j][0]=1;
33                 }
34                 if(t[i]<=dp[i][j][0]){
35                     dp[i][j][0]=INF;
36                 }
37                 disl=dp[i][j-1][0]+a[j]-a[i];
38                 disr=dp[i][j-1][1]+a[j]-a[j-1];
39                 if(disl<=disr){
40                     dp[i][j][1]=disl;
41                     path[i][j][1]=0;
42                 }
43                 else {
44                     dp[i][j][1]=disr;
45                     path[i][j][1]=1;
46                 }
47                 if(t[j]<=dp[i][j][1]){
48                     dp[i][j][1]=INF;
49                 }
50             }
51         }
52         int l,r,tmp;
53         if(dp[1][n][0]<INF){
54             tmp=path[1][n][0];
55             cout<<1;
56             l=2,r=n;
57         }
58         else if(dp[1][n][1]<INF){
59             tmp=path[1][n][1];
60             cout<<n;
61             l=1,r=n-1;
62         }
63         else {
64             cout<<"Mission Impossible"<<endl;
65             continue;
66         }
67         while(l<=r){
68             if(tmp==0){
69                 tmp=path[l][r][0];
70                 cout<< <<l;
71                 l++;
72             }
73             else {
74                 tmp=path[l][r][1];
75                 cout<< <<r;
76                 r--;
77             }
78         }
79         cout<<endl;
80     }
81 }

 

ps:辣鸡HDU,spj出锅了,UVA和ZOJ上都能过,但是HDU上就是WA!!

以上是关于『HDU 4053』The Last Puzzle的主要内容,如果未能解决你的问题,请参考以下文章

The Last Puzzle ZOJ - 3541

HDU 1405 The Last Practice

MU Puzzle HDU - 4662

HDU 2514 Another Eight Puzzle(DFS)

HDU 4662 MU Puzzle:找规律

HDU 5465 Clarke and puzzle Nim游戏+二维树状数组