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 |
上代码,二分法#include<stdio.h> #include<stdlib.h> int list[40000]; int t,n,num,res,i,w; int binary_search(int left,int right,int path) { int mid; while(left<=right) { mid=(left+right)/2; if(list[mid]<path) left=mid+1; else right=mid-1; } return right;//输出大于等于num的最小的数的位置 } int main() { scanf("%d",&t); while(t--) { scanf("%d",&n); scanf("%d",&num); list[0]=num; res=1; for(i=1;i<n;i++) { scanf("%d",&num); if(num>list[res-1]) list[res++]=num;//比当前的数大的话插在后面 else if(num<list[res-1]) { w=binary_search(0,res-1,num);//如果小的话在前面找到合适的位置插入 list[w+1]=num; } } printf("%d\n",res); } return 0; } Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator