救救孩子吧!!只有10分

P1678 烦恼的高考志愿

dongran @ 2024-02-19 20:56:18

#include <iostream>
#include <bits/stdc++.h>

using namespace std;

const int N=1010,M=10010;
int n,m;
int school[N], student[M];
int res=0;
int ans=0x3f3f3f3f;

int main(){
    cin>>m>>n;
    for(int i=0;i<m;i++) cin>>school[i];
    for(int i=0;i<n;i++) cin>>student[i];

    sort(school,school+m-1);

    //每次选与k相匹配的差距最小的

    for(int i=0;i<n;i++){
        int k=student[i];

        int l=0,r=m-1;
        while(l<r){
            int mid=l+r>>1;

            if(school[mid]>=k) 
                r=mid;
            else 
                l=mid+1;
        }
        //res+=abs(school[l]-student[i]);
        res+=min(abs(school[l]-student[i]), abs(school[l-1]-student[i]));
    }
    cout<<res<<endl;

    return 0;
}

|