poj 3903 最长上升子序列,nlog(n)的算法

OJ

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <vector>
#include <map>
#include <set>
#include <string>
#include <cstring>
#include <list>
#include <queue>
#include <stack>
#include <cmath>

using namespace std;

#define PF(x) (scanf("%d",&x))
#define PT(x,y) (scanf("%d%d",&x,&y))
#define PR(x) (printf("%d\n",x))
#define PRT(x,y)(printf("%d %d\n",x,y))
#define PB(x)(scanf("%I64d",&x))
#define PRB(x)(printf("%I64d\n",(x)))
typedef __int64 LL;
#define N 100005
#define M 105
#define Mod 1000
#define Inf 0x7fffffff

int s[N];

void init()
{
   int n;
   while(scanf("%d",&n)!=EOF)
   {
	   int cnt = 0;
	   for(int i=0;i<n;i++)
	   {
		   int x;
		   PF(x);
		   if(i==0 || x>s[cnt-1])
	          s[cnt++]= x;
		   else
		   {
			   int low = 0;
			   int high = cnt;
			   while(low<=high)
			   {
				   int mid = (low+high)>>1;
				   if(s[mid]==x) {low = mid;break;}
				   else if(s[mid]>x) high = mid-1;
				   else low = mid+1;
			   }
			   s[low]= x;
		   }
	   }
	   PR(cnt);
   }
	return ;
}

int main()
{
	init();
	return 0;
}


你可能感兴趣的:(poj 3903 最长上升子序列,nlog(n)的算法)