zoukankan      html  css  js  c++  java
  • HDU1087 Super Jumping! Jumping! Jumping! 最长上升子序列

    Super Jumping! Jumping! Jumping!

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
    Total Submission(s): 9547    Accepted Submission(s): 3918


    Problem Description
    Nowadays, a kind of chess game called “Super Jumping! Jumping! Jumping!” is very popular in HDU. Maybe you are a good boy, and know little about this game, so I introduce it to you now.



    The game can be played by two or more than two players. It consists of a chessboard(棋盘)and some chessmen(棋子), and all chessmen are marked by a positive integer or “start” or “end”. The player starts from start-point and must jumps into end-point finally. In the course of jumping, the player will visit the chessmen in the path, but everyone must jumps from one chessman to another absolutely bigger (you can assume start-point is a minimum and end-point is a maximum.). And all players cannot go backwards. One jumping can go from a chessman to next, also can go across many chessmen, and even you can straightly get to end-point from start-point. Of course you get zero point in this situation. A player is a winner if and only if he can get a bigger score according to his jumping solution. Note that your score comes from the sum of value on the chessmen in you jumping path.
    Your task is to output the maximum value according to the given chessmen list.
     


    Input
    Input contains multiple test cases. Each test case is described in a line as follow:
    N value_1 value_2 …value_N 
    It is guarantied that N is not more than 1000 and all value_i are in the range of 32-int.
    A test case starting with 0 terminates the input and this test case is not to be processed.
     


    Output
    For each case, print the maximum according to rules, and one line one case.
     


    Sample Input
    3 1 3 2 4 1 2 3 4 4 3 3 2 1 0
     


    Sample Output
    4 10 3
     
      该题可以算是一道经典的DP题了,题中数据是这样的。以 3 1 3 2 为例,首先 3 代表有三个数, 后面给出三个数,求该串的一个子串,使得其值一直是递增的,而且要求输出最大的和值。可以论证,该子串一定会是最长上升子串,因为,如果一个串还能够插入一个元素的话,那么这个串就一定不是最大的和了。而这个最长的上升子串还满足是所有同样长度的子串中最优的,和值最大。
      具体实现过程这样来,首先记录下N个数  rec[] 数组,对于每个数开辟一个 dp[] 数组,用来记录到达该数值时的最大和值,dp[]数组很重要,因为它能够对解决后面的问题提供信息,记录局部的最有解。 动态规划方程是  dp[i] = Max( dp[i] , dp[j] + rec[i] ) , 这个方程的前提是,j < i  &&  rec[j] < rec[i], 因为题目要求是从前往后跳,且只能跳到较大的数字上面去。最后再遍历一次,找到最大的值即可。
      代码如下:
     1 #include <cstdlib>
    2 #include <cstring>
    3 #include <cstdio>
    4 using namespace std;
    5
    6 int rec[1005], dp[1005];
    7
    8 inline int Max( int x, int y )
    9 {
    10 return x > y ? x : y;
    11 }
    12
    13 int main()
    14 {
    15 int N;
    16 while( scanf( "%d", &N ), N )
    17 {
    18 memset( dp, 0, sizeof( dp ) );
    19 for( int i = 0; i < N; ++i )
    20 {
    21 scanf( "%d", &rec[i] );
    22 dp[i] = rec[i];
    23 }
    24 for( int i = 0; i < N; ++i )
    25 {
    26 for( int j = 0; j <= i; ++j )
    27 {
    28 if( rec[i] > rec[j] )
    29 dp[i] = Max( dp[i], dp[j] + rec[i] );
    30 }
    31 }
    32 int max = -0x7fffffff;
    33 for( int i = 0; i < N; ++i )
    34 {
    35 max = max > dp[i] ? max : dp[i];
    36 }
    37 printf( "%d\n", max );
    38 }
    39 return 0;
    40 }

      

  • 相关阅读:
    mybatis之关联关系映射
    spa项目开发之tab页实现
    mybatis整合redis实现二级缓存
    mybatis整合spring
    mybatis动态sql和分页
    Mybatis入门
    使用java代码操作redis
    Redis安装
    IDEA的安装和使用
    Linux入门——安装jdk、tomcat、MySQL以及项目部署
  • 原文地址:https://www.cnblogs.com/Lyush/p/2161314.html
Copyright © 2011-2022 走看看