x轴上的数要相邻,处理一下for (int i = 0; i < n; i ++ ) x[i] -= i;,然后正常就是每个数与中位数做差
C++ 代码
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
typedef long long LL;
const int N = 10010;
int n;
int x[N], y[N];
LL work(int q[])
{
sort(q, q + n);
LL res = 0;
for (int i = 0; i < n; i ++ )
{
res += abs(q[i] - q[n / 2]);
}
return res;
}
int main()
{
cin >> n;
for (int i = 0; i < n; i ++ )
{
scanf("%d%d", &x[i], &y[i]);
}
sort(x, x + n);
sort(y, y + n);
for (int i = 0; i < n; i ++ )
x[i] -= i;
cout << work(x) + work(y) << endl;
return 0;
}