蒟蒻求助,二分查找WA

P1678 烦恼的高考志愿

Fraction @ 2018-08-19 17:04:35

RT,我找不出问题qwq

#include <bits/stdc++.h>
#define fp(i, l, r) for(register int i = (l); i <= (r); ++i)
#define fd(i, l, r) for(register int i = (l); i >= (r); --i)
#define ANTISYNC ios::sync_with_stdio(false)
#define full(a, b) memset(a, b, sizeof(a))
#define MAXN (int)1e6 + 5
#define ll long long
#define il inline
#define SAFE 4
using namespace std;

int n, m, ans = 0;
int school[MAXN], people[MAXN];

il int find(int num, int l, int r) {
    while(l < r) {
        int mid = (l+r) >> 1;
        if(abs(school[mid]-people[num]) > abs(school[mid+1]-people[num])) l = mid+1;
        else r = mid;
//      if(people[num] == school[mid]) return school[mid];
    }
    return school[l];
}

il int init() {
    scanf("%d%d", &m, &n);
    fp(i, 1, m) scanf("%d", &school[i]);
    fp(i, 1, n) scanf("%d", &people[i]);
    sort(school+1, school+1+m);
    fp(i, 1, n) {
        ans += abs(people[i] - find(i, 1, m));
    }
    printf("%d", ans);
    return 0;
}

int main() {
    init();
    return 0;
}

by Fraction @ 2018-08-19 17:22:21

求助啊qwq


by NuclearBaseACE @ 2018-08-19 17:24:50

。。。。


by Viston @ 2018-08-19 17:32:00

lowerbound了解一下


by Fraction @ 2018-08-19 18:59:14

@HNFMS__viston lower_bound怎么查,不是只能查大于等于的数吗,这个cmp我写不出


|