2.2最长递增子序列

题意:在可以改变一个数字的前提之下,算出连续最长递增的序列的长度

思路:其实就是模拟的找最长的长度就是了,不过不是暴力的每一个都找,在找到一段长度之后,直接跳回这次找到的最后面位置

找几个例子:1 2 3 6 5这个答案是5,把6修改成4,
而2 3 2 3 4这个答案是4,把3变成1

#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include <iomanip>
#include<cmath>
#include<float.h> 
#include<string.h>
#include<algorithm>
#define sf scanf
#define scf(x) scanf("%d",&x)
#define scff(a,b) scanf("%d%d",&a,&b)
#define pf printf
#define prf(x) printf("%d\n",x)
#define mm(x,b) memset((x),(b),sizeof(x))
#include<vector>
#include<queue>
#include<map>
#define rep(i,a,n) for (int i=a;i<n;i++)
#define per(i,a,n) for (int i=a;i>=n;i--)
typedef long long ll;
const ll mod=1e9+100;
const double eps=1e-8;
using namespace std;
const double pi=acos(-1.0);
const int inf=0xfffffff;
const int N=1e6+2;
int a[N];
int main()
{
    int n,x,last,l=1,low1;//low记录返回的位置
    int maxn=0;    //答案
    int temp=0;    //记录有没有修改
    scf(n);
    rep(i,0,n)
        scf(a[i]);
    last=a[0];    //last是记录上一个数字是多少
    rep(i,1,n)
    {
        if(a[i]>last)//大于就长度加一,更新last
        {
            last=a[i];
            l++;
        }else
        {
            if(temp==0)        //判断是否改过一次,如果没改过就改
            {
                temp++;    //改变标记
                if(i>=2&&(a[i]-a[i-2])>=2)    //这就是第一个例子,可以把当前不符合的,,前一个改小
                last=a[i];
                else                            //这个就是把后一个改大
                last=last+1;
                low1=i;                //low记录返回的位置
                l++;
            }else
            {
                maxn=max(l,maxn);
                temp=0;
                l=1;
                i=low1;            //返回位置继续找长度
                last=a[i];
            }
        }
    }
    if(temp==0) l++;    //这个就是第二个例子,最后面的长度,可以把前一个数字改小,长度加一
        maxn=max(maxn,l);
    prf(maxn);
    return 0;
} 

猜你喜欢

转载自www.cnblogs.com/wzl19981116/p/10043541.html