100分求助……

P1678 烦恼的高考志愿

yu1128 @ 2023-11-09 19:17:14

#include<iostream>
#include<cmath>
#include<algorithm>
using namespace std;
int main(){
    int m,n;
    cin>>m>>n;
    int a[100010],b[100010];
    for(int i=1;i<=m;i++) cin>>a[i];
    for(int i=1;i<=n;i++) cin>>b[i];
    int sum=0;
    sort(a+1,a+m+1);
    for(int i=1;i<=n;i++){
        int l=0,r=m+1;
        while(l<r){
            int mid=(l+r)/2;
            if(a[mid]<=b[i]){
                l=mid+1;
            }
            else r=mid;
        }
        if(b[i]<a[1]+1){
            sum+=a[1]-b[i];
        }
        else sum+=min(abs(a[l-1]-b[i]),abs(a[l]-b[i]));
    }
    cout<<sum;
}

by ikun_newperson @ 2023-11-09 19:23:23

#include <bits/stdc++.h>
using namespace std;
const int N=1e8;
long long n,a[N],x,m,cnt;
int main(){
    cin>>n>>m;
    for(int i=1;i<=n;i++){
        cin>>a[i];
    }
    sort(a+1,a+n+1);
    while(m--){
        cin>>x;
        int l=1,r=n;
        while(l+1<r){
            int mid=(l+r)>>1;
            if(a[mid]>=x)
                r=mid;
            else
                l=mid;
        }
        cnt+=abs(a[l]-x)<=abs(a[r]-x)?abs(a[l]-x):abs(a[r]-x);
    }
    cout<<cnt;
    return 0;
}

by Mrlvbo @ 2023-11-25 16:43:37

十年OI一场空,不开long long见祖宗 @Linhibit


|