谁来帮帮我呀!

P1678 烦恼的高考志愿

AND_OR_NOT @ 2024-10-28 21:37:11

# include <bits/stdc++.h>

using namespace std;

int main()
{
    int n,m;
    cin >> m >> n;
    int a[n],b[m];
    for(int i = 0;i < m;i ++)
    {
        cin >> a[i];
    }
    for(int i = 0;i < n;i ++)
    {
        cin >> b[i];
    }
    sort(a,a + m);
    long long sum = 0;
    for(int i = 0;i < n;i ++)
    {
        int x = lower_bound(a,a + m,b[i]) - a;
        sum += min(abs(a[x] - b[i]),abs(a[x == 0 ? 0 : x - 1] - b[i]));
    }
    cout << sum;
    return 0;
}

by wangtianhao0609 @ 2024-10-28 21:38:37

最好用二分


by 3_14 @ 2024-11-20 20:45:07

@wangtianhao0609lower_bound不是二分吗?


by wangtianhao0609 @ 2024-11-20 22:07:22

@3_14 \ 我不知道


by wangtianhao0609 @ 2024-11-20 22:08:10

我最近 @ 怎么都没成功


|