区间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.
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.
题意:有一列石头,只有全部按下的时候才能打开大门,可以任意选择按下的起点,但是每个石头还有一个隔一段会弹起的时间,问是否存在一种方案可以打开大门。
思路分析:
首先这个人如果可以成功打开石门的按下的方案一定是每次都是从左侧或右侧去按,不然你先中间按下一次,但是它的左侧或右侧还有没按的,这时如果再回去按,那么先按的中间的这个显然时间会变长,因此是不可取的。
类似送外卖那个问题,dp[i][j][0]表示送完区间(i, j)后此人位于区间的左侧,dp[i][j][1]表示位于右侧,那么就可以得到处理完全部区间后最优的时间,但是在求的过程要判断一下当前扩展出来的区间的是否是满足题目要求的,同时记录一下路径,最后倒着往回想一下就可以了。
代码示例:
int n;
int t[205], d[205];
int dp[205][205][2];
int path[205][205][2]; int main() {
//freopen("in.txt", "r", stdin);
//freopen("out.txt", "w", stdout); while(~scanf("%d", &n)){
for(int i = 1; i <= n; i++) scanf("%d", &t[i]);
for(int i = 1; i <= n; i++) scanf("%d", &d[i]); memset(dp, 0, sizeof(dp));
for(int len = 2; len <= n; len++){
for(int i = 1; i <= n; i++){
int j = i+len-1;
if (j > n) break;
if (dp[i+1][j][0]+d[i+1]-d[i] <= dp[i+1][j][1]+d[j]-d[i]){
dp[i][j][0] = dp[i+1][j][0]+d[i+1]-d[i];
path[i][j][0] = 0;
}
else {
dp[i][j][0] = dp[i+1][j][1]+d[j]-d[i];
path[i][j][0] = 1;
}
if (t[i] <= dp[i][j][0]) dp[i][j][0] = inf; if (dp[i][j-1][1]+d[j]-d[j-1] <= dp[i][j-1][0]+d[j]-d[i]){
dp[i][j][1] = dp[i][j-1][1]+d[j]-d[j-1];
path[i][j][1] = 1;
}
else {
dp[i][j][1] = dp[i][j-1][0]+d[j]-d[i];
path[i][j][1] = 0;
}
if (t[j] <= dp[i][j][1]) dp[i][j][1] = inf;
}
} int tem;
int l = 1, r = n;
if (dp[1][n][0] < inf){
printf("1");
tem = path[1][n][0];
l++;
}
else if (dp[1][n][1] < inf){
printf("%d", n);
tem = path[1][n][1];
r--;
}
else {printf("Mission Impossible\n"); continue;} while(l <= r){
if (tem == 0){
tem = path[l][r][0];
printf(" %d", l++);
}
else{
tem = path[l][r][1];
printf(" %d", r--);
}
}
printf("\n");
}
return 0;
}
区间dp - 全部按下一列石头的更多相关文章
- 区间DP(力扣1000.合并石头的最低成本)
一.区间DP 顾名思义区间DP就是在区间上进行动态规划,先求出一段区间上的最优解,在合并成整个大区间的最优解,方法主要有记忆化搜素和递归的形式. 顺便提一下动态规划的成立条件是满足最优子结构和无后效性 ...
- LightOJ1031 Easy Game(区间DP)
我可能真想不到这题是区间DP,不过知道是区间DP想了下就AC了. dp[i][j]表示局面为ai...aj先手能获得与后手得分的最大差值 那么转移到当前状态就是枚举中间的位置,分成两边,其中一边先手全 ...
- 区间dp总结篇
前言:这两天没有写什么题目,把前两周做的有些意思的背包题和最长递增.公共子序列写了个总结.反过去写总结,总能让自己有一番收获......就区间dp来说,一开始我完全不明白它是怎么应用的,甚至于看解题报 ...
- [BZOJ 1652][USACO 06FEB]Treats for the Cows 题解(区间DP)
[BZOJ 1652][USACO 06FEB]Treats for the Cows Description FJ has purchased N (1 <= N <= 2000) yu ...
- 区间DP(总结)
学长一晚上的耐心讲解,使我明白区间DP这么高级的东西,还是挺容易的.也就是在一段区间内的动态规划. 下面用例题进行总结. 例题:石子归并. 描述 有N堆石子排成一排,每堆石子有一定的数量.现要将N堆石 ...
- 区间DP小结
也写了好几天的区间DP了,这里稍微总结一下(感觉还是不怎么会啊!). 但是多多少少也有了点感悟: 一.在有了一点思路之后,一定要先确定好dp数组的含义,不要模糊不清地就去写状态转移方程. 二.还么想好 ...
- 洛谷P1514 引水入城 [搜索,区间DP]
题目传送门 引水入城 题目描述 在一个遥远的国度,一侧是风景秀美的湖泊,另一侧则是漫无边际的沙漠.该国的行政区划十分特殊,刚好构成一个 N 行×M 列的矩形,如上图所示,其中每个格子都代表一座城市,每 ...
- 区间DP【p2858】[USACO06FEB]奶牛零食Treats for the Cows
Description 约翰经常给产奶量高的奶牛发特殊津贴,于是很快奶牛们拥有了大笔不知该怎么花的钱.为此,约翰购置了N(1≤N≤2000)份美味的零食来卖给奶牛们.每天约翰售出一份零食.当然约翰希望 ...
- 简单Dp----最长公共子序列,DAG最长路,简单区间DP等
/* uva 111 * 题意: * 顺序有变化的最长公共子序列: * 模板: */ #include<iostream> #include<cstdio> #include& ...
随机推荐
- P1077 子串乘积正负分类
题目描述 给你一个序列包含 \(n\) 个元素的序列 \(a_1, a_2, \dots , a_n\) (每个元素 \(a_i \ne 0\)). 你需要计算如下两个值: 有多少对数 \((l, r ...
- PHP mysqli扩展整理,包括面向过程和面向对象的比较\事务控制\批量执行\预处理
相关文章:PHP的mysql扩展整理,操作数据库的实现过程分析 PHP PDO扩展整理,包括环境配置\基本增删改查\事务\预处理 介绍 mysqli是PHP程序与mysql数据库进行数据交互的桥梁, ...
- the password has expired
Oracle提示错误消息ORA-28001: the password has expired,是由于Oracle11G的新特性所致, Oracle11G创建用户时缺省密码过期限制是180天(即6个月 ...
- Linux 内核USB 接口配置
USB 接口是自己被捆绑到配置的. 一个 USB 设备可有多个配置并且可能在它们之间转换 以便改变设备的状态. 例如, 一些允许固件被下载到它们的设备包含多个配置来实现这个. 一个配置只能在一个时间点 ...
- Luogu P4173 残缺的字符串-FFT在字符串匹配中的应用
P4173 残缺的字符串 FFT在字符串匹配中的应用. 能解决大概这种问题: 给定长度为\(m\)的A串,长度为\(n\)的B串.问A串在B串中的匹配数 我们设一个函数(下标从\(0\)开始) \(C ...
- mangoDB 储存 id为objectid
- IDEA Maven创建多个Module相互依赖
1.前言 在大型企业项目中,系统架构复杂多变,一个项目根本无法支撑起所有业务.为了提高项目扩展性.灵活性.重用性,封装性,将项目分为多个Module是非常必要的. 这里就不说IDEA如何安装了,安装好 ...
- 使用Python完成SAP客户端的打开和系统登陆
最近小爬一直思忖着如何将以前写的一些半自动化程序转为全自动化,这其中就涉及到SAP的打开和登录过程.我们都知道,SAP原生的“脚本录制和回放”功能是在用户进入到某一个SAP”用户指定系统“后才可以启用 ...
- 【Python系统学习02】数据类型与类型转换
一.数据类型 字符串 整数 浮点数 [补充中...] 1.字符串 str 字符串:英文string,简写str. name = '小石头:' print(name,'2019', '12', '24' ...
- redis 和 memcached 有什么区别?redis 的线程模型是什么?为什么 redis 单线程却能支撑高并发?
redis 和 memcached 有啥区别? redis 支持复杂的数据结构 redis 相比 memcached 来说,拥有更多的数据结构,能支持更丰富的数据操作.如果需要缓存能够支持更复杂的结构 ...