zoukankan      html  css  js  c++  java
  • poj 2533 Longest Ordered Subsequence

    LIS裸题, 是严格递增的不是 不下降子序列(一开始看错了WA了好几次)

    O(n*n)

    #include <stdio.h>
    #include <string.h>
    #define N 10100
    int a[N];
    int n;
    
    void LOS()
    {
        int dp[N],i,j,tmp,max;
        memset(dp,0,sizeof(dp));
        dp[1]=1; max=1;
        for(i=2; i<=n; i++)
        {
            for(tmp=0,j=1; j<i; j++)
                if(a[i]>a[j] && dp[j]>tmp)
                    tmp=dp[j];
            dp[i]=tmp+1;
            if(dp[i]>max) max=dp[i];
        }
        printf("%d\n",max);
    }
    int main()
    {
        int i;
        while(scanf("%d",&n)!=EOF)
        {
            for(i=1; i<=n; i++) scanf("%d",&a[i]);
            LOS();
        }
        return 0;
    }

    O(n*longn)

    //二分查找的算法,c[i]数组表示长度为i的子序列最大元素的最小值
    //每得到一个新的a[i]在二分c数组,找到合适位置去更新c数组
    #include <stdio.h>
    #include <string.h>
    #define N 1010
    int a[N],c[N],n;
    
    int binary_search(int key , int l , int r)
    {
        int mid;
        while(l<=r)
        {
            mid=(l+r)/2;
            if(c[mid]<key && key<=c[mid+1]) return mid+1;
            else if(key<=c[mid]) r=mid-1;
            else                 l=mid+1;
        }
    }
    void LOS()
    {
        int i,j,x,top;
        memset(c,0,sizeof(c));
        c[1]=a[1]; top=1;
        for(i=2; i<=n; i++)
        {
            if(a[i]<=c[1])       c[1]=a[i];
            else if(a[i]>c[top]) c[++top]=a[i];
            else
            {
                x=binary_search(a[i],1,top);
                c[x]=a[i];
            }
        }
        printf("%d\n",top);
    }
    int main()
    {
        int i;
        while(scanf("%d",&n)!=EOF && n)
        {
            for(i=1; i<=n; i++) scanf("%d",&a[i]);
            LOS();
        }
        return 0;
    }
  • 相关阅读:
    事务传播机制,搞懂。
    洛谷 P1553 数字反转(升级版) 题解
    洛谷 P1200 [USACO1.1]你的飞碟在这儿Your Ride Is Here 题解
    洛谷 P1055 ISBN号码 题解
    洛谷 P2141 珠心算测验 题解
    洛谷 P1047 校门外的树 题解
    洛谷 P1980 计数问题 题解
    洛谷 P1008 三连击 题解
    HDU 1013 题解
    HDU 1012 题解
  • 原文地址:https://www.cnblogs.com/scau20110726/p/2740797.html
Copyright © 2011-2022 走看看