POJ1631

题意

求最长上升子序列长度

思路

本题直接用dp写,会超时,需要优化

优化办法:二分+dp

AC代码

//4test n
//6     p
//4 2 6 3 1 5  ->3
//10
//2 3 4 5 6 7 8 9 10 1  ->9
//8
//8 7 6 5 4 3 2 1  ->1
//9
//5 8 9 2 3 1 7 4 6  ->4

#include<iostream>
#include<string.h>
#include<algorithm>
#define inf 0x3f3f3f3f
using namespace std;

int a[40020],dp[40020];

int main()
{
    std::ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int n,p;
    cin>>n;
    while(n--)
    {
        memset(a,0,sizeof(0));
      // fill(dp,dp+n,inf);
        cin>>p;
        for(int i=1; i<=p; i++)
            cin>>a[i];
        memset(dp,inf,sizeof(dp));
       // int maxx=0;
        for(int i=1;i<=p;i++)
            *lower_bound(dp,dp+p,a[i])=a[i];
        cout<<lower_bound(dp,dp+p,inf)-dp<<endl;
       // cout<<endl<<endl;
    }
    return 0;
}
原文地址:https://www.cnblogs.com/OFSHK/p/14667220.html