繁华模拟赛 最长上升子串

#include<iostream>
#include<cstdio>
#include<string>
#include<cstring>
#include<algorithm>
using namespace std;
int n,a[300005],l[300005],r[300005],ans;
int main(){
    freopen("lis.in","r",stdin);
    freopen("lis.out","w",stdout);
    cin>>n;
    for(int i = 1;i <= n;i++) scanf("%d",&a[i]);
    for(int i = 1;i <= n;i++){
        if(a[i] > a[i-1]) l[i] = l[i-1] + 1;
        else l[i] = 1;
    }
    r[n] = 1;
    for(int i = n - 1;i >= 1;i--){
        if(a[i] < a[i+1]) r[i] = r[i+1] + 1;
        else r[i] = 1;
    }
    for(int i = 1;i <= n;i++){
        if(a[i-1] <= a[i+1] - 2) ans = max(ans,l[i-1] + r[i+1] + 1);
        else ans = max(ans,l[i-1] + 1);
    }
    cout<<ans;
    return 0;
}

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<vector>
#include<queue>
#include<map>
#include<set>
#include<stack>
#include<cstdlib>
#define INF 100000000
#define fi first
#define se second
using namespace std;
typedef long long LL;
typedef pair<int,int> pii;
int a[300005],l[300005],r[300005];
int main()
{
    int n,i,cou=0,ans=0;
    freopen("lis.in","r",stdin);
    freopen("lis.out","w",stdout);
    cin>>n;
    a[0]=0;
    a[n+1]=0;
    for(i=1;i<=n;i++)
        scanf("%d",&a[i]);
    for(i=1;i<=n;i++)
    {
        if(a[i]>a[i-1])
        {
            cou++;
            l[i]=cou;
        }
        else
            cou=1,l[i]=cou;
    }
    cou=0;
    for(i=n;i>=1;i--)
    {
        if(a[i]<a[i+1])
        {
            cou++;
            r[i]=cou;
        }
        else
            cou=1,r[i]=cou;
    }
    for(i=1;i<=n;i++)
    {
        if(a[i-1]+1<a[i+1])
            ans=max(ans,l[i-1]+1+r[i+1]);
        else
        {
            ans=max(ans,l[i-1]+1);
            ans=max(ans,r[i+1]+1);
        }
    }
    cout<<ans<<endl;
    return 0;
}
// davidlee1999WTK 2014/
// srO myk Orz
//ios::sync_with_stdio(false);

 

posted @ 2016-08-20 23:02  ACforever  阅读(171)  评论(0编辑  收藏  举报