HDU--1087 Sum the longest increasing subsequence (DP)

Address: http://acm.hdu.edu.cn/showproblem.php?pid=1087

 

    Question meaning: This example is very misleading. The meaning of this question is to jump from the beginning to the end, keep increasing the jump and score the most. Note that the start and end points are not scored;

    Analysis: Just change the template, the length of the sequence was previously sought. Sum up here, then as long as dp is initialized a [], still discuss whether each number is selected or not. Transfer equation:

  if(a[i]>a[j])
  dp[ i ]=max(dp[ i ],dp[ j ]+a[ i ])
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
const int maxn = 1e6+10;
typedef long long ll;
ll a[maxn];
ll dp[maxn];
int main()
{
    ll n ;
    while(cin>>n)
    {
        if(n==0)    
            break;
        for(int i =0  ; i<n;i++)
            cin>>a[i];
        for(int i=0;i<n;i++)
            dp[i]=a[i];
        ll maxx=-1;
        ll sum = 0;
        for(int i = 1 ;i <n ; i++)
        {
            for(int j = 0 ; j< i; j++)
            {
                if(a[i]>a[j])
                {
                    dp[i]=max(dp[i],dp[j]+a[i]);
                }
            }
            maxx=max(dp[i],maxx);
        }
        cout<<maxx<<endl;
    }
}

 

Guess you like

Origin www.cnblogs.com/liyexin/p/12683114.html