80分求助,WA#3,WA#9

P1678 烦恼的高考志愿

guang_zi_guei_ji @ 2024-05-26 18:39:24

#include<bits/stdc++.h>
using namespace std;
long long mid=0,school[100010],student,n,m;
int main(){
    cin>>n>>m;
    for(int i=1;i<=n;i++){
        cin>>school[i];
    }
    sort(school+1,school+n+1);
    for(int i=1;i<=m;i++){
        cin>>student;
        long long j=lower_bound(school+1,school+n+1,student)-school,a=school[j]-student,b=student-school[j-1];
        if(a>b&&j-1>0){
            mid+=b;
        }
        else{
            mid+=a;
        }
    }
    cout<<mid;
    return 0;
}

by sickCat @ 2024-06-01 10:33:34

school数组中可能没有大于student的数,所以a,b取值那里都要用abs函数取绝对值


by syy999 @ 2024-07-05 10:41:59

我#3AC,#9TLE。


|