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. getaddrinfo ENOTFOUND https://api.weixin.qq.com https://api.weixin.qq.com:443

    原因:这是由于你当前的主机不能够连接到你填写的url. 解决方法: 先ping api.weixin.qq.com ping不通的话,就是外网访问的问题. 开通外网访问就可以了.

  2. MongoDB 常用shell命令汇总

    //指定用户名和密码连接到指定的MongoDB数据库 mongo 192.168.1.200:27017/admin -u user -p password use youDbName 1.Mongo ...

  3. 调用ffmpeg库编译时出现common.h:175:47: error: 'UINT64_C' was not declared in this scope

    解决办法 出现错误:jni/ffmpeg/libavutil/common.h:175:47: error: 'UINT64_C' was not declared in this scope 解决: ...

  4. MySQL无法重启问题解决Warning: World-writable config file ‘/etc/mysql/my.cnf’ is ignored

    今天在修改mysql数据库的配置文件,由于方便操作,就将“/etc/mysql/my.cnf” 的权限设置成 “777” 了,然后进行修改,当修改完进行重启mysql的时候,却报错,提示Warning ...

  5. linux源代码编译安装OpenCV

    为了尽可能保证OpenCV的特性,使用OpenCV源代码编译安装在linux上.先从安装其依赖项開始,以ubuntu 14.04.X为例解说在Linux上源代码编译安装OpenCV,其它linux版本 ...

  6. 重载 CreateParams 方法[2]: 重载 TForm.CreateParams 方法的几个例子

    这里有所有相关参数的解释: http://www.cnblogs.com/del/archive/2008/04/15/1154359.html //最大化窗口 procedure TForm1.Cr ...

  7. Unity资源解决方案之AssetBundle

    1.什么是AssetBundle AssetBundle是Unity pro提供的一种用来存储资源的文件格式,它可以存储任意一种Unity引擎能够识别的资源,如Scene.Mesh.Material. ...

  8. C# 多线程操作队列

    using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threa ...

  9. HDU 2594 Simpsons’ Hidden Talents (KMP)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2594 这题直接用KMP算法就能够做出来,只是我还尝试了用扩展的kmp,这题用扩展的KMP效率没那么高. ...

  10. YARN的设计

    YARN:下一代 Hadoop 计算平台 我们现在稍微改变一下用辞.以下名称的改动有助于更好地了解 YARN 的设计: ResourceManager 代替集群管理器 ApplicationMaste ...