Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

LIS 不优化O(n^2) 用二分查找优化O(n*log n)

Posted by 947186602 at 2020-07-16 10:49:37 on Problem 1631
#include<cstdio>
#include<algorithm>
#define MAX_P 40005
#define INF 100000
using namespace std;
int dp[MAX_P];  //长度为i+1的递增子序列末尾元素的最小值
int T,P;
int port[MAX_P];

void solve(){
	fill(dp,dp+P,INF);//INF表示没有
	for(int i = 0;i < P;++i){ //从长度1开始 
		*lower_bound(dp,dp+P,port[i]) = port[i]; //STL的二分查找,返回第一个大于等于的指针
	}
	printf("%d\n",lower_bound(dp,dp+P,INF) - dp);
}
int main(){
	scanf("%d",&T);
	for(int i = 0;i < T;++i){
		scanf("%d",&P);
		for(int j = 0;j < P;++j)
			scanf("%d",&port[j]);
		solve(); 
	}
} 

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator