CF446A DZY Loves Sequences 简单dp

本文介绍了一种通过动态规划算法解决的问题,即在一个整数序列中找到最长的严格递增子段,允许修改一个数字。文章详细解释了如何定义两个DP数组来分别计算以每个位置开始和结束的最长递增子段长度,并展示了如何通过比较和调整这些子段来得到全局最优解。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

 

转载于:https://www.cnblogs.com/zxyqzy/p/10215200.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值