Spiderman’s workout (dynamic programming)
来源:互联网 发布:对网络表情包的看法 编辑:程序博客网 时间:2024/06/07 12:49
题目:
Spiderman’s workout
Time Limit:1000MS Memory Limit:65536K
Total Submit:667 Accepted:244 Special Judged
Description
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
Sample Output
Source
Svenskt Mästerskap i Programmering/Norgesmesterskapet 2003
解: 在第i步可以到达某些一定的高度都必须已经是最优解(曾走过的高度最低),那么i+1步中可以到达的高度最多只有两种可能性,得出最优解(因为第i步的高度确定的话,并不影响后面的走法)。
代码如下:
Source
Problem Id:3003 User Id:bmexue
Memory:224K Time:15MS
Language:C++ Result:Accepted
- Source
#include < iostream > using namespace std; const int MAXN = 501 ; const int INF = 2000000000 ; int dp[ 41 ][MAXN]; //第i步在高度j的最优解 int s[ 41 ][MAXN]; //在第i步到达高度j的走发 int a[ 41 ]; void print( int n, int m) { if (n == 0 ) return ; if (s[n][m] == 0 ){ print(n - 1 , m - a[n]); printf( "U" ); } else { print(n - 1 , m + a[n]); printf( "D" ); } } int max(int i, int j) //改成内敛函数反而增加时间 { if(i>j) return i; return j; } void solve() { int n; int i, j; int t1, t2; int maxn = 0 ; scanf( "%d" , &n); for (i = 1 ; i <= n; i ++ ){ scanf( "%d" , &a[i]); maxn += a[i]; } if(maxn%2 !=0) //这样优化后时间变少{ printf( "IMPOSSIBLE/n" ); return ;} maxn = maxn/2 +1; //这样优化后时间变少 memset(dp, - 1 , sizeof (dp)); //初始化时间太多,要尽量缩小数组范围! memset(s, - 1 , sizeof (s)); dp[ 1 ][a[ 1 ]] = a[ 1 ]; s[ 1 ][a[ 1 ]] = 0 ;for (i = 2 ; i <= n; i ++ ){ for (j = 0 ; j < maxn; j ++ ){ t1 = INF; t2 = INF; if (j - a[i] >= 0 && dp[i - 1 ][j - a[i]] != - 1 ) //up t1 = max(j, dp[i - 1 ][j - a[i]]); if (j + a[i] < maxn && dp[i - 1 ][j + a[i]] != - 1 ) //down t2 = max(j, dp[i - 1 ][j + a[i]]); if (t1 < t2){ dp[i][j] = t1; s[i][j] = 0 ; } else { if (t2 != INF){ dp[i][j] = t2; s[i][j] = 1 ; } } } } if (dp[n][ 0 ] == - 1 ) printf( "IMPOSSIBLE/n" ); else { print(n, 0); printf( "/n" ); } } int main(){ int caseTime; scanf( " %d " , & caseTime); int i=0; while (i<caseTime) { solve();i++; } return 0 ;}
- Spiderman’s workout (dynamic programming)
- POJ 3003 Spiderman’s workout
- HOJ 2139 Spiderman's workout(动态规划)
- A. Greg's Workout
- Greg's Workout
- A. Greg's Workout
- A. Greg's Workout CodeForces255A
- spiderman's 点滴(1)
- spiderman's 点滴(2)
- codeforces 255A Greg's Workout
- dynamic programming
- Dynamic Programming
- Dynamic Programming
- dynamic programming
- Dynamic Programming
- dynamic programming
- Dynamic Programming
- Dynamic Programming
- 在.NET下多层架构企业管理系统的开发
- 11月4日JPetStore与工作流培训日记
- 浏览器滚动条的参数总结
- 沟通三境界
- 打开一个光秃秃的只有页面的窗口
- Spiderman’s workout (dynamic programming)
- 基于动态页面的静态页面实现
- 自制CSS颜色选择器
- Ajax-Atlas简单调用Web服务经验谈- -
- 基本算法连载(4)-杂七杂八的树
- c#.net常用的小函数和方法集
- DataGrid使用心得
- ASP.NET之精通弹出窗口
- 求解要成为程序员应该学什么知识!!急求