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

上代码,二分法

Posted by 10041112acmer at 2013-01-22 17:19:26 on Problem 1631
#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:
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