Solution

cookiebus 2023-04-07 22:36:56 41 返回题目

小学奥数题,签到题,分别取 的中位数即可,显然距离和最近,然后算一下总和

#include <iostream>
#include <cstdio>
#include <string>
#include <algorithm>
using namespace std;
int n, ans;
struct nod {
    int x, y;
} v[100005];
bool cmpx(nod a, nod b) { return a.x < b.x; }
bool cmpy(nod a, nod b) { return a.y < b.y; }
int main() {
    cin >> n;
    for (int i = 0; i < n; i++) cin >> v[i].x >> v[i].y;
    sort(v, v + n, cmpx);
    int xx = v[n / 2].x;
    sort(v, v + n, cmpy);
    int yy = v[n / 2].y;
    for (int i = 0; i < n; i++) {
        ans += abs(v[i].x - xx);
        ans += abs(v[i].y - yy);
    }
    cout << ans << endl;
    return 0;
}
{{ vote && vote.total.up }}