You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

31 lines
1.1 KiB

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