10
落实同学题单.ing。
难度在于积累 trick。
思路
首先介绍一个 trick:将
于是就简单了,观察到一个
以找
找
代码
有一点小细节。
#include <bits/stdc++.h>
using namespace std;
const int N = 4e6 + 7, INF = 1e9 + 7;
int n, ans, p[N], dp[N];
stack<int> s;
void clear() {
while (s.size())
s.pop();
}
int main() {
int T;
cin >> T;
while (T --) {
cin >> n, ans = n;
for (int i = 1; i <= n; i ++)
cin >> p[i], dp[i] = n;
clear();
for (int i = 1; i <= n; i ++) {
s.push(i);
while (!s.empty() && p[s.top()] <= i)
s.pop();
if (!s.empty())
dp[i] = -s.top();
}
clear();
for (int i = n; i >= 1; i --) {
while (!s.empty() && p[s.top()] > i)
s.pop();
if (!s.empty())
ans = min(ans, s.top() + dp[i]);
s.push(i);
}
cout << n - ans << '\n';
}
return 0;
}
知更鸟小姐****!