ZOJ3541:The Last Puzzle(区间DP)

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.

题意:一排在一条献上的开关,要把所有开关全部按下的步骤,每移动一个单位距离需要一个单位时间,给出每个开关的位置,并且每个开关按下后经过一定的时间又会弹起来,求出步骤

思路:dp[i][j][k]表示把i,j全部按下花费的时间,k=0表示停在i,k=1表示停在j,再开个next进行路径记录,跟ZOJ3469类似

#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;
#define up(i,x,y) for(i=x;i<=y;i++)
#define down(i,x,y) for(i=x;i>=y;i--)
#define mem(a,b) memset(a,b,sizeof(a))
#define w(x) while(x)
#define ss(x) scanf("%d",&x)
const int inf = 1<<30;
int dp[205][205][2],t[205],d[205],next[205][205][2];

int main()
{
    int i,j,k,l,n;
    w(~scanf("%d",&n))
    {
        up(i,1,n)
        ss(t[i]);
        up(i,1,n)
        ss(d[i]);
        mem(dp,0);
        up(l,2,n)
        {
            up(i,1,n-l+1)
            {
                j=i+l-1;
                dp[i][j][0]=min(dp[i+1][j][0]+d[i+1]-d[i],dp[i+1][j][1]+d[j]-d[i]);
                next[i][j][0]=(dp[i+1][j][0]+d[i+1]-d[i]>=dp[i+1][j][1]+d[j]-d[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]+d[j]-d[j-1],dp[i][j-1][0]+d[j]-d[i]);
                next[i][j][1]=(dp[i][j-1][0]+d[j]-d[i]>=dp[i][j-1][1]+d[j]-d[j-1]);
                if(dp[i][j][1]>=t[j] || dp[i][j][1]>inf)
                    dp[i][j][1]=inf;
            }
        }
        int l,r,m;
        if(dp[1][n][0]<inf)
        {
            l=2,r=n,m=next[1][n][0];
            printf("1");
        }
        else if(dp[1][n][1]<inf)
        {
            l=1,r=n-1,m=next[1][n][1];
            printf("%d",n);
        }
        else
            printf("Mission Impossible");
        while(l<=r)
        {
            if(m==0)
            {
                printf(" %d",l);
                m=next[l][r][m];
                l++;
            }
            else
            {
                printf(" %d",r);
                m=next[l][r][m];
                r--;
            }
        }
        printf("\n");
    }

    return 0;
}

ZOJ3541:The Last Puzzle(区间DP),布布扣,bubuko.com

时间: 2024-08-09 02:18:55

ZOJ3541:The Last Puzzle(区间DP)的相关文章

POJ1651 Multiplication Puzzle 区间dp

题意:给一个序列,每次选一个数(记为b),计算左边离他最近的数(记为a),右边离他最近的数(记为c),计算a*b*c,sum+=a*b*c 重复操作直到剩下开头结尾两个数 不同的方案对应不同的sum 计算最小的sum值 分析:典型的区间dp,dp[i][j]表示把从i到j所有的数都选走得到的最小值 dp[i][j]=min(dp[i][j],dp[i][k-1]+dp[k+1][j]+num[k]*num[i-1]*num[j+1]): 注:记得预处理dp[i][i],i(2~n-1) #inc

POJ 1651 Multiplication Puzzle 区间DP

题意:在一个数字序列中, 取出不包括头和尾的所有数字, 每次取出一个数字的代价等于取出的这个数和左右两个数的乘积, 求总代价和最小的取法 此小到大递归 1 //#pragma comment(linker, "/STACK:167772160")//手动扩栈~~~~hdu 用c++交 2 #include <cstdio> 3 #include <cstring> 4 #include <cstdlib> 5 #include <iostrea

POJ 1651 Multiplication Puzzle 区间dp(水

题目链接:点击打开链 题意: 给定一个数组,每次可以选择内部的一个数 i 消除,获得的价值就是 a[i-1] * a[i] * a[i+1] 问最小价值 思路: dp[l,r] = min( dp[l, i] + dp[i, r] + a[l] * a[i] * a[r]); #include <cstdio> #include <iostream> #include <algorithm> #include <queue> #include <cst

POJ1651:Multiplication Puzzle(区间DP 最优矩阵链乘)

题意:除了头尾不能动,每次取出一个数字,这个数字与左右相邻数字的乘积为其价值,最后将所有价值加起来,要求最小值 和最优矩阵链乘模型一样,最后取出的数决定了序,如果没学过最优矩阵连乘找重复子问题还是比较难找的 DP //180K 0MS #include<cstdio> #include<cstring> #include<iostream> #include<algorithm> using namespace std; int dp[110][110];

POJ 1651 Multiplication Puzzle (区间dp)

题目大意:对n个数组成的序列取数,规定最两边不能取,每次取一个a[i],得到 a[l] * a[i] * a[r] 的分数(a[l]是a[i]左边的数,a[r]是a[i]右边的数),并把这个数从序列中移走,求n-2次取数后的得分和的最小值 分析:正着确定状态不好做,不如反着来,设dp[l][r]为向区间[l, r]中填满数所得到分数和的最小值,考虑最近一次填数的位置,不难得出: dp[l][r] = fmin(dp[l][m] + dp[m][r] + a[l] * a[m] * a[r]) (

POJ 区间DP Multiplication Puzzle

The multiplication puzzle is played with a row of cards, each containing a single positive integer. During the move player takes one card out of the row and scores the number of points equal to the product of the number on the card taken and the numb

POJ 1651 Multiplication Puzzle 题解 区间DP

题目链接:http://poj.org/problem?id=1651[题目描述]<乘法谜题>乘法谜题源自这样一个背景,我们有一行 n 张牌,平铺在桌面上,每张牌的牌面上都标有一个正整数.玩家的初始得分是 0,他接下来要进行 n-2 次操作,每次操作他都需要从桌面上取出一张牌,然后他的得分会加上他取出的这张牌与取出的这张牌的左边的牌以及取出的这张牌的右边的牌的乘积.在第 n-2 次操作结束后,桌面上将会只剩下两张牌.你的目的是帮助玩家决定取牌的顺序,使得玩家的最终得分最小.举个例子,如果一开始

HDU5900 QSC and Master(区间DP + 最小费用最大流)

题目 Source http://acm.hdu.edu.cn/showproblem.php?pid=5900 Description Every school has some legends, Northeastern University is the same. Enter from the north gate of Northeastern University,You are facing the main building of Northeastern University.

2016 ACM/ICPC Asia Regional Shenyang Online 1009/HDU 5900 区间dp

QSC and Master Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Total Submission(s): 859    Accepted Submission(s): 325 Problem Description Every school has some legends, Northeastern University is the same. Enter