文章
211
粉丝
0
获赞
999
访问
33.7k
#include <bits/stdc++.h>
using namespace std;
int main() {
int n;
while (cin >> n) {
vector<int> h(n);
for (int i = 0; i < n; i++)
cin >> h[i];
// tails[i] = 所有长度为 i+1 的递增子序列中,结尾元素的最小值
vector<int> tails;
for (int x : h) {
// 在 tails 中找第一个 >= x 的位置
// 如果找不到,返回 tails.end()
auto it = lower_bound(tails.begin(), tails.end(), x);
if (it == tails.end()) {
// x 比所有 tails 元素都大
// 可以形成更长的递增子序列
tails.push_back(x);
} else {
// *it >= x,用 x 替换 *it
// 这样同样长度的子序列有更小的结尾,更优
*it = x;
}
}
// tails 的长度 = 最长递增子序列的长度
cout << tails.size() << endl;
}
return 0;
}
登录后发布评论
关键结论:最少队伍数 = 最长递增子序列(LIS)的长度
原因:递增序列中的每个元素都必须放在不同队伍(因为递减队伍中不能有递增关系)。