求助,八十分

P1678 烦恼的高考志愿

竹下的魂 @ 2020-07-10 19:47:46

我已经知道学生信息可以不用数组 不过懒得改咯

#include <iostream>
#include <algorithm>
#include <cmath>
using namespace std;
int main(){
    int m,n;
    cin>>m>>n;
    int schools[int(1e5+10)];
    int students[int(1e5+10)];
    int sum = 0;

    for(int i = 1;i<=m;i++)
        cin>>schools[i];

    sort(schools+1,schools+m+1);

    for(int i = 1;i<=n;i++)
    {
        cin>>students[i];
        int w = lower_bound(schools+1,schools+m+1,students[i])-schools;
        if(w == m+1)
        {
           sum+=students[i]-schools[w];
        }
        else if(w == 1)
        {
            sum+=schools[w]-students[i];    
        }
        else
            sum+=min(abs(schools[w]-students[i]),abs(schools[w-1]-students[i]));
    }
    cout<<sum;
    return 0;
}

by 竹下的魂 @ 2020-07-10 20:12:20

已经解决


by 竹下的魂 @ 2020-07-10 20:13:51

呼,在第一个if判断里面,判断是否越界的条件写错了,应该是“w==m+2”,而不是“w==m+1”


|