题解:UVA10534 Wavio Sequence
残阳如血
·
·
题解
UVA10534 Wavio Sequence
题意
定义一个合法序列为:
- 长度为奇数。
- 前一半的数字严格递增。
- 后一半的数字严格递减。
给定一个序列 \{a_n\},求其最长的合法子序列长度。
## 解法
预处理最长上升子序列 $\{f_n\}$ 和最长下降子序列 $\{g_n\}$,$f_i$ 表示以 $i$ 为末尾,$g_i$ 表示以 $i$ 为开头。
枚举分割点 $i$,那么两边最长可以达到 $f_i$ 和 $g_i$。显然长度取两者之中较小的。
答案如下:
$$
\min\limits_{i=1}^{n}\{2\min\{f_i,g_i\}-1\}
$$
### 时间复杂度
- 预处理 $f,g$,可以使用 $O(n\log n)$ 的做法。
- 计算答案,$O(n)$。
故时间复杂度 $O(n \log n)$,预期得分 $100$。
### 代码
:::success[AC 代码]
```c++
/**
* 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;
}
```