题解:ABC468 - F
题面:Chmax。
解题思路
题意转化:从 的一个排列中计算出两个不相交的 LIS 的最长总长度,且如果可行,那么需要占用每一个可占用的位置。
先考虑一种情况:即其中一个固定,另一个做 LIS。此时为前缀最大值作为第一个 LIS 序列 (令其长度为 ),剩下的数的 LIS 作为第二个 LIS 序列 (令其长度为 ),答案为 。
这样想的原因
由于每个元素可以的话都必须属于两个序列 (固定)或 (做 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;
}