超时求助

P1678 烦恼的高考志愿

1234xwk @ 2023-08-09 09:07:32

#include<bits/stdc++.h>
using namespace std;
int a[100001],b[100001];
int main(){
    int n,m;
    cin>>n>>m;
    for(int i=1;i<=n;i++){
        cin>>a[i];
    }
    for(int i=1;i<=m;i++){
        cin>>b[i];
    }
    sort(a+1,a+n+1);
    int ans=0;
    for(int i=1;i<=m;i++){
        int start=1,end=n+1;
        int mid=(start+end)/2;
        while(start<end){
            if(a[mid]<=b[i]){
                start=mid+1;
            }
            else end=mid;
        }
        if(b[i]<=a[1]){
            ans+=a[1]-b[i];
        }
        else{
            ans+=min(abs(a[start-1]-b[i]),abs(a[start]-b[i]));
        }
    }
    cout<<ans;
    return 0;
}

by dsdsds123 @ 2023-08-09 09:12:51

厉害


|