CF446A DZY Loves Sequences 简单dp

Posted zxyqzy

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了CF446A DZY Loves Sequences 简单dp相关的知识,希望对你有一定的参考价值。

DZY has a sequence a, consisting of n integers.

We‘ll call a sequence ai,?ai?+?1,?...,?aj (1?≤?i?≤?j?≤?n) a subsegment of the sequence a. The value (j?-?i?+?1) denotes the length of the subsegment.

Your task is to find the longest subsegment of a, such that it is possible to change at most one number (change one number to any integer you want) from the subsegment to make the subsegment strictly increasing.

You only need to output the length of the subsegment you find.

Input

The first line contains integer n (1?≤?n?≤?105). The next line contains n integers a1,?a2,?...,?an (1?≤?ai?≤?109).

Output

In a single line print the answer to the problem — the maximum length of the required subsegment.

Examples
Input
Copy
6
7 2 3 1 5 6
Output
Copy
5
Note

You can choose subsegment a2,?a3,?a4,?a5,?a6 and change its 3rd element (that is a4) to 4.

问最多修改一个数字,序列可获得地最大严格递增字段长度为多大;

考虑dp;

dp1 表示以 i 位置结尾的最长子段长度;

dp2 表示以 i 位置开头的最长子段长度;

特判一下当 n=1时,长度为1;

考虑拼接:当 x[ i+1 ]>=2+ x[ i-1 ]时,那么改变 x[ i ]即可拼接子段

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstdlib>
#include<cstring>
#include<string>
#include<cmath>
#include<map>
#include<set>
#include<vector>
#include<queue>
#include<bitset>
#include<ctime>
#include<deque>
#include<stack>
#include<functional>
#include<sstream>
//#include<cctype>
//#pragma GCC optimize(2)
using namespace std;
#define maxn 200005
#define inf 0x7fffffff
//#define INF 1e18
#define rdint(x) scanf("%d",&x)
#define rdllt(x) scanf("%lld",&x)
#define rdult(x) scanf("%lu",&x)
#define rdlf(x) scanf("%lf",&x)
#define rdstr(x) scanf("%s",x)
typedef long long  ll;
typedef unsigned long long ull;
typedef unsigned int U;
#define ms(x) memset((x),0,sizeof(x))
const long long int mod = 1e9 + 7;
#define Mod 1000000000
#define sq(x) (x)*(x)
#define eps 1e-3
typedef pair<int, int> pii;
#define pi acos(-1.0)
//const int N = 1005;
#define REP(i,n) for(int i=0;i<(n);i++)
typedef pair<int, int> pii;
inline ll rd() {
    ll x = 0;
    char c = getchar();
    bool f = false;
    while (!isdigit(c)) {
        if (c == ‘-‘) f = true;
        c = getchar();
    }
    while (isdigit(c)) {
        x = (x << 1) + (x << 3) + (c ^ 48);
        c = getchar();
    }
    return f ? -x : x;
}

ll gcd(ll a, ll b) {
    return b == 0 ? a : gcd(b, a%b);
}
ll sqr(ll x) { return x * x; }

/*ll ans;
ll exgcd(ll a, ll b, ll &x, ll &y) {
    if (!b) {
        x = 1; y = 0; return a;
    }
    ans = exgcd(b, a%b, x, y);
    ll t = x; x = y; y = t - a / b * y;
    return ans;
}
*/


int n;
int x[maxn];


int main() {
    //ios::sync_with_stdio(0);
    cin >> n;
    vector<int>dp1(maxn, 1);
    vector<int>dp2(maxn, 1);
    for (int i = 0; i <= n; i++)dp1[i] = dp2[i] = 1;
    for (int i = 0; i < n; i++)rdint(x[i]);
    if (n < 2) {
        cout << 1 << endl; return 0;
    }
    for (int i = 1; i < n; i++)
        dp1[i] = (x[i] > x[i - 1]) ? dp1[i - 1] + 1 : 1;
    for (int i = n - 2; i >= 0; i--)
        dp2[i] = (x[i + 1] > x[i]) ? dp2[i + 1] + 1 : 1;
    int ans = 0;
    for (int i = 1; i < n; i++)ans = max(ans, dp1[i - 1] + 1);
    for (int i = 0; i < n; i++)ans = max(dp2[i + 1] + 1, ans);
    for (int i = 1; i <= n - 1; i++) {
        
        if (x[i + 1] - x[i - 1] >= 2) {
            ans = max(ans, dp1[i - 1] + 1 + dp2[i + 1]);
        }
    }
    cout << ans << endl;
    return 0;
}

 


以上是关于CF446A DZY Loves Sequences 简单dp的主要内容,如果未能解决你的问题,请参考以下文章

CodeForces 446A DZY Loves Sequences (DP+暴力)

CF A. DZY Loves Hash

CF444E. DZY Loves Planting

CF446D. DZY Loves Games

CF446C [DZY loves Fibonacci]

(CF)Codeforces445A DZY Loves Chessboard(纯实现题)