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 10000000
int 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(动态规划)的更多相关文章

  1. HOJ 2124 &POJ 2663Tri Tiling(动态规划)

    Tri Tiling Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 9016 Accepted: 4684 Descriptio ...

  2. HOJ 2252 The Priest(动态规划)

    The Priest Source : 计算机学院第二届"光熙杯"程序设计大赛 Time limit : 3 sec Memory limit : 32 M Submitted : ...

  3. HOJ题目分类

    各种杂题,水题,模拟,包括简单数论. 1001 A+B 1002 A+B+C 1009 Fat Cat 1010 The Angle 1011 Unix ls 1012 Decoding Task 1 ...

  4. HOJ 2133&POJ 2964 Tourist(动态规划)

    Tourist Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 1503 Accepted: 617 Description A ...

  5. HOJ 13845 Atomic Computer有向无环图的动态规划

    考虑任意一个数字,任何一个都会有奇怪的..性质,就是一个可以保证不重复的方案——直接简单粗暴的最高位加数字..于是,如同上面的那个题:+1.-1.0 但是考虑到65536KB的标准内存限制,会得出一个 ...

  6. poj动态规划列表

    [1]POJ 动态规划题目列表 容易: 1018, 1050, 1083, 1088, 1125, 1143, 1157, 1163, 1178, 1179, 1189, 1208, 1276, 13 ...

  7. POJ 动态规划题目列表

    ]POJ 动态规划题目列表 容易: 1018, 1050, 1083, 1088, 1125, 1143, 1157, 1163, 1178, 1179, 1189, 1208, 1276, 1322 ...

  8. HOJ 1402 整数划分

    HOJ1402 整数划分 http://acm.hit.edu.cn/hoj/problem/view?id=1402 [题目描述] 整数划分是一个经典的问题.希望这道题会对你的组合数学的解题能力有所 ...

  9. 增强学习(三)----- MDP的动态规划解法

    上一篇我们已经说到了,增强学习的目的就是求解马尔可夫决策过程(MDP)的最优策略,使其在任意初始状态下,都能获得最大的Vπ值.(本文不考虑非马尔可夫环境和不完全可观测马尔可夫决策过程(POMDP)中的 ...

随机推荐

  1. fbset

    fbset用于读取和设置framebuffer的参数. # fbset mode "800x480-112" # D: 64.998 MHz, H: 58.034 kHz, V: ...

  2. 基于Linux的智能家居的设计(4)

    3  开发环境的搭建 本次课题使用的开发环境比較特殊.没有一个现成的集成开发环境,需要自己一步一步的搭建开发环境,开发环境的搭建的过程十分复杂,并且假设没有这个开发环境本次课题就无法进行. 因此.在进 ...

  3. 用json在java和C#之间传递base64的问题。。。

    记录下..唉.... java代码: 导入这个 commons-codec-1.8.jar (下载链接: http://files.cnblogs.com/files/gaocong/jar%E5%8 ...

  4. PHP删除目录及目录下所有文件或删除指定文件

    PHP删除目录及目录下所有文件或删除指定文件 <?php header("content-type:text/html;charset=utf-8"); /** * 删除目录 ...

  5. PHP中替换换行符方法总结

    <?php header("content-type:text/html;charset=utf-8"); $str = "aaaa bbbb cccc dddd& ...

  6. 简单html弹窗

    css: <style type="text/css"> .moneyrecord { display:none; border:0.5em solid #00AAEE ...

  7. [转]Git学习笔记与IntelliJ IDEA整合

    Git学习笔记与IntelliJ IDEA整合 一.Git学习笔记(基于Github) 1.安装和配置Git 下载地址:http://git-scm.com/downloads Git简要使用说明:h ...

  8. MFC中编辑框Edit Control添加“变量”后

  9. mybatis由浅入深day01_4.9删除用户_4.10更新用户

    4.9 删除用户 4.9.1 映射文件 4.9.2 代码: 控制台: 4.10 更新用户 4.10.1 映射文件 4.10.2 代码 控制台:

  10. MyException--org.apache.ibatis.exceptions.PersistenceException: ### Error building SqlSession. ###

    org.apache.ibatis.exceptions.PersistenceException:  ### Error building SqlSession. ### The error may ...