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

水题,可是我却因为dp数组的初始化造成了多遍wa,这题就是求上升序列的最大和。

转移方程:

首先要对dp初始化。

if(w[i]>w[j]) dp[i]=dp[j]+w[i];i>j

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <queue>
#include <math.h>
#define inf 0x3f3f3f3f
using namespace std;
int n,w[10100],dp[10100];
int main()
{
    int maxx;
    while(scanf("%d",&n)!=EOF&&n!=0)
    {
        memset(dp,0,sizeof(dp));
        for(int i=0; i<n; i++)
        {
            scanf("%d",&w[i]);
            dp[i]=w[i];//边界问题要好好考虑,输入3 5 3 4
        }
        dp[0]=w[0];
        maxx=w[0];
        for(int i=1;i<n;i++)
        {
            for(int j=0;j<i;j++)
            {
                if(w[i]>w[j])
                    dp[i]=max(dp[i],dp[j]+w[i]);
            }
            maxx=max(maxx,dp[i]);
        }
        printf("%d\n",maxx);
    }
    return 0;
}

 

相关文章:

  • 2022-12-23
  • 2021-05-23
  • 2021-07-31
  • 2021-11-07
  • 2022-12-23
  • 2021-10-11
猜你喜欢
  • 2022-12-23
  • 2022-01-04
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案