POJ - 2533 Longest Ordered Subsequence
https://vjudge.net/problem/POJ-2533
DP
#include <iostream>
#include <algorithm>
using namespace std;
const int N = 1010;
int n;
int a[N], f[N]; //
int main()
{
scanf("%d", &n);
for (int i = 1; i <= n; i ++ ) scanf("%d", &a[i]);
for (int i = 1; i <= n; i ++ )
{
f[i] = 1; // 初始以a[i]结尾的子序列 只有a[i]一个数
for (int j = 1; j < i; j ++ )
if (a[j] < a[i])
f[i] = max(f[i], f[j] + 1); //以j结尾的子序列长度
}
int res = 0;
for (int i = 1; i <= n; i ++ ) res = max(res, f[i]);
printf("%d\n", res);
return 0;
}
朴素法
#include<iostream>
#include<algorithm>
#include<cstring>
#define open ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
using namespace std;
const int N=1010;
int n,len;
int d[N],h[N];
int main()
{
open;
while(cin>>n)
{
for(int i=1;i<=n;i++) cin>>h[i];
len=1;
d[1]=h[1];
for(int i=2;i<=n;i++)
if(h[i]>d[len])
d[++len]=h[i];
else
{
int j=lower_bound(d+1,d+len+1,h[i])-d;
d[j]=h[i];
}
cout<<len<<endl;
}
return 0;
}
本文来自博客园,作者:斯文~,转载请注明原文链接:https://www.cnblogs.com/zhiweb/p/15483289.html

浙公网安备 33010602011771号