只过了#11,求调

P1678 烦恼的高考志愿

sub_10 @ 2024-10-24 09:54:39

#include<bits/stdc++.h>
using namespace std;
#define int long long
int a[114514];
int m,n;
int ans;
int judge(int x){
    int l,r,mid;
    l=0;
    r=m;
    while(l<r){
        mid=(l+r)/2;
        if(a[mid]<=x)
            l=mid+1;
        else
            r=mid;
    }
    int ans1=abs(a[r]-x);
    int ans2=abs(a[r-1]-x);
    if(x<=a[1])
        return a[1]-x;
    else
        return min(ans1,ans2);
}
signed main(){
    //ios_base::sync_with_stdio(0);
    //cin.tie(0);
    //cout.tie(0);

    cin>>m>>n;
    for(int i=1;i<=m;i++)
        cin>>a[i];
    sort(a+1,a+n+1);
    for(int i=1,x;i<=n;i++){
        cin>>x;
        ans+=judge(x);
    }
    cout<<ans;
    return 0;
}

by wusongxu @ 2024-10-24 17:49:06

sort(a+1,a+n+1)改成“sort(a+1,a+m+1)”


|