数列中求一个连续子序列中可改变其中一个数后为严格递增子序列的最大长度

问题

在这里插入图片描述

解答:

// #include<bits/stdc++.h>
#include <iostream> 
#include <cstdio> 
#include <fstream> 
#include <algorithm> 
#include <cmath> 
#include <math.h>
#include <vector> 
#include <queue> 
#include <string> 
#include <cstring> 
#include <map> 
#include <stack> 
#include <set> 
using namespace std;

int main() {
    int n,count = 1, max = 0;//n输入数据个数,count计数
    int flag = 0;//标记在子序列中是否已经发生改变
    scanf("%d",&n);
    int Num[n];
    int temp;
    for(int i = 0; i < n; i++){
        scanf("%d",&Num[i]);
    }
    for(int i = 0; i < n; i++){
        temp = Num[i];
        for(int j = i + 1; j < n; j++){
            if(temp < Num[j]){//严格递增
                temp = Num[j];
                count++;
            }else if(flag == 0){//flag为0表示尚未出现子序列中某数改变
                if(j == n-1){//当为最末尾数时直接+1
                    count++;
                    flag = 0;
                    break;
                }else if(Num[j] - Num[j-2] >1){//改变点前后两数差额大于一才可以更改
                    flag = 1;
                    temp = Num[j] - 1;//设改变点数为较大数-1
                    count++;
                }
            }else{
                flag = 0;
                break;
            }
        }
        if(max < count)
            max = count;
        count = 1;
    }
    printf("%d",max);
    return 0;
}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值