题解:【ABC468】F - Chmax
题解:ABC468 - F
题面:Chmax。
解题思路
题意转化:从
先考虑一种情况:即其中一个固定,另一个做 LIS。此时为前缀最大值作为第一个 LIS 序列
这样想的原因
由于每个元素可以的话都必须属于两个序列
证明:该值为答案
反证法:(
在
因此,
细节
注意 LIS 只能用二分,复杂度
代码实现
#include <iostream>
#include <vector>
#include <algorithm>
#include <climits>
#include <atcoder/all>
#define endl '\n'
using namespace std;
typedef long long ll;
int main(int argc, char* argv[]) {
// freopen(".in", "r", stdin);
// freopen(".out", "w", stdout);
ios_base::sync_with_stdio(false);
cin.tie(nullptr); cout.tie(nullptr);
int n; cin >> n;
vector<int> a;
int maxx = 0, res = 0; // 维护固定序列 P
for (int i = 0; i < n; i++) {
int p; cin >> p;
if (maxx < p) {
maxx = p;
res++;
} else {
a.push_back(p); // 剩下的元素加到 a 中
}
}
vector<int> t; // a 的 LIS(序列 Q)
// 二分求 LIS
for (auto& x : a) {
auto iter = lower_bound(t.begin(), t.end(), x);
if (iter == t.end()) {
t.push_back(x);
} else {
*iter = x;
}
}
int ans = res + t.size();
cout << ans << endl;
return 0;
}