令 \(f_i,g_i\) 分别为以 \(i\) 结尾的最长序列,以 \(i\) 开始的最长序列,答案为 \(\max\{f_i+1,g_i+1,[a_{i-1}+1\leqslant a_{i+1}-1]*(f_{i-1}+1+g_{i+1})\}\)。
#include <iostream>
using namespace std;
const int N = 1000010;
int n;
int a[N];
int f[N],g[N];
int main () {
cin >> n;
for (int i = 1;i <= n;i++) cin >> a[i];
f[1] = 1;
for (int i = 2;i <= n;i++) {
f[i] = 1;
if (a[i - 1] < a[i]) f[i] = max (f[i],f[i - 1] + 1);
}
g[n] = 1;
for (int i = n - 1;i >= 1;i--) {
g[i] = 1;
if (a[i] < a[i + 1]) g[i] = max (g[i],g[i + 1] + 1);
}
int ans = 0;
for (int i = 1;i <= n;i++) {
ans = max (ans,max (f[i] + 1,g[i] + 1));
if (a[i - 1] + 1 <= a[i + 1] - 1) ans = max (ans,f[i - 1] + 1 + g[i + 1]);
}
cout << min (ans,n) << endl;
return 0;
}
标签:签到,max,P9147,int,序列,最长
From: https://www.cnblogs.com/incra/p/17223873.html