HOJ 2139 Spiderman's workout(动态规划)
来源:互联网 发布:淘宝口碑好的玉器店 编辑:程序博客网 时间:2024/06/13 02:53
Spiderman’s workout
My Tags (Edit)
Source : Nordic Collegiate Programming Contest 2003
Time limit : 3 sec Memory limit : 32 M
Submitted : 93, Accepted : 59
Staying fit is important for every super hero, and Spiderman is no exception. Every day he undertakes a climbing exercise in which he climbs a certain distance, rests for a minute, then climbs again, rests again, and so on. The exercise is described by a sequence of distances d1, d2, … , dm telling how many meters he is to climb before the first first break, before the second break, and so on. Froman exercise perspective it does not really matter if he climbs up or down at the i:th climbing stage, but it is practical to sometimes climb up and sometimes climb down so that he both starts and finishes at street level. Obviously, he can never be below street level. Also, he would like to use as low a building as possible (he does not like to admit it, but he is actually afraid of heights). The building must be at least 2 meters higher than the highest point his feet reach during the workout.
He wants your help in determining when he should go up and when he should go down. The answer must be legal: it must start and end at street level (0 meters above ground) and it may never go below street level. Among the legal solutions he wants one that minimizes the required building height. When looking for a solution, you may not reorder the distances.
If the distances are 20 20 20 20 he can either climb up, up, down, down or up, down, up, down. Both are legal, but the second one is better (in fact optimal) because it only requires a building of height 22, whereas the first one requires a building of height 42. If the distances are 3 2 5 3 1 2, an optimal legal solution is to go up, up, down, up, down, down. Note that for some distance sequences there is no legal solution at all (e.g., for 3 4 2 1 6 4 5).
Input
The first line of the input contains an integer N giving the number of test scenarios. The following 2N lines specify the test scenarios, two lines per scenario: the first line gives a positive integer M <= 40 which is the number of distances, and the following line contains the M positive integer distances. For any scenario, the total distance climbed (the sum of the distances in that scenario) is at most 1000.
Output
For each input scenario a single line should be output. This line should either be the string “IMPOSSIBLE” if no legal solution exists, or it should be a string of length M containing only the characters “U” and “D”, where the i:th character indicates if Spiderman should climb up or down at the i:th stage. If there are several different legal and optimal solutions, output one of them (it does not matter which one as long as it is optimal).
Sample Input
3
4
20 20 20 20
6
3 2 5 3 1 2
7
3 4 2 1 6 4 5
Sample Output
UDUD
UUDUDD
IMPOSSIBLE
现在越来越发现做动态规划题目关键是状态表示,如果你想到正确的状态,那么状态递推就容易了。
#include <iostream>#include <string.h>#include <stdio.h>#include <algorithm>#include <math.h>#include <stdlib.h>using namespace std;#define MAX 10000000int dp[45][1005];int pre[45][1005];int n;int a[45];void dfs(int x,int height){ if(x==0) return; dfs(x-1,pre[x][height]); if(height>pre[x][height]) printf("U"); else printf("D");}int main(){ int t; scanf("%d",&t); while(t--) { scanf("%d",&n); for(int i=1;i<=n;i++) { scanf("%d",&a[i]); } for(int i=0;i<=n;i++) for(int j=0;j<=1000;j++) dp[i][j]=MAX; dp[0][0]=0; for(int i=1;i<=n;i++) { for(int j=0;j<=1000;j++) { if(j-a[i]>=0&&dp[i-1][j-a[i]]!=MAX) { if(dp[i][j]>max(dp[i-1][j-a[i]],j)) { dp[i][j]=max(dp[i-1][j-a[i]],j); pre[i][j]=j-a[i]; } } if(j+a[i]<=1000&&dp[i-1][j+a[i]]!=MAX) { if(dp[i][j]>max(dp[i-1][j+a[i]],j)) { dp[i][j]=max(dp[i-1][j+a[i]],j); pre[i][j]=j+a[i]; } } } } if(dp[n][0]==MAX) printf("IMPOSSIBLE\n"); else { dfs(n,0); printf("\n"); } } return 0;}
- HOJ 2139 Spiderman's workout(动态规划)
- (中等)动态规划Hoj 1719 Spiderman
- Spiderman’s workout (dynamic programming)
- POJ 3003 Spiderman’s workout
- spiderman's 点滴(1)
- spiderman's 点滴(2)
- HOJ-1005 Fast Food(动态规划)
- HOJ-2056 Bookshelf(线性动态规划)
- HOJ 2252 The Priest(动态规划)
- 动态规划-HOJ-2571
- A. Greg's Workout
- Greg's Workout
- A. Greg's Workout
- (中等) 动态规划 HOJ 2177 ICPC Strikes Again
- (中等) 动态规划+搜索 HOJ 2156 Colored stones
- HOJ 2133&POJ 2964 Tourist(动态规划)
- A. Greg's Workout CodeForces255A
- 哈工大hoj第1042题,动态规划
- JAVA常用库类
- 浅析操作系统----2、操作系统概述
- Bedtime Reading I--水题
- MySQL索引背后的数据结构及算法
- 虚函数里面调用虚函数的输出
- HOJ 2139 Spiderman's workout(动态规划)
- 归并排序【模板】
- java单点登录系统CAS的简单使用
- poj 1061 青蛙的约会 拓展欧几里德 解二元一次不定方程
- C#虚函数virtual详解
- Xcode build解密
- 判断输入的是否是整数
- STM32为什么必须先配置时钟再配置GPIO
- Prime Palindromes--回文+素数判断