Codeforces Round #605 (Div. 3) D. Remove One Element

时间:2022-07-26
本文章向大家介绍Codeforces Round #605 (Div. 3) D. Remove One Element,主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

D. Remove One Element

time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

You are given an array aa consisting of nn integers.

You can remove at most one element from this array. Thus, the final length of the array is n−1n−1 or nn.

Your task is to calculate the maximum possible length of the strictly increasing contiguous subarray of the remaining array.

Recall that the contiguous subarray aa with indices from ll to rr is a[l…r]=al,al+1,…,ara[l…r]=al,al+1,…,ar. The subarray a[l…r]a[l…r] is called strictly increasing if al<al+1<⋯<aral<al+1<⋯<ar.

Input

The first line of the input contains one integer nn (2≤n≤2⋅1052≤n≤2⋅105) — the number of elements in aa.

The second line of the input contains nn integers a1,a2,…,ana1,a2,…,an (1≤ai≤1091≤ai≤109), where aiai is the ii-th element of aa.

Output

Print one integer — the maximum possible length of the strictly increasing contiguous subarray of the array aa after removing at most one element.

Examples

input

Copy

5
1 2 5 3 4

output

Copy

4

input

Copy

2
1 2

output

Copy

2

input

Copy

7
6 5 4 3 2 4 3

output

Copy

2

Note

In the first example, you can delete a3=5a3=5. Then the resulting array will be equal to [1,2,3,4][1,2,3,4] and the length of its largest increasing subarray will be equal to 44.

哎,这题真的值得学习一下,dp运用的非常灵活,其实观察题目可知,最长连续递增序列长度增加只可能是两个小段的合并观察到这一点很重要!

没有删去元素时,运用dp,我们可以求出整个序列的最长递增序列长度,f[i]代表以元素a[i]结尾的序列的最长递增序列长度,递推方程如下:

f[i+1]=f[i]+1 if(a[i]<a[i+1)

else f[i+1]=1

有删去元素时,我们接着这样考虑:对于答案唯一能够改变的可能就是删掉了这个元素之后,前后两个递增序列合并起来变的大了,答案更新了吗?

所以,比方说我们要删除第i个元素,if(a[i-1]<a[i+1])那么可能会更新答案,那不就是f[i-1]加上一个以a[i+1]元素开头的最长递增子序列的长度吗?

那这个怎么求?还是o(n)dp啊!

ok,已AC~,细节详见代码

#include<bits/stdc++.h>
#define ll long long
#define rg register ll
using namespace std;
#define inf 2147483647
ll a[200005],f[200005],g[200005],maxx=-inf;
int main()
{
    ll n;
    cin>>n;
    for(rg i=1;i<=n;i++)cin>>a[i];
    f[1]=1,g[n]=1;
    for(rg i=2;i<=n;i++)
    {
        if(a[i]>a[i-1])f[i]=f[i-1]+1;
        else f[i]=1;
    }
    for(rg i=n-1;i>=1;i--)
    {
        if(a[i]<a[i+1])g[i]=g[i+1]+1;
        else g[i]=1;
    }
    for(rg i=1;i<=n;i++)maxx=max(maxx,f[i]);
    for(rg i=1;i<=n;i++)
    {
        if(a[i-1]<a[i+1])maxx=max(maxx,f[i-1]+g[i+1]);
    }
    cout<<maxx<<endl;
    while(1)getchar();
    return 0;
}