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

提醒nlogn wa的同志们

Posted by LiWang112358 at 2009-05-23 20:21:58 on Problem 1609
二分的时候不要找到一个大于等于的就把它的位置返回,它后面可能有许多相等的,得越过这些相等的,返回最后的位置。

   int l,r,mid;
    l=1;r=tot;
    while (l<r)
    {
        mid=(l+r)/2;
        if (k[mid]==num)
        {
            while(k[mid]==k[mid+1]) mid++;//注意!!!!!!!!!!!
            return mid+1;
        }
        if (k[mid]>num) l=mid+1;
        else r=mid;
    }
    if (k[l]>=num)
    {
        while (k[l]==k[l+1]) l++;//注意!!!!!!!!!!!!!!
        return l+1;
    }
    else return l;

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