1 条题解
-
0
【本题解由站长编写】
一套系统最多拦截的数量是最长不上升子序列长度;所有导弹所需的最少系统数等于最长严格上升子序列长度。第一问把高度取负,使用 upper_bound 允许相等高度延长;第二问使用 lower_bound 保持严格上升。两问均为 O(n log n),避免题面未规定数量上界时 O(n²) 的性能问题。
代码(C++17)
#include <bits/stdc++.h> using namespace std; int main() { vector<long long> h; long long x; while (cin >> x) h.push_back(x); int n = h.size(); vector<long long> decreasing, increasing; for (long long height : h) { // Equal heights extend an interception sequence, but not a strict LIS. long long negative = -height; auto a = upper_bound(decreasing.begin(), decreasing.end(), negative); if (a == decreasing.end()) decreasing.push_back(negative); else *a = negative; auto b = lower_bound(increasing.begin(), increasing.end(), height); if (b == increasing.end()) increasing.push_back(height); else *b = height; } cout << decreasing.size() << "\n" << increasing.size() << endl; return 0; }
- 1
信息
- ID
- 4272
- 时间
- 1000ms
- 内存
- 256MiB
- 难度
- 5
- 标签
- 递交数
- 0
- 已通过
- 0
- 上传者