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. PHPOffice下PHPWord生成Word2007(docx)使用方法

    要正常使用,下载依赖包: PhpOffice/Common:https://github.com/PHPOffice/Common Zend/Escaper:https://github.com/ze ...

  2. 常用Javascript函数与原型功能收藏

    // 重复字符串 String.prototype.repeat = function(n) { return new Array(n+1).join(this); } // 替换全部 String. ...

  3. css制作上下左右的箭头

    <!doctype html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  4. Java线程之Callable和Future

    本篇说明的是Callable和Future,它俩很有意思的,一个产生结果,一个拿到结果.        Callable接口类似于Runnable,从名字就可以看出来了,但是Runnable不会返回结 ...

  5. WiFi(网络)调试Android手机

    手机需要root 使用adb tcpip命令开启网络调试功能,一旦手机重启,又要重复这些步骤,比较麻烦. 一劳永逸的方法是,使用re管理器(给予root权限)在手机的/system/build.pro ...

  6. write solid code 零散(原文)

    整理下目录,看了这个文件,幸好未删除. 以下是<write solid code>中的原文摘录. 1.How could I have prevented this bug? 2.How ...

  7. Effective C++ Item 33 Avoid hiding inherited names

    class Base { private: int x; public: ; virtual void mf2(); void mf3(); ... }; class Derived: public ...

  8. imx6ul开发板

    Feescale飞思卡尔于发布全新的基于ARM Cortex-A7核心的低功耗处理器i.MX6UL,主要面向车载信息处理.家庭能源管理系统.工控领域.物联网网关等应用.具有可扩展性.高性能和低功耗特性 ...

  9. php面向对象的简单总结 $this $parent self

    面向对象涉及到的比较多,大概总结整理一下php的属性.对象,以及访问方式$this  $parent  self  的使用场景. 1. PHP类属性定义和访问方式: 1 <?php 2 clas ...

  10. java基础---->java中国际化的实现

    应用程序的功能和代码设计考虑在不同地区运行的需要,其代码简化了不同本地版本的生产.开发这样的程序的过程,就称为国际化.今天,我们就开始学习java中国际化的代码实现. Java国际化主要通过如下3个类 ...