给n个数,求出最长非下降序列。
Input
第一行一个整数n(1≤n≤105)
第二行n个整数ai(1≤ai≤109)
Output
输出一个整数表示最长非下降序列的长度。
Sample Input
Raw
5
1 2 3 4 5
5
1 1 1 1 1
Sample Output
Raw
5
5
#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<vector>
#include<math.h>
#include<string.h>
using namespace std;
const int maxn=1e5+5;
#define me(x) memset(x,0,sizeof(x));
int dp[maxn]={0};
int a[maxn];
int b[maxn];
int main()
{
int n;
while(cin>>n)
{
me(dp);
for (int i = 1; i <= n; i++)
cin >> a[i];
int cnt = 1;
b[1] = a[1];
for (int i = 2; i <= n; i++)
{
if (a[i] >= b[cnt])
b[++cnt] = a[i];
else {
int j = upper_bound(b + 1, b + 1 + cnt, a[i]) - b;
b[j] = a[i];
}
}
cout << cnt << endl;
}
return 0;
}
本文介绍了一种求解最长非下降子序列的高效算法,输入为一系列整数,输出为该序列中最大非下降子序列的长度。通过动态规划思想实现,特别适合于处理大规模数据集。
679

被折叠的 条评论
为什么被折叠?



