zoukankan      html  css  js  c++  java
  • Codeforces702A

    题意:
    求一个连续的最长子序列长度;
    思路:
    没看仔细还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
    ",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
    ",len);
        return 0;
    }
  • 相关阅读:
    MySQL索引
    《深度探索C++对象模型》笔记——Data语意学
    《深度探索C++对象模型》笔记——Function语意学
    近期的bug常见[从以前的零散笔记中整理]
    一个小trick
    3月9日-日记
    第一次考试_心得
    第一次考试_笔记
    哈希笔记
    Dp刷版笔记
  • 原文地址:https://www.cnblogs.com/keyboarder-zsq/p/5934895.html
Copyright © 2011-2022 走看看