最长上升子序列裸题
在网上看到有两种方法...一种复杂度O(N^2),一种O(NlogN)。orz

O(N^2):

#include
#define N 1001
int main()
{
    int n,ans;
    int a[N],d[N];

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

    ans=0;
    for(int i=1; i<=n; i++)
    {
        d[i]=1;
        for(int j=1; j<=i-1; j++)
        {
            if(a[i]>a[j] && d[i]1)
                d[i]=d[j]+1;
        }
        if(d[i]>ans)
            ans=d[i];
    }
    printf("%d\n",ans);
    }
    return 0;
}
View Code

O(NlogN):

 #include   
    using namespace std;  
      
    const int maxn = 1000 + 10;  
    int n, num[maxn], LIS[maxn];  
      
    int binary_search(int start, int end, int target)  
    {  
        while (end > start) {  
            int mid = (end + start) / 2;  
            if (target > LIS[mid])  
                start = mid + 1;  
            else  
                end = mid;  
        }  
        return start;  
    }  
      
    int lis()  
    {  
        LIS[1] = num[1];  
        int maxl = 1;  
        for (int i = 2; i <= n; ++i) {  
            if (num[i] > LIS[maxl])  
                LIS[++maxl] = num[i];  
            else {  
                int pos = binary_search(1, maxl, num[i]);  
                LIS[pos] = num[i];  
            }  
        }  
        return maxl;  
    }  
      
    int main()  
    {  
        scanf("%d", &n);  
        for (int i = 1; i <= n; ++i)  
            scanf("%d", &num[i]);  
        printf("%d\n", lis());  
    }  
View Code