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.

37 lines
1.5 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

#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 res;
int main() {
// 用整行字符串+流的办法,读入多个带空格的数字
int n;
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++) {
// 用贪心的思想来模拟创建新的导弹系统或者加入一个比h[i]大的导弹系统,并且,尾巴值最小的那套导弹系统中去
// q[cnt] : 现在共cnt套导弹系统q[i]记录的是每套导弹系统中最后一个拦截的高度
int k = 0;
while (k < ql && q[k] < a[i]) k++;
if (k == ql) // 如果没有找到可以接在后面的导弹拦截系统,那么需要创建一套新的拦截系统
q[ql++] = a[i];
else
q[k] = a[i]; // 否则就直接接到找到的第k套拦截系统的后面,那么第k套拦截系统的最后一个拦截高度=q[k]=h[i]
}
// 输出最长不升序列长度,即:最多能拦截的导弹数
printf("%d\n%d\n", res, ql);
return 0;
}