80分求助,WA#3#9

P1678 烦恼的高考志愿

LQ_Q @ 2024-03-27 17:11:45

#include<bits/stdc++.h>
using namespace std;
long long int n,m,ans,fs[100050],yg,l,r;
int main(){
    scanf("%lld%lld",&m,&n);
    for(int i=1;i<=m;i++) scanf("%lld",&fs[i]);
    sort(fs+1,fs+1+m);
    for(int i=1;i<=n;i++){
        scanf("%lld",&yg);
        l=1;
        r=m;
        while(l<r){
            if(fs[(l+r)/2]>=yg) r=(l+r)/2;
            else l=(l+r)/2+1;
        }
        if(fs[l]-yg>=yg-fs[l-1]&&l-1!=0) ans+=(yg-fs[l-1]);
        else ans+=(fs[l]-yg);
    } 
    printf("%lld",ans);
}

by thesadbee @ 2024-03-27 22:38:27

可以尝试把手写二分换成STL,本蒟蒻刚才全是WA,换成STL后就AC了,你也可以试一下


by LQ_Q @ 2024-04-01 21:48:44

@thesadbee Thanks!!!!!!!!!!!!!!!!!!!!!!AC了!


by Martin07 @ 2024-04-14 14:31:36

666


|