题目描述
某国为了防御敌国的导弹袭击,发展出一种导弹拦截系统。但是这种导弹拦截系统有一个缺陷:虽然它的第一发炮弹能够到达任意的高度,但是以后每一发炮弹都不能高于前一发的高度。
假设某天雷达捕捉到敌国的导弹来袭。由于该系统还在试用阶段,所以只有一套系统,因此有可能不能拦截所有的导弹。
输入n个导弹依次飞来的高度(给出的高度数据是不大于30000的正整数),计算如果要拦截所有导弹最少要配备多少套这种导弹拦截系统。
比如:有8颗导弹,飞来的高度分别为
389 207 175 300 299 170 158 165
那么需要2个系统来拦截,他们能够拦截的导弹最优解分别是:
系统1:拦截 389 207 175 170 158
系统2:拦截 300 299 165
输入
两行,第一行表示飞来导弹的数量n(n<=1000)
第二行表示n颗依次飞来的导弹高度
输出
要拦截所有导弹最小配备的系统数k
样例输入
8
389 207 175 300 299 170 158 165
样例输出
2
代码如下
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main() {
int n;
cin >> n;
vector<int> heights(n);
for (int i = 0; i < n; ++i) {
cin >> heights[i];
}
vector<vector<int>> systems;
for (int height : heights) {
bool placed = false;
for (auto &system : systems) {
if (system.back() >= height) {
system.push_back(height);
placed = true;
break;
}
}
if (!placed) {
systems.push_back({height});
}
}
cout << systems.size();
return 0;
}