main
黄海 1 year ago
parent 0f1816c10a
commit 0cf7a8abf4

@ -1,37 +1,40 @@
#include <sstream>
#include <iostream>
#include <algorithm>
#include <bits/stdc++.h>
using namespace std;
const int N = 1010;
int n;
int h[N], f[N], q[N];
int h[N], f[N];
int q[N], ql; // 需要ql套拦截系统每个拦截系统最后一个拦截高度为q[i]
int res;
int main() {
// 用整行字符串+流的办法,读入多个带空格的数字
string line;
getline(cin, line);
stringstream ssin(line);
while (ssin >> h[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 (h[i] <= h[j])
if (h[i] <= h[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 < cnt && q[k] < h[i]) k++;
if (k == cnt)
q[cnt++] = h[i];
while (k < ql && q[k] < h[i]) k++;
if (k == ql) // 如果没有找到可以接在后面的导弹拦截系统,那么需要创建一套新的拦截系统
q[ql++] = h[i];
else
q[k] = h[i];
q[k] = h[i]; // 否则就直接接到找到的第k套拦截系统的后面,那么第k套拦截系统的最后一个拦截高度=q[k]=h[i]
}
printf("%d\n", res);
printf("%d\n", cnt);
// 输出最长不升序列长度,即:最多能拦截的导弹数
printf("%d\n%d\n", res, ql);
return 0;
}
Loading…
Cancel
Save