zju 1986 Bridging Signals(最长上升非降子序列)

#include<iostream>
using namespace std;
#define MAX_N 40001

int dp[MAX_N],bridge[MAX_N];
int main(){
	int T,n,i,j;
	cin>>T;
	while(T--){
		cin>>n;
		for(i=1;i<=n;i++)
			cin>>bridge[i];
		dp[1]=bridge[1];
		int len=1;
		for(j=2;j<=n;j++){
			int low=1,high=len;
				
			while(low<=high){
				int mid=(low+high)>>1;
				if(dp[mid]<bridge[j])
					low=mid+1;
				else 
					high=mid-1;

			}
			dp[low] = bridge[j];
			len=len>low?len:low;
		}
		cout<<len<<endl;
	}
	return 0;
}


//STL 版本
#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
using namespace std;
#define MAX_N 40001
int dp[MAX_N],b[MAX_N];
int main(){
	int t;
	scanf("%d",&t);
	while(t--){
		int n,i;
		scanf("%d",&n);
		for(i=1;i<=n;i++)
			scanf("%d",&b[i]);
		dp[1]=b[1];
		int len=1,p;
		for(i=2;i<=n;i++){
			p=upper_bound(dp+1,dp+len+1,b[i])-dp;
			dp[p]=b[i];
			if(p>len)
				len=p;
		}
		printf("%d\n",len);
			
	}
	return 0;
}


你可能感兴趣的:(zju 1986 Bridging Signals(最长上升非降子序列))