UVA10534 Wavio Sequence
题意
定义一个合法序列为:
- 长度为奇数。
- 前一半的数字严格递增。
- 后一半的数字严格递减。
给定一个序列 ,求其最长的合法子序列长度。
,多测。
解法
预处理最长上升子序列 和最长下降子序列 , 表示以 为末尾, 表示以 为开头。
枚举分割点 ,那么两边最长可以达到 和 。显然长度取两者之中较小的。
答案如下:
时间复杂度
- 预处理 ,可以使用 的做法。
- 计算答案,。
故时间复杂度 ,预期得分 。
代码
AC 代码
/**
* Problem: UVA10534 Wavio Sequence
* Author: OIer_wst
* Date: 2025-07-25
*/
#include <bits/stdc++.h>
using lint = long long;
using pii = std::pair<int, int>;
const int N = 1e4 + 10;
int n, ans, a[N], f[N], g[N];
int main() {
std::cin.tie(0)->sync_with_stdio(0);
while (std::cin >> n) {
ans = 0;
for (int i = 1; i <= n; ++i) std::cin >> a[i];
std::vector<int> t;
for (int i = 1; i <= n; ++i) {
int pos = std::lower_bound(t.begin(), t.end(), a[i]) - t.begin();
if (pos == t.size()) t.emplace_back(a[i]);
else t[pos] = a[i];
f[i] = pos + 1;
}
t.clear();
for (int i = n; i; --i) {
int pos = std::lower_bound(t.begin(), t.end(), a[i]) - t.begin();
if (pos == t.size()) t.emplace_back(a[i]);
else t[pos] = a[i];
g[i] = pos + 1;
}
for (int i = 1; i <= n; ++i)
ans = std::max(ans, 2 * std::min(f[i], g[i]) - 1);
std::cout << ans << "\n";
}
std::cout.flush();
return 0;
}