Mushroom 的序列 CF Round_FF div1 A

Mushroom 的序列

【问题描述】
Mushroom 手中有n 个数排成一排,现在Mushroom 想取一个连续的子序列,使得这个子序列满足:最多只改变一个数,使得这个连续的子序列是严格上升子序列,Mushroom 想知道这个序列的最长长度是多少。

【输入格式】
第一行一个整数n,表示有n 个数。
第二行为n 个数。

【输出格式】
一个数,为最长长度。

【输入样例】
6
7 2 3 1 5 6

【输出样例】
5

【样例解释】
选择第2 个数到第6 个数,把1 改变成4 即可。

【数据范围】
对于30%的数据,n<=10
对于60%的数据,n<=1000
对于100%的数据,n<=100000

思路:
处理出所有的上升子串(当前位置最长的),然后考虑可以更改一个数也就是相邻子串的合并。细节问题就不详述啦,注意是严格上升哟。

#include <cstdio>
#include <iostream>
#include <cstring>
#include <cmath>
using namespace std;
const int N = 100010;

struct Edge{
    int l, r;
}ed[N];

int n, idc=0;
int a[N];

inline int read(){
    int x=0,f=1;char ch=getchar();
    while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
    while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
    return x*f;
}

int main(){ 
    freopen("seq.in", "r", stdin);
    freopen("seq.out", "w", stdout);
    scanf("%d", &n);
    for(register int i=1; i<=n; ++i) a[i] = read();
    ed[++idc].l = 1;
    for(register int i=2; i<=n; i++){
        if(a[i] > a[i-1]) continue;
        if(a[i] <= a[i-1]){
            ed[idc].r = i - 1;
            ed[++idc].l = i;
            continue;
        }
    }
    ed[idc].r = n;
    int ans = ed[1].r - ed[1].l + 1;
    for(register int i=2; i<=idc; i++){
        if(ed[i-1].l == ed[i-1].r || ed[i].l == ed[i].r)
        ans = max(ans, ed[i].r-ed[i-1].l+1);
        else{
            if((a[ed[i].l] > a[ed[i-1].r-1]+1) || (a[ed[i].l+1] > a[ed[i-1].r]+1))
            ans = max(ans, ed[i].r-ed[i-1].l+1);
            else{
                ans = max(ans, ed[i-1].r-ed[i-1].l+2);
                ans = max(ans, ed[i].r-ed[i].l+2);
            }
        } 
    }
    printf("%d", ans);
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值