Codeforces Round #FF (Div. 1) A. DZY Loves Sequences
题目链接:
http://www.codeforces.com/contest/446/problem/A
题解:
dp1[x]表示以x结尾的最大严格升序连续串,dp2[x]表示以x开头的最大严格升序连续串
#include<iostream> #include<cstdio> #include<cstring> #include<map> #include<list> #include<stack> #include<algorithm> using namespace std; const int maxn = 100000 + 10; const int INF = 2e9; typedef __int64 LL; int n; int dp1[maxn], dp2[maxn]; int arr[maxn]; void init() { memset(dp1, 0, sizeof(dp1)); memset(dp2, 0, sizeof(dp2)); } int main() { while (scanf("%d", &n) == 1 && n) { init(); for (int i = 0; i < n; i++) scanf("%d", arr + i); dp1[0] = 1; for (int i = 1; i < n; i++) { if (arr[i] > arr[i - 1]) dp1[i] = dp1[i - 1] + 1; else dp1[i] = 1; } dp2[n - 1] = 1; for (int i = n - 2; i >= 0; i--) { if (arr[i] < arr[i + 1]) dp2[i] = dp2[i + 1] + 1; else dp2[i] = 1; } int ans = max(dp2[1] + 1, dp1[n - 2]+1); for (int i = 1; i < n - 1; i++) { if (arr[i - 1] < arr[i + 1] - 1) ans = max(ans, dp1[i - 1] + 1 + dp2[i + 1]); else { ans = max(ans,dp1[i - 1] + 1); ans = max(ans,dp2[i + 1] + 1); } } printf("%d\n", ans); } return 0; }