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


  1. 2
  2. 4 3
  3. 0 3
  4. 2
  5. 3 3
  6. 0 3
  7. 4
  8. 5 200 1 2
  9. 0 1 2 3

Sample Output


  1. 1 2
  2. Mission Impossible
  3. 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]从左/右端点开始的最小时间。

状态转移方程见代码。

  1. //Writer : Hsz %WJMZBMR%tourist%hzwer
  2. #include <iostream>
  3. #include <cstring>
  4. #include <cstdio>
  5. #include <cmath>
  6. #include <queue>
  7. #include <map>
  8. #include <set>
  9. #include <stack>
  10. #include <vector>
  11. #include <cstdlib>
  12. #include <algorithm>
  13. const int inf=0x3fffffff;
  14. #define LL long long
  15. using namespace std;
  16. const int N=222;
  17. int n;
  18. LL t[N],d[N],f[N][N][2];
  19. bool way[N][N][2];
  20. int main() {
  21. while(scanf("%d",&n)!=EOF) {
  22. memset(f,0,sizeof f);
  23. for(int i=1; i<=n; i++)
  24. scanf("%lld",&t[i]);
  25. for(int i=1; i<=n; i++)
  26. scanf("%lld",&d[i]);
  27. for(int l=2; l<=n; l++) {
  28. for(int i=1; i+l-1<=n; i++) {
  29. int j=i+l-1;
  30. if(f[i+1][j][0]+d[i+1]-d[i]<f[i+1][j][1]+d[j]-d[i])
  31. f[i][j][0]=f[i+1][j][0]+d[i+1]-d[i],way[i][j][0]=0;
  32. else f[i][j][0]=f[i+1][j][1]+d[j]-d[i],way[i][j][0]=1;
  33. if(t[i]<=f[i][j][0]||f[i][j][0]>=inf)
  34. f[i][j][0]=inf;
  35. if(f[i][j-1][1]+d[j]-d[j-1]<=f[i][j-1][0]+d[j]-d[i])
  36. f[i][j][1]=f[i][j-1][1]+d[j]-d[j-1],way[i][j][1]=1;
  37. else f[i][j][1]=f[i][j-1][0]+d[j]-d[i],way[i][j][1]=0;
  38. if(t[j]<=f[i][j][1]||f[i][j][1]>=inf)
  39. f[i][j][1]=inf;
  40. }
  41. }
  42. int l,r,v;
  43. if(f[1][n][0]<inf) {
  44. printf("1");
  45. l=2,r=n,v=way[1][n][0];
  46. } else if(f[1][n][1]<inf) {
  47. printf("%d",n);
  48. l=1,r=n-1,v=way[1][n][1];
  49. } else {
  50. puts("Mission Impossible");
  51. continue;
  52. }
  53. while(l<=r) {
  54. if(!v) printf(" %d",l),v=way[l][r][0],l++;
  55. else printf(" %d",r),v=way[l][r][1],r--;
  56. }
  57. printf("\n");
  58. }
  59. return 0;
  60. }

[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. 《简明 Python 教程》笔记

    基础 字符串:python 中字符串可以用单引号.双引号和三个引号括起来,其中三个引号可以用来指定多行的字符串. print('hello'* 3) 连续打印 3 个 hello 格式化:print ...

  2. [Javascript] Getter and Setter Abstractions

    JavaScript provides primitive types and means of processing those. However, those are not enough. Re ...

  3. rsync与cwRsync

    以下这个链接是windows之间的csRsync和csRsyncServer同步教程: http://www.cnblogs.com/wwufengg/p/rsync-config.html !Att ...

  4. FPGA 浮点单元设计

    浮点数在内存中的存放格式例如以下: 地址 +0 +1 +2 +3 内容 SEEE EEEE EMMM MMMM MMMM MMMM MMMM MMMM 这里 S 代表符号位,1是负,0是正 E 偏移1 ...

  5. 如何做URL静态化 和页面的静态化

    为什么要进行URL静态化? 如果帮到了您,您可以小支持一下,谢谢您   1.更好的迎合搜索引擎工作原理的爬行抓取机制:2.把网站URL静态化更有助于网站获得好的排名:3.URL静态化有利于用户体验.不 ...

  6. [源码管理] Microsoft Visual SourceSafe 2005 下载与配置

    一.VSS2005的下载地址是:百度搜索关键字:vss, 二.配置Microsoft Visual SourceSafe 2005的Internet访问 VSS2005发布以后,早就听说可以支持Int ...

  7. Jquery 重置表单

    1.重置表单回初始状态 $('#fromid')[0].reset(); 此方法一步到位,不需要一个个的去赋值为空

  8. .net中的TreeView的数据绑定与EasyUi_tree的数据绑定

    昨天看到了.net中的TreeView,学习了一波TreeView的数据绑定,联想到EasyUi中的Tree的数据,觉得里面的逻辑差不多,就总结了一下两者的数据绑定. 前端页面和必要的JS如下 < ...

  9. Navcat12 for mysq 激活工具

    Navcat12 for mysq 激活 https://www.cnblogs.com/sunyangCoder/p/9060454.html

  10. ACM-ICPC北京赛区[2017-11-19]

    Domains K-Dimensional Foil Graph Chinese Checkers Cats and Fish #include<stdio.h> #include< ...