/* DP:先用l,r数组记录前缀后缀上升长度,最大值会在三种情况中产生: 1. a[i-1] + 1 < a[i+1],可以改a[i],那么值为l[i-1] + r[i+1] + 1 2. l[i-1] + 1 3. r[i+1] + 1 //修改a[i] */ #include <cstdio> #include <algorithm> #include <cstring> using namespace std; const int MAXN = 1e5 + 10; const int INF = 0x3f3f3f3f; int a[MAXN]; int l[MAXN], r[MAXN]; int n; int main(void) //Codeforces Round #FF (Div. 1) A. DZY Loves Sequences { scanf ("%d", &n); int ans = 0; for (int i=1; i<=n; ++i) { scanf ("%d", &a[i]); l[i] = 1; if (i > 1 && a[i] > a[i-1]) l[i] = l[i-1] + 1; } for (int i=n; i>=1; --i) { r[i] = 1; if (i < n && a[i] < a[i+1]) r[i] = r[i+1] + 1; } for (int i=1; i<=n; ++i) { ans = max (ans, l[i]); ans = max (ans, r[i]); if (i > 1 && i < n && a[i-1] + 1 < a[i+1]) ans = max (ans, l[i-1] + r[i+1] + 1); if (i > 1) ans = max (ans, l[i-1] + 1); if (i < n) ans = max (ans, r[i+1] + 1); } printf ("%d\n", ans); return 0; }
时间: 2024-12-15 01:43:20