hdu 1087 Super Jumping! Jumping! Jumping! (动态规划)

问题描述:


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 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.


输出:


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

算法分析:

其实这道题和最长递增子序列问题很相似,思路其实是一样的,但是还是要转换下思想,这里不要求输出元素或者元素下标,

其实是简单了很多,用一个数组来记录各个行的和即可。



代码:


#include<iostream>


using namespace std;


int a[1001];
int x[1001];
int lis(int a[],int n)
{
int i,j,max;
x[0]=a[0];
max=a[0];
for(i=1;i<n;i++)
{
x[i]=a[i];
for(j=0;j<i;j++)
{
if(a[j]<a[i] && x[i]<x[j]+a[i])
x[i]=x[j]+a[i];
}
}
for(i=1;i<n;i++)
{
if(max<x[i]) max=x[i];
}
return max;
}


int main()
{
int n,i;
while(cin>>n && n!=0)
{
for(i=0;i<n;i++)
{
cin>>a[i];
}
cout<<lis(a,n)<<endl;
}
}


对于这道题我附加一个代码,也可以实现本题要求,但却繁琐很多,是完全顺着最大递增子序列的思路编写的,由此可以提醒大家,还是要视具体情况而定

/*#include<iostream>


using namespace std;


int x[1000][1000];//用来存放最大递增序列的具体元素
int a[1000];
int l[1000];//用来存放最大递增序列的长度
int lis(int a[],int n)
{
    int i,j,k,index,max;
    
    for(i=0;i<n;i++)
    {
        l[i]=1;
        x[i][0]=a[i];//初始化单个的最大递增序列长度为1 
        //而且二维数组每行用来存放最大递增序列元素
     }
    for(i=1;i<n;i++)
    {
        for(j=i-1;j>=0;j--)
        {
            if(a[j]<a[i] && l[i]<l[j]+1)//满足条件时长度要加1同时要把该行的元素放到i行
            {
                l[i]=l[j]+1;
                max=l[i];
                for(k=0;k<max-1;k++)
                    x[i][k]=x[j][k];
                x[i][max-1]=a[i];
            }
        }
    }
     for(index=0,i=1;i<n;i++)
   {
       if(l[index]<l[i])  index=i; //找出最大长度
    }
    for(i=0;i<n;i++)
    {
        if(l[i]==l[index])
        {
            for(j=1;j<l[index];j++)
                 x[i][0]+=x[i][j];//可能最大长度的有很多,所以要把该行的元素都
                 //相加放到第一个元素里,方便比较       
        
        }
    }


    for(i=1;i<n;i++)
    {
        if(x[0][0]<x[i][0]) x[0][0]=x[i][0];
    }
    return x[0][0];
}
int main()
{
    int n,i,flag;
        
    while(cin>>n && n!=0)
    {       
 flag=0;
          for(i=0;i<n;i++)
               cin>>a[i];
               if(flag!=0) cout<<endl;
               cout<<lis(a,n);
               flag=1;
    }
    return 0;
}*/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值