5 条题解
-
0
满分解法(100 分)
固定端点时能选择什么
设所选子序列的两个端点位于 ,两个端点值中的较小值为 。区间 内所有小于 的元素都可以加入子序列:它们保持原来的相对顺序,并且同时小于两个端点。反过来,值不小于 的元素不能作为中间元素。
所以,端点固定后,最优方案一定选择区间内全部小于 的元素。问题变成:对每个可能的阈值 ,找一对值都不小于 的端点,使它们之间的小值元素尽量多。
固定阈值时选择最外侧端点
固定 ,把排列中值不小于 的位置称为高值位置。设其中最左、最右的位置为 。只要 ,这两个位置就可以作为端点。
任取另一对高值位置 ,都有 。区间 内所有值小于 的元素,也都在 内。因此,以 为端点至少能选择同样多的小值元素,不会比 更差。于是固定阈值时只需考虑这对最外侧端点。
由于 是 的排列,值不小于 的元素恰有 个,而且它们全部位于 中。区间共有 个元素,其中两个极端位置作为端点,其余高值元素不能加入,小于 的元素全部加入。候选长度为
这个候选方案确实合法,所以公式给出了可达到的长度;前面的最外侧端点论证又说明,任何端点较小值不小于 的方案都不会更长。枚举所有 后,便覆盖了任意最优方案对应的端点阈值。
递减枚举阈值
预先记录每个值出现的位置 。让 从 递减到 :从阈值 变到 时,高值位置集合只新增 ,因此用两个变量维护所有已加入位置的最小值和最大值即可。
开始时答案为 ,这也处理了 。每次加入新位置后,若最左、最右位置不同,就用上式更新答案。阈值递减到 后,所有可能的端点较小值都已经被考虑。
时间复杂度为 ,空间复杂度为 。
AC 代码(C++20)
#include <bits/stdc++.h> using namespace std; using i64 = long long; void solve() { int n; cin >> n; vector<int> pos(n + 1); for (int i = 0; i < n; ++i) { int x; cin >> x; pos[x] = i; } int ans = 1; int left = n, right = -1; for (int x = n; x >= 1; --x) { left = min(left, pos[x]); right = max(right, pos[x]); if (left < right) { ans = max(ans, right - left - n + x + 2); } } cout << ans << '\n'; } int main() { cin.tie(0)->sync_with_stdio(0); solve(); } -
0
有序集合维护位置(通过子任务 1–4,共 70 分)
仍然使用阈值 的做法:值不小于 的最左、最右位置记为 ,则候选长度为 。固定阈值时选择这两个最外侧位置最优,因为它们包含了任意其他高值端点之间的所有小值元素。
先记录每个值的位置,再让 从 递减到 。每一步把 的位置加入有序集合;集合首尾元素就是当前的 。每个位置只插入一次,查询首尾位置为常数时间。
时间复杂度为 ,空间复杂度为 ,可以通过 的子任务。
参考代码(C++20)
#include <bits/stdc++.h> using namespace std; using i64 = long long; void solve() { int n; cin >> n; vector<int> pos(n + 1); for (int i = 0; i < n; ++i) { int x; cin >> x; pos[x] = i; } int ans = 1; set<int> active; for (int x = n; x >= 1; --x) { active.insert(pos[x]); if (active.size() >= 2) { int left = *active.begin(); int right = *active.rbegin(); ans = max(ans, right - left - n + x + 2); } } cout << ans << '\n'; } int main() { cin.tie(0)->sync_with_stdio(0); solve(); } -
0
枚举阈值并扫描(通过子任务 1–3,共 50 分)
枚举阈值 。把所有值不小于 的位置找出来,并记最左、最右位置为 。固定阈值时,取这两个位置作端点不会更差:任意其他两个高值位置之间的小于 的元素,也都包含在 中。
排列中值不小于 的元素恰有 个,并且全在 内。若 ,选择两个端点以及区间内全部小于 的元素,长度为
对每个 扫描整个排列以求 ,再取所有候选的最大值。枚举了端点较小值可能对应的全部阈值,因此不会漏掉最优解。
时间复杂度为 ,空间复杂度为 ,可以通过 的子任务。
参考代码(C++20)
#include <bits/stdc++.h> using namespace std; using i64 = long long; void solve() { int n; cin >> n; vector<int> p(n); for (int &x : p) cin >> x; int ans = 1; for (int x = 1; x <= n; ++x) { int left = n, right = -1; for (int i = 0; i < n; ++i) { if (p[i] >= x) { left = min(left, i); right = max(right, i); } } if (left < right) ans = max(ans, right - left - n + x + 2); } cout << ans << '\n'; } int main() { cin.tie(0)->sync_with_stdio(0); solve(); } -
0
枚举两个端点(通过子任务 1–2,共 30 分)
枚举子序列两个端点的位置 。端点确定后,中间每个同时小于 的元素都可以加入,而且加入它不会影响其他元素;不满足条件的元素则一定不能加入。因此,这对端点能得到的最长长度为
再扫描一遍 内的所有位置即可算出这个值。枚举覆盖所有可能端点,所以所有候选的最大值就是全局答案; 时单个元素本身构成合法子序列。
时间复杂度为 ,空间复杂度为 ,可以通过 的子任务。
参考代码(C++20)
#include <bits/stdc++.h> using namespace std; using i64 = long long; void solve() { int n; cin >> n; vector<int> p(n); for (int &x : p) cin >> x; int ans = 1; for (int left = 0; left < n; ++left) { for (int right = left + 1; right < n; ++right) { int limit = min(p[left], p[right]); int len = 2; for (int i = left + 1; i < right; ++i) { len += p[i] < limit; } ans = max(ans, len); } } cout << ans << '\n'; } int main() { cin.tie(0)->sync_with_stdio(0); solve(); } -
0
枚举所有子序列(通过子任务 1,共 10 分)
在 时,可以枚举每个位置选或不选,从而得到全部 个子序列。
对于一个已枚举的子序列,长度不超过 时一定合法;否则逐个检查其中间元素是否同时小于首、尾元素。所有通过检查的子序列长度的最大值就是答案,因为枚举覆盖了每一种选择,检查条件也与题意完全相同。
时间复杂度为 ,空间复杂度为 。
参考代码(C++20)
#include <bits/stdc++.h> using namespace std; using i64 = long long; void solve() { int n; cin >> n; vector<int> p(n), chosen; for (int &x : p) cin >> x; int ans = 0; auto dfs = [&](auto &&self, int i) -> void { if (i == n) { int len = chosen.size(); if (len <= 2) { ans = max(ans, len); return; } for (int j = 1; j + 1 < len; ++j) { if (chosen[j] >= chosen.front() || chosen[j] >= chosen.back()) return; } ans = max(ans, len); return; } self(self, i + 1); chosen.push_back(p[i]); self(self, i + 1); chosen.pop_back(); }; dfs(dfs, 0); cout << ans << '\n'; } int main() { cin.tie(0)->sync_with_stdio(0); solve(); }
- 1
信息
- ID
- 2319
- 时间
- 1000ms
- 内存
- 512MiB
- 难度
- 9
- 标签
- (无)
- 递交数
- 68
- 已通过
- 8
- 上传者