题目
找最大上升子序列 题目数列长度为1000
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1087
代码
很容易想到On^2的算法
#include<cstdio>
#include<cstring>
#include<cmath>
#include<cstdlib>
#include<cctype>
#include<ctime>
#include<iostream>
#include<string>
#include<map>
#include<queue>
#include<stack>
#include<set>
#include<vector>
#include<iomanip>
#include<list>
#include<bitset>
#include<sstream>
#include<fstream>
#include<complex>
#include<algorithm>
#if __cplusplus >= 201103L
#include <unordered_map>
#include <unordered_set>
#endif
#define ll long long
using namespace std;
const int INF = 0x3f3f3f3f;
int a[1010],dp[1010];
int main(){
ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
int n;
while(cin>>n){
if(n==0) break;
for(int i=1;i<=n;i++) cin>>a[i],dp[i]=a[i];
int maxx=0;
for(int i=1;i<=n;i++){
int res=0;
for(int j=i-1;j>=1;j--){
if(a[i]>a[j]) res=max(res,dp[j]);
}
dp[i]+=res;
maxx=max(maxx,dp[i]);
}
cout<<maxx<<endl;
}
return 0;
}
当然也可以用树状数组做 不过要处理一下相同的数