大体题意:
给你一个长度为n 的数组,要求求出一个长度为奇数并且最长的上升子序列(不一定连续),要求前k + 1项严格递增,后k+1项严格递减!
输出最大长度?
思路:
最长上升子序列,
因为n 最多是10000,我们必须用n log n 级别的复杂度!
我们可以先预处理,求出到每一个位置i 的最长上升子序列的长度!
在倒着求,倒着枚举求出每一项的最长上升子序列长度! (这样反过来就是最长下降子序列)
然后枚举每一个位置!求出左边最长长度,和右边最长长度 取个最小值 就是当前的最长上升子序列!
不断更新答案即可!
详细见代码:
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn = 10000 + 10;
int a[maxn];
int lcs[maxn];
int dp[maxn],dp2[maxn];
int main(){
int n;
while(scanf("%d",&n) == 1){
for (int i = 1; i <= n; ++i)scanf("%d",&a[i]);
int p = 1;
lcs[1] = a[1];
dp[1] = 1;
for (int i = 2; i <= n; ++i){
if (a[i] > lcs[p]){
lcs[++p] = a[i];
dp[i] = p;
}
else {
int m = lower_bound(lcs,lcs+p,a[i]) - lcs;
lcs[m] = a[i];
dp[i] = p;
}
}
lcs[1] = a[n];
dp2[n] = 1;
p = 1;
for (int i = n-1; i >= 1; --i){
if (a[i] > lcs[p]){
lcs[++p] = a[i];
dp2[i] = p;
}else {
int m = lower_bound(lcs,lcs+p,a[i]) - lcs;
lcs[m] = a[i];
dp2[i] = p;
}
}
int ans = -1;
for (int i = 1; i <= n; ++i){
int t = min(dp[i],dp2[i]);
ans = max(ans,t);
}
printf("%d\n",2*ans-1);
}
return 0;
}