| ||||||||||
| 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 | |||||||||
提醒nlogn wa的同志们二分的时候不要找到一个大于等于的就把它的位置返回,它后面可能有许多相等的,得越过这些相等的,返回最后的位置。
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: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator