ZOJ 3541

题目大意:有n个按钮,第i个按钮在按下ti 时间后回自动弹起,每个开关的位置是di,问什么策略按开关可以使所有的开关同时处于按下状态


Description


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 T1, T2, ..., 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 D1, D2, ..., 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.

Solution


本题很容易想到区间DP,对于一个区间,一定是从某个端点开始,因为如果从中间开始之后按别的开关时一定会经过这个点。

状态

\(f_{i,j,0/1}\)

表示区间[i,j]从左/右端点开始的最小时间。

状态转移方程见代码。

//Writer : Hsz %WJMZBMR%tourist%hzwer
#include <iostream>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <queue>
#include <map>
#include <set>
#include <stack>
#include <vector>
#include <cstdlib>
#include <algorithm>
const int inf=0x3fffffff;
#define LL long long
using namespace std;
const int N=222;
int n;
LL t[N],d[N],f[N][N][2];
bool way[N][N][2];
int main() {
while(scanf("%d",&n)!=EOF) {
memset(f,0,sizeof f);
for(int i=1; i<=n; i++)
scanf("%lld",&t[i]);
for(int i=1; i<=n; i++)
scanf("%lld",&d[i]);
for(int l=2; l<=n; l++) {
for(int i=1; i+l-1<=n; i++) {
int j=i+l-1; if(f[i+1][j][0]+d[i+1]-d[i]<f[i+1][j][1]+d[j]-d[i])
f[i][j][0]=f[i+1][j][0]+d[i+1]-d[i],way[i][j][0]=0; else f[i][j][0]=f[i+1][j][1]+d[j]-d[i],way[i][j][0]=1; if(t[i]<=f[i][j][0]||f[i][j][0]>=inf)
f[i][j][0]=inf; if(f[i][j-1][1]+d[j]-d[j-1]<=f[i][j-1][0]+d[j]-d[i])
f[i][j][1]=f[i][j-1][1]+d[j]-d[j-1],way[i][j][1]=1; else f[i][j][1]=f[i][j-1][0]+d[j]-d[i],way[i][j][1]=0; if(t[j]<=f[i][j][1]||f[i][j][1]>=inf)
f[i][j][1]=inf;
}
}
int l,r,v;
if(f[1][n][0]<inf) {
printf("1");
l=2,r=n,v=way[1][n][0];
} else if(f[1][n][1]<inf) {
printf("%d",n);
l=1,r=n-1,v=way[1][n][1];
} else {
puts("Mission Impossible");
continue;
}
while(l<=r) {
if(!v) printf(" %d",l),v=way[l][r][0],l++;
else printf(" %d",r),v=way[l][r][1],r--;
}
printf("\n");
}
return 0;
}

[ZOJ]3541 Last Puzzle (区间DP)的更多相关文章

  1. ZOJ 3469 Food Delivery 区间DP

    这道题我不会,看了网上的题解才会的,涨了姿势,现阶段还是感觉区间DP比较难,主要是太弱...QAQ 思路中其实有贪心的意思,n个住户加一个商店,分布在一维直线上,应该是从商店开始,先向两边距离近的送, ...

  2. POJ1651:Multiplication Puzzle(区间DP)

    Description The multiplication puzzle is played with a row of cards, each containing a single positi ...

  3. poj 1651 Multiplication Puzzle (区间dp)

    题目链接:http://poj.org/problem?id=1651 Description The multiplication puzzle is played with a row of ca ...

  4. zoj 3469 Food Delivery 区间dp + 提前计算费用

    Time Limit: 2 Seconds      Memory Limit: 65536 KB When we are focusing on solving problems, we usual ...

  5. ZOJ - 3469 Food Delivery (区间dp)

    When we are focusing on solving problems, we usually prefer to stay in front of computers rather tha ...

  6. ZOJ 3469 Food Delivery(区间DP好题)

    题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=4255 题目大意:在x轴上有n个客人,每个客人每分钟增加的愤怒值不同. ...

  7. POJ 1651 Multiplication Puzzle 区间dp(水

    题目链接:id=1651">点击打开链 题意: 给定一个数组,每次能够选择内部的一个数 i 消除,获得的价值就是 a[i-1] * a[i] * a[i+1] 问最小价值 思路: dp ...

  8. POJ1651Multiplication Puzzle[区间DP]

    Multiplication Puzzle Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 8737   Accepted:  ...

  9. ZOJ - 3537 Cake (凸包+区间DP+最优三角剖分)

    Description You want to hold a party. Here's a polygon-shaped cake on the table. You'd like to cut t ...

随机推荐

  1. 日志输出最不重要的就是控制台输出,控制台输出就是system.out而已

    1.日志输出最不重要的就是控制台输出,控制台输出就是system.out而已 2.所以日志输出时候会存在一个Bug就是:stdout要配置在日志输出的最前面,因为stdout控制台输出,最不重要,如果 ...

  2. TensorFlow 入门之手写识别CNN 三

    TensorFlow 入门之手写识别CNN 三 MNIST 卷积神经网络 Fly 多层卷积网络 多层卷积网络的基本理论 构建一个多层卷积网络 权值初始化 卷积和池化 第一层卷积 第二层卷积 密集层连接 ...

  3. 11153 kill boss

    11153 kill boss 时间限制:1000MS  内存限制:65535K提交次数:1090 通过次数:340 题型: 编程题   语言: G++;GCC Description Acmer最近 ...

  4. Object对象具体解释(二)之clone

    clone方法会返回该实例对象的一个副本,通常情况下x.clone() != x || x.clone().getClass() == x.getClass() || x.clone().equals ...

  5. 0x66 Tarjan算法与无向图联通性

    bzoj1123: [POI2008]BLO poj3694 先e-DCC缩点,此时图就变成了树,树上每一条边都是桥.对于添加边的操作,相当于和树上一条路径构环,导致该路径上所有边都不成为桥.那么找这 ...

  6. Gym-101915D Largest Group 最大独立集 Or 状态压缩DP

    题面题意:给你N个男生,N个女生,男生与男生之间都是朋友,女生之间也是,再给你m个关系,告诉你哪些男女是朋友,最后问你最多选几个人出来,大家互相是朋友. N最多为20 题解:很显然就像二分图了,男生一 ...

  7. 你要的 React 面试知识点,都在这了

    摘要: 问题很详细,插图很好看. 原文:你要的 React 面试知识点,都在这了 作者:前端小智 Fundebug经授权转载,版权归原作者所有. React是流行的javascript框架之一,在20 ...

  8. cloudfoundry service broker 制作

    实验室这边需要制作service broker.从今天开始将精力投入其中.

  9. linux下创建公钥

    # linux下创建公钥 链接地址:https://www.cnblogs.com/ibyte/p/6086630.html 示例: scp -r /home/yutang/.ssh/id_rsa.p ...

  10. Fail2ban + firewalld 防护doss攻击

    系统环境:centos7.3 用途:利用fail2ban+Firewalld来防CC攻击和SSH爆破 准备工作: 1.检查Firewalld是否启用 #如果您已经安装iptables建议先关闭 ser ...