改不动了,要崩溃了

P1678 烦恼的高考志愿

Play_CP_4fun @ 2022-09-10 23:32:52

/*
Author: SJ

general
1.shuzu buyao kai xiaole
2.0 1 zhe liangge tepan
3.dfs jide biaoji qidian

str
1.yongle s.substr(), jide kanyixiashibushikeyi yigeyige shuchu

think twice, code once.

*/
#include <bits/stdc++.h>
const int N = 1e5 + 10;
const int INF = 1e9;
using ll = long long;

int n, m, a[100010];
ll ans;
void bsearch(int x) {
    int l = 0, r = n - 1, mark = 0;
    while (l <= r) {
        int mid = l + r >> 1;
        if (a[mid] >= x) {
            mark = mid;
            r = mid - 1;
        } else l = mid + 1;
    }
    int y = std::abs(a[mark] - x), z = std::abs(a[mark - 1] - x);
    ans += std::min(y, z);
}
int main() {
    std::ios::sync_with_stdio(false);
    std::cin.tie(nullptr);

    std::cin >> n >> m;
    for (int i = 0; i < n; i++) std::cin >> a[i];
    std::sort(a, a + n);
    for (int i = 0; i < m; i++) {
        int b;
        std::cin >> b;
        bsearch(b);
    }
    std::cout << ans;
    return 0;
}

by Eydte @ 2022-09-10 23:45:17

你二分的while括号里好像有问题


by Play_CP_4fun @ 2022-09-10 23:51:18

@_EDC 调出来了,二分是没有问题的,我这个是记录答案的写法,是在加法的时候没有考虑三种情况。

二分有问题一般都会tle吧我想


|