Title: C. dzy loves sequences (LIS upgrade)
Question:
Of the N numbers, a maximum of one number is changed, and the maximum length of the subsequence (continuous) that can be reached is obtained.
Analysis:
Consider the number of I, whether it can be changed, and then splice the two strings before and after, and maintain the current maximum value
Status:
Left [I]: indicates the maximum length of the strictly ascending subsequence ending with I
Right [I]: indicates the maximum length of the strictly ascending subsequence starting from I.
DP [I]: indicates the length of the string before and after splicing after changing the number of I
Transfer equation:
dp[i] = max{left[i-1] + right[i+1] + 1 | a[i-1] + 1 < a[i+1]};
Core:
for(i = 1; i<=n; i++){ if(a[i-1] >= a[i]) ans = max(ans, right[i] + 1); if(a[i+1] <= a[i]) ans = max(ans, left[i] + 1); if(a[i-1] + 1 < a[i+1]) ans = max(ans, left[i-1] + right[i+1] + 1);}
Code:
#include <stdio.h>#include <iostream>#include <math.h>#include <algorithm>#include <string.h>#include <string>#include <queue>#include <stack>#include <map>#include <vector>#include <time.h>using namespace std;int a[100000+10];int L[100000+10];int R[100000+10];int main(){//freopen("a.txt", "r", stdin);int n, i, j;while(~scanf("%d", &n)){for(i = 1; i<=n; i++){scanf("%d", &a[i]);}memset(L, 0, sizeof(L));for(i = 1; i<=n; i++){L[i] = 1;if(i>1 && a[i] > a[i-1]){L[i] = max(L[i], L[i-1]+1);}}memset(R, 0, sizeof(R));int ans = 0;for(i = n; i>=1; i--){R[i] = 1;if(i<n && a[i] < a[i+1]){R[i] = max(R[i], R[i+1]+1);}ans = max(ans, R[i]);}for(i = 1; i<=n; i++){if(i>1 && a[i-1] >= a[i])ans = max(ans, L[i-1] + 1);if(i<n && a[i] >= a[i+1])ans = max(ans, R[i+1] + 1);if(i>1 && i<n && a[i-1] + 1 < a[i+1])ans = max(ans, L[i-1] + R[i+1] + 1);}printf("%d\n", ans);}return 0;}