tj
2025-07-27 20:07:10
发布于:浙江
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main() {
vector<int> heights;
int height;
while (cin >> height) {
heights.push_back(height);
}
int n = heights.size();
vector<int> dp(n, 1);
vector<int> prev(n, -1);
for (int i = 0; i < n; ++i) {
for (int j = 0; j < i; ++j) {
if (heights[j] >= heights[i] && dp[j] + 1 > dp[i]) {
dp[i] = dp[j] + 1;
prev[i] = j;
}
}
}
int maxLength = 0;
int lastIndex = 0;
for (int i = 0; i < n; ++i) {
if (dp[i] > maxLength) {
maxLength = dp[i];
lastIndex = i;
}
}
vector<int> interceptedMissiles;
while (lastIndex != -1) {
interceptedMissiles.push_back(heights[lastIndex]);
lastIndex = prev[lastIndex];
}
reverse(interceptedMissiles.begin(), interceptedMissiles.end());
cout << maxLength << endl;
for (int i = 0; i < interceptedMissiles.size(); ++i) {
if (i != 0) {
cout << " ";
}
cout << interceptedMissiles[i];
}
cout << endl;
return 0;
}
这里空空如也
有帮助,赞一个