标签:+= wing www its stdin ifd ace pre freopen
https://www.acwing.com/problem/content/125/
一开始觉得很复杂,但是其实和去中点一样,只不过对于x来说要排个序,然后贪心让最近的士兵去他应该去的位置,这样不会更差。
所以xy是无关的,总是存在一种办法使得他们不会走到禁止格。
直接排序。
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int x[10005], y[10005];
int main() {
#ifdef Yinku
freopen("Yinku.in", "r", stdin);
#endif // Yinku
int n;
scanf("%d", &n);
for(int i = 1; i <= n; ++i)
scanf("%d%d", &x[i], &y[i]);
sort(x + 1, x + 1 + n);
for(int i = 1; i <= n; ++i)
x[i] -= i;
sort(x + 1, x + 1 + n);
sort(y + 1, y + 1 + n);
int xmid = x[(n + 1) / 2];
int ymid = y[(n + 1) / 2];
ll sum = 0;
for(int i = 1; i <= n; ++i) {
sum += abs(xmid - x[i]);
sum += abs(ymid - y[i]);
}
printf("%lld\n", sum);
}
标签:+= wing www its stdin ifd ace pre freopen
原文地址:https://www.cnblogs.com/Inko/p/11515937.html