[AcWing 1010] 拦截导弹

image


点击查看代码
#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;
} 

  1. 贪心思路(可用调整法证明)
    从前往后扫描每个数,对于每个数:
    ① 如果现有的子序列的结尾都小于当前数,则创建新的子序列
    ② 将当前数放到结尾大于等于它的最小的子序列后面
posted @ 2022-06-17 21:32  wKingYu  阅读(36)  评论(0)    收藏  举报