Online Judge | Problem Set | Authors | Online Contests | User | ||||||
---|---|---|---|---|---|---|---|---|---|---|
Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest |
LIS 不优化O(n^2) 用二分查找优化O(n*log n)#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: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator