P1678 烦恼的高考志愿

点击查看代码
#include<bits/stdc++.h>
using namespace std;

typedef long long LL;
const int M=1e5+10;
int s[M];
int n,m;

int main()
{
    ios::sync_with_stdio(0),cin.tie(0);
    cin>>m>>n;
    for(int i=0;i<m;i++) cin>>s[i];

    LL ans =0;

    sort(s,s+m);

    for(int i=0;i<n;i++){
        int sc;
        cin>>sc;
        
        int l=lower_bound(s,s+m,sc)-s;
        if(l==0){
            ans+=s[l]-sc;
        }else if(l==m){
            ans+=sc-s[l-1];
        }else{
            ans+=min(s[l]-sc,sc-s[l-1]);
        }
    }

    cout<<ans<<endl;
}
posted @ 2026-01-25 23:40  AnoSky  阅读(3)  评论(0)    收藏  举报