pku 1631 Bridging signals nLogn效率的最长上升子序列

 

nLogn效率的最长上升子序列,第一次用二分查找

 

题目:

http://acm.pku.edu.cn/JudgeOnline/problem?id=1631

#include <stdio.h> #include <string.h> #define maxn 40001 int num[maxn]; //B[i]表示序列长度为i的所有序列的末尾元素的最小值 int B[maxn]; //opt[i]表示以第i个元素结尾的序列中的最长上升子序列的长度 int opt[40001]; int main(){ freopen("data.txt","r",stdin); int T,n,i; scanf("%d",&T); while(T--) { scanf("%d",&n); for(i=1;i <= n;i++) scanf("%d",num + i); memset(opt,1,sizeof(opt)); int s,left,right,mid; int blen = 0; int max = 0; for(i = 1;i <= n;i++){ left = 1; right = blen; s = num[i]; while(left <= right){ mid = (left + right) / 2; if(B[mid] < s) left = mid + 1; else right = mid - 1; } opt[i] = left; B[left] = s; blen = blen < left ? left : blen; max = max < opt[i] ? opt[i] : max; } printf("%d/n",max); } return 0; }

你可能感兴趣的:(pku 1631 Bridging signals nLogn效率的最长上升子序列)