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

为什么二分时在a[i]-a[pre]=mid 时也要取出石头

Posted by xvector at 2016-11-22 17:43:06 on Problem 3258 and last updated at 2016-11-22 17:51:10
代码:
long long a[50005];
int main()
{
    long long L;
    int n,m;
    scanf("%I64d %d %d",&L,&n,&m);
    long long l=1e10,r=L;
    for(int i=1;i<=n;i++)
        {
            scanf("%I64d",&a[i]);
        }
    a[0]=0;
    a[n+1]=L;
    sort(a,a+n+2);
    for(int i=1;i<=n+1;i++)
        {
            l=min(l,a[i]-a[i-1]);//二分一般要注意初始边界
        }
    while(l<r)
    {
        long long mid=(l+r)/2;
        long long pre=0,stone=0;
        for(int i=1;i<=n+1;i++)
        {
            if(a[i]-a[pre]<=mid)//=  这里一定要取 = wa无数次TAT
                {
                    stone++;
                }
            else{
            pre=i;
            }
        }
        if(stone>m) r=mid;
        else l=mid+1;
        //cout<<l<<' '<<r<<endl;
    }
    printf("%I64d\n",l);
    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