最大上升子序列和
题目描述 :
一个数的序列bi,当b1<b2<…<bs的时候,我们称这个序列是上升的。
对于给定的一个序列(a1,a2,…,aN),我们可以得到一些上升的子序列(ai1,ai2,…,aik),其中1≤i1<i2<…<ik≤N。
比如,对于序列(1,7,3,5,9,4,8),有它的一些上升子序列,如(1,7),(3,4,8) 等等。 这些子序列中序列和最大为18,为子序列(1,3,5,9)的和。
对于给定的序列,求出最大上升子序列和。注意,最长的上 升子序列的和不一定是最大的,比如序列(100,1,2,3)的最大上升子序列和为100,而最长上升子序列为(1,2,3)。
输入 :
输入的第一行是序列的长度N(N≤1000)。
第二行给出序列中的N个整数,这些整数的取值范围都在0到10000(可能重复)。
输出 :
一个整数,为最大上升子序列和。
样例输入:
7
1 7 3 5 9 4 8
样例输出:
18
典型DP题,核心的方程式是dp[i]=max(dp[i],dp[j]+a[i]),其中,i先从前往后扫,j从第一个往i扫。
#pragma GCC optimize(3,"Ofast","inline")
#pragma G++ optimize(3)
#include <bits/stdc++.h>
#include <iostream>
#include <cstdio>
#include <fstream>
#include <algorithm>
#include <cmath>
#include <deque>
#include <vector>
#include <queue>
#include <string>
#include <cstring>
#include <map>
#include <stack>
#include <set>
#include <sstream>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll,ll> pll;
typedef pair<int,int> pii;
typedef queue<int> q_i;
typedef queue<string> q_s;
typedef queue<double> q_d;
typedef queue<ll> q_ll;
typedef queue<char> q_c;
typedef priority_queue<int> pq_i;
typedef priority_queue<string> pq_s;
typedef priority_queue<double> pq_d;
typedef priority_queue<ll> pq_ll;
typedef stack<int> s_i;
typedef stack<string> s_s;
typedef stack<double> s_d;
typedef stack<ll> s_ll;
typedef stack<char> s_c;
typedef map<ll,ll> m_ll_ll;
typedef map<int,ll> m_i_ll;
typedef map<string,ll> m_s_ll;
typedef map<char,int> m_c_i;
typedef map<char,ll> m_c_ll;
#define rep(i,l,r) for(int i=l;i<=r;i++)
#define per(i,l,r) for(int i=r;i>=l;i--)
#define eif else if
#define N 105
#define mm(dp) memset(dp,0,sizeof(dp))
#define mm1(dp) memset(dp,-1,sizeof(dp))
#define mm2(dp) memset(dp,0x3f,sizeof(dp))
const double e=2.71828182845;
const double pi = acos(-1.0);
void read(int &x)
{
char ch=getchar();
x=0;
for(; ch<'0'||ch>'9'; ch=getchar());
for(; ch>='0'&&ch<='9'; x=x*10+ch-'0',ch=getchar());
}
inline void write(int x)
{
if(x<0)
putchar('-'),x=-x;
if(x>9)
write(x/10);
putchar(x%10+'0');
printf("\n");
}
float SqrtByCarmack( float number )
{
int i;
float x2, y;
const float threehalfs = 1.5F;
x2 = number * 0.5F;
y = number;
i = * ( int * ) &y;
i = 0x5f375a86 - ( i >> 1 );
y = * ( float * ) &i;
y = y * ( threehalfs - ( x2 * y * y ) );
y = y * ( threehalfs - ( x2 * y * y ) );
y = y * ( threehalfs - ( x2 * y * y ) );
return number*y;
}
ll qpow(ll a,ll b,ll mod)
{
ll sum=1;
while(b)
{
if(b%2==1)
{
sum=sum*a%mod;
}
b/=2;
a=a*a%mod;
}
return sum;
}
int erfen(int *a,int start,int endd,int l)//小于等于l的最大值的角标
{
int mid=(start+endd)/2;
if((a[mid]<=l&&a[mid+1]>l)||(mid==endd&&a[mid]<=l))
return mid;
else if(a[mid]<=l)
return erfen(a,mid+1,endd,l);
else if(a[mid]>l)
{
if(start!=mid)
return erfen(a,start,mid,l);
else
return start-1;
}
}
ll prime[6] = {2, 3, 5, 233, 331};
ll qmul(ll x, ll y, ll mod)
{
return (x * y - (long long)(x / (long double)mod * y + 1e-3) *mod + mod) % mod;
}
bool Miller_Rabin(ll p)
{
if(p < 2)
return 0;
if(p != 2 && p % 2 == 0)
return 0;
ll s = p - 1;
while(! (s & 1))
s >>= 1;
for(int i = 0; i < 5; ++i)
{
if(p == prime[i])
return 1;
ll t = s, m = qpow(prime[i], s, p);
while(t != p - 1 && m != 1 && m != p - 1)
{
m = qmul(m, m, p);
t <<= 1;
}
if(m != p - 1 && !(t & 1))
return 0;
}
return 1;
}
ll gcd(ll x,ll y)
{
if(y==0)
return x;
else
return gcd(y,x%y);
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int n;
cin>>n;
int a[n+1];
int dp[n+1];
rep(i,1,n)
{
cin>>a[i];
dp[i]=a[i];
}
rep(i,1,n)
{
rep(j,1,i-1)
{
if(a[j]<a[i])
{
dp[i]=max(dp[i],dp[j]+a[i]);
}
}
}
int maxn=-1;
rep(i,1,n)
{
maxn=max(maxn,dp[i]);
}
cout<<maxn;
return 0;
}