poj2397 Spiderman
2015-12-08 17:14
309 查看
Spiderman
DescriptionStaying fit is important for every superhero, and Spiderman is no exception. Every day he undertakes a climbing exercise in which he climbs a certain height of a tall building, rests for a minute, then climbs again, rests again, and so on. The exercise is describedby a sequence of heights d1, d2, d3, ... , dm telling how many meters he is to climb before the first break, before the second break, and so on. From an exercise perspective it does not really matter if he climbs up or down at the ith climbing stage, but itis practical that sometimes climbs up and sometimes climbs down, so that he both starts and finishes at street level. Obviously, he can never be below street level! Also, he would like to climb as short 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 minimizesthe required building height. When looking for a solution, you may not reorder 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 height42. 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).InputThe first line of the input contains an integer N giving the number of test cases. The following 2N lines specify the test cases, two lines per test case: the first line gives a positive integer M, which is the number of heights, and the following line containsthe M positive integer heights. For any test case, the total distance climbed (the sum of the heights in that test case) is at most 1000.OutputFor each test case 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 ith character indicates if Spidermanshould climb up or down at the ith 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
Time Limit: 1000MS | Memory Limit: 65536K | |||
Total Submissions: 1634 | Accepted: 624 | Special Judge |
3 4 20 20 20 20 6 3 2 5 3 1 2 7 3 4 2 1 6 4 5Sample Output
UDUD UUDUDD IMPOSSIBLE
//<span style="font-family: Arial; font-size: 14px; line-height: 26px;">dp[i][j]代表第i步爬到j高度时所爬过的最高点,同时用pr[i][j]代表上一次的高度</span>
#include<cstdio>#include<cstring>#include<algorithm>using namespace std;int dp[50][1050],pr[50][1050];//01背包的变形,感觉还是没有完全理解透背包问题。。。int inf=0x3f3f3f3f;void dfs(int k,int d){if(pr[k][d]!=-1){dfs(k-1,pr[k][d]);printf("%c",(pr[k][d]>d)?'D':'U');}}int n;int main(){int T;scanf("%d",&T);while(T--){scanf("%d",&n);memset(dp,0x3f,sizeof(dp));memset(pr,-1,sizeof(pr));dp[0][0]=0;for(int i=1,d;i<=n;i++){scanf("%d",&d);for(int j=0;j<=1000;j++){if(j-d>=0 && dp[i-1][j-d]!=inf && dp[i][j]>max(dp[i-1][j-d],j)){dp[i][j]=max(dp[i-1][j-d],j);pr[i][j]=j-d;}if(j+d<=1000 && dp[i-1][j+d]!=inf && dp[i][j]>dp[i-1][j+d]){dp[i][j]=dp[i-1][j+d];pr[i][j]=j+d;}}}if(dp[0]==inf)printf("IMPOSSIBLE");elsedfs(n,0);printf("\n");}return 0;}
相关文章推荐
- 基于Android中dp和px之间进行转换的实现代码
- Android中dip、dp、sp、pt和px的区别详解
- LFC1.0.0 版本发布
- Android px、dp、sp之间相互转换
- HP data protector软件学习1--基本角色与基本工作流程
- HP data protector软件学习2--软件组成与界面介绍
- android中像素单位dp、px、pt、sp的比较
- Android对px和dip进行尺寸转换的方法
- Android根据分辨率进行单位转换-(dp,sp转像素px)
- android 尺寸 dp,sp,px,dip,pt详解
- DP问题各种模型的状态转移方程
- POJ-1695-Magazine Delivery-dp
- nyoj-1216-整理图书-dp
- TYVJ1193 括号序列解题报告
- 对DP的一点感想
- TYVJ上一些DP的解题报告
- soj1005. Roll Playing Games
- 01背包问题
- LeetCode之Maximum Product Subarray
- DP Flow