#include <iostream>
#include <vector>
#include <algorithm>
int main() {
int n;
std::cin >> n;
std::vector<int> missiles(n);
for (int i = 0; i < n; ++i) {
std::cin >> missiles[i];
}
std::sort(missiles.begin(), missiles.end(), std::greater<int>());
int systems = 0;
std::vector<int> currentSystem;
std::vector<std::vector<int>> allSystems;
for (int height : missiles) {
if (currentSystem.empty() || height > currentSystem.back()) {
currentSystem.push_back(height);
} else {
allSystems.push_back(currentSystem);
currentSystem.clear();
currentSystem.push_back(height);
}
}
allSystems.push_back(currentSystem);
systems = allSystems.size();
std::cout << systems << std::endl;
for (int i = 0; i < systems; ++i) {
std::cout << i + 1 << ":";
for (int height : allSystems[i]) {
std::cout << " " << height;
}
std::cout << std::endl;
}
return 0;
}
1373: 【提高】拦截导弹方案求解
内存限制:128 MB
时间限制:1.000 S
评测方式:文本比较
命题人:外部导入
提交:24
解决:6
题目描述
某国为了防御敌国的导弹袭击,发展出一种导弹拦截系统。但是这种导弹拦截系统有一个缺陷:
虽然它的第一发炮弹能够到达任意的高度,但是以后每一发炮弹都不能高于前一发的高度。
某天,雷达捕捉到敌国的导弹来袭。由于该系统还在试用阶段,所以只有一套系统,因此有可能不能拦截所有的导弹。
输入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
接下来k行分别输出每套系统拦截哪些高度的导弹
样例输入 复制
8
389 207 175 300 299 170 158 165
样例输出 复制
2
1:389 207 175 170 158
2:300 299 165
为什么答案错误?