大佬求助

P1678 烦恼的高考志愿

Password201214 @ 2023-06-24 09:29:42

#include<bits/stdc++.h>
using namespace std;
int m, n, a[100100], b[100100]; 
int main(){
    cin>>m>>n;
    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 l=0, r=n+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])
            ans+=a[1]-b[i];
        else
            ans+=min(abs(a[l-1]-b[i]), abs(a[l]-b[i]));//加上两个绝对值中最小
    }
    cout<<ans;
    return 0;
}

by rnf5114 @ 2023-06-24 09:56:36

@Password201214 ans没开longlong,然后输入反了,应该先输入n再输入m


|