# Codeforces round # ff (255) C. dzy loves sequences (LIS upgrade)

Source: Internet
Author: User

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;}`

Related Keywords:
Related Article

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

## A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

• #### Sales Support

1 on 1 presale consultation

• #### After-Sales Support

24/7 Technical Support 6 Free Tickets per Quarter Faster Response

• Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.