
点击查看代码
#include<iostream>
using namespace std;
const int N = 1010;
int n;
int a[N], q[N], f[N];
int main()
{
while (cin >> a[n]) n ++;
int res = 0, cnt = 0;
for (int i = 0; i < n; i ++) {
f[i] = 1;
for (int j = 0; j < i; j ++)
if (a[i] <= a[j])
f[i] = max(f[i], f[j] + 1);
res = max(res, f[i]);
int t = 0;
while (t < cnt && q[t] < a[i]) t ++;
if (t == cnt) q[cnt ++] = a[i];
else q[t] = a[i];
}
cout << res << endl;
cout << cnt << endl;
return 0;
}
- 贪心思路(可用调整法证明)
从前往后扫描每个数,对于每个数:
① 如果现有的子序列的结尾都小于当前数,则创建新的子序列
② 将当前数放到结尾大于等于它的最小的子序列后面