Codeforces702A - Maximum Increase【尺取】

题意:
求一个连续的最长子序列长度;
思路:
没看仔细还wa1了…以为LIS…
然后写了尺取吧。。。= =太不仔细了。不过收获是LIS特么写挫了然后看了学长的blog<-点我…

题目的挫code…

#include <iostream>
#include <cstdio>
#include <string.h>
#include <algorithm>
using namespace std;
typedef __int64 LL;

const int N=1e5+10;

int n;
int a[N];


int main()
{
    scanf("%d",&n);
    for(int i=1;i<=n;i++)
        scanf("%d",&a[i]);

    int s,t,ans,temp;
    s=t=1;
    ans=1;
    temp=0;
    while(s<=n)
    {
        while(t+1<=n&&a[t]<a[t+1])
        {
            temp=t-s+2;
            t++;
        }
        s=t+1;
        t=t+1;
        ans=max(temp,ans);
    }
    printf("%d\n",ans);
    return 0;
}

LIS的nlogn写法:

#include <iostream>
#include <cstdio>
#include <string.h>
#include <algorithm>
using namespace std;
typedef __int64 LL;

const int N=1e5+10;

int n;
int dp[N];
int a[N];

int kill(int x,int len)
{
    int s=1,t=len;
    while(s<=t)
    {
        int mid=(s+t)/2;
        if(dp[mid]>=x)//这里等于也写成末尾-1,主要是最后返回起点,因为最重要的是要找到哪个点比他大然后就放那
            t=mid-1;
        else
            s=mid+1;
    }
    return s;
}

int main()
{
    scanf("%d",&n);
    for(int i=1;i<=n;i++)
        scanf("%d",&a[i]);

    int len=1,j;

    dp[1]=a[1];
    for(int i=2;i<=n;i++)
    {
        if(a[i]<=dp[1])         //如果a[i]比dp数组的第一位小;
            j=1;
        else if(a[i]>dp[len])  //如果a[i]比dp数组最大的还大;
            j=++len;
        else
            j=kill(a[i],len);  //寻找在dp数组的位置;
        dp[j]=a[i];
    }
    printf("%d\n",len);
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值