There is one last gate between the hero and the dragon. But opening the gate isn't an easy task.
There wereNButtons 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 wocould 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 wocould pop up after a while before he cocould press all the buttons down. he soon realized that the integer on the button is the time when the button wowould 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 cocould reach per second. even with this information, the hero cocould not figure out in what order he shocould 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 cocould begin from any button.
Input
The input file wocould contain multiple cases. Each case contains three lines. process to the end of file.
The first line contains a single integerN(1 ≤N≤ 200), the number of buttons.
The second line containsNIntegersT1,T2,...,TN, WhereTi(1 ≤Ti≤ 1,000,000) is the time the ith button wocould automatic pop up after pressing it, in units of second.
The third line containsNIntegersD1,D2,...,DN, WhereDi(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
24 30 323 30 345 200 1 20 1 2 3
Sample output
1 2Mission Impossible1 2 4 3
Hint
In the second sample, no matter which button the hero pressed first, the button wowould always pop up before he press the other button. So there is no way to make all the button pressed down.
Question: A forward switch is arranged. All the switches should be followed by the following steps. It takes one unit time to move one unit to give the position of each switch, after each switch is pressed, after a certain period of time, it will pop up again.
Idea: DP [I] [J] [k] indicates the time spent by pressing all I and j, k = 0 indicates stopping at I, k = 1 indicates stopping at J, open next to record the path, which is similar to 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;}