Files
python/TangDou/AcWing/LIS/1010_TanXin.cpp
HuangHai 1f397eca87 'commit'
2025-08-30 18:35:01 +08:00

31 lines
1.1 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#include <bits/stdc++.h>
using namespace std;
const int N = 1010;
int a[N], f[N]; // 每个导弹的高度,f[i]:以a[i]结尾的最长不升子序列长度
int q[N], ql; // 需要ql套拦截系统,每个拦截系统最后一个拦截高度为q[i]
int n, res;
int main() {
while (cin >> a[n]) n++;
// 第一问
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]);
}
// 第二问
for (int i = 0; i < n; i++) {
int p = lower_bound(q, q + ql, a[i]) - q;
if (p == ql) // 如果没有找到可以接在后面的导弹拦截系统,那么需要创建一套新的拦截系统
q[ql++] = a[i];
else
q[p] = a[i]; // 否则就直接接到找到的第k套拦截系统的后面,那么,第k套拦截系统的最后一个拦截高度=q[k]=h[i]
}
// 输出最长不升序列长度,即:最多能拦截的导弹数
printf("%d\n%d\n", res, ql);
return 0;
}