zoj 3541 The Last Puzzle 区间dp

The Last Puzzle

Time Limit: 2 Seconds       Memory Limit: 65536 KB       Special Judge



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.

Input

The input file would contain multiple cases. Each case contains three lines. Process to the end of file.

The first line contains a single integer n(1 ≤ n ≤200), the number of buttons.

The second line contains n integers T1T2, ..., Tn, where Ti(1 ≤ Ti ≤ 1,000,000) is the time the ith button would automatic pop up after pressing it, in units of second.

The third line contains n integers D1D2, ..., Dn, where Di(1 ≤ Di ≤ 1,000,000) is the time hero needed to go between the ith button and the first button, in units of second. The sequence will be in ascending order and the first element is always 0.

Output

Output a single line containing n integers which is the sequence of button to press by the hero. If there are multiply sequences, anyone will do. If there is no way for the hero to solve the puzzle, just output "Mission Impossible"(without quote) in a single line.

Sample Input
2
4 3
0 3
2
3 3
0 3
4
5 200 1 2
0 1 2 3
Sample Output
1 2
Mission Impossible
1 2 4 3
Hint

In the second sample, no matter which button the hero pressed first, the button would always pop up before he press the other button. So there is no way to make all the button pressed down.


Author:  WANG, Yelei
Contest:  The 2011 ACM-ICPC Asia Dalian Regional Contest

Submit     Status
一条直线上有若干个按钮每个按钮都晕一个阈值 即按下后过了这个时间就会恢复按钮与按钮之间有距离每走一步花费一个单位的时间
问如何让按钮全部按下
分析对于区间【i,j】若使得花时间最小应该从两头开始按,对于三元组dp【i,j,k】表示区间i到j中是从k边开始按 k取0时代表从左1代表从右
ACcode:
#pragma warning(disable:4786)//使命名长度不受限制
#pragma comment(linker, "/STACK:102400000,102400000")//手工开栈
#include <map>
#include <set>
#include <queue>
#include <cmath>
#include <stack>
#include <cctype>
#include <cstdio>
#include <cstring>
#include <stdlib.h>
#include <iostream>
#include <algorithm>
#define rd(x) scanf("%d",&x)
#define rd2(x,y) scanf("%d%d",&x,&y)
#define rd3(x,y,z) scanf("%d%d%d,&x,&y,&z)
#define rdl(x) scanf("%I64d,&x);
#define rds(x) scanf("%s",x)
#define rdc(x) scanf("%c",&x)
#define ll long long int
#define ull unsigned long long
#define maxn 1005
#define mod 1000000007
#define INF 1<<30//int 最大值
#define FOR(i,f_start,f_end) for(int i=f_start;i<=f_end;++i)
#define MT(x,i) memset(x,i,sizeof(x))
#define PI  acos(-1.0)
#define E  exp(1)
#define eps 1e-8
ll gcd(ll a,ll b){return b==0?a:gcd(b,a%b);}
ll mul(ll a,ll b,ll p){ll sum=0;for(;b;a=(a+a)%p,b>>=1)if(b&1)sum=(sum+a)%p;return sum;}
inline void Scan(int &x) {
      char c;while((c=getchar())<'0' || c>'9');x=c-'0';
      while((c=getchar())>='0' && c<='9') x=(x<<3)+(x<<1)+c-'0';
}
using namespace std;
int dp[maxn][maxn][2],path[maxn][maxn][2];
int a[maxn],t[maxn];
int main(){
    int n,loop,cnt=1;
    while(rd(n)!=EOF){
        FOR(i,1,n)rd(t[i]);
        FOR(i,1,n)rd(a[i]);
        MT(dp,0);
        FOR(l,2,n)for(int i=1;i+l-1<=n;++i){
            int j=i+l-1;
            dp[i][j][0]=min(dp[i+1][j][0]+a[i+1]-a[i],dp[i+1][j][1]+a[j]-a[i]);
            path[i][j][0]=(dp[i+1][j][0]+a[i+1]-a[i]>=dp[i+1][j][1]+a[j]-a[i]);
            if(dp[i][j][0]>=t[i] || dp[i][j][0]>INF) dp[i][j][0]=INF;
            dp[i][j][1]=min(dp[i][j-1][1]+a[j]-a[j-1],dp[i][j-1][0]+a[j]-a[i]);
            path[i][j][1]=(dp[i][j-1][0]+a[j]-a[i]>=dp[i][j-1][1]+a[j]-a[j-1]);
            if(dp[i][j][1]>=t[j] || dp[i][j][1]>INF)  dp[i][j][1]=INF;
        }
        int l=1,r=n,p;
        if(dp[1][n][0]<INF){
            p=path[1][n][0];
            printf("%d",l++);
        }
        else if(dp[1][n][1]<INF){
            p=path[1][n][1];
            printf("%d",r--);
        }
        else{
            printf("Mission Impossible\n");
            continue;
        }
        while(l<=r){
            if(!p){
                p=path[l][r][0];
                printf(" %d",l++);
            }else {
                p=path[l][r][1];
                printf(" %d",r--);
            }
        }
        putchar('\n');
    }
    return 0;
}



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值