1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
| #include <bits/stdc++.h> using namespace std; using ll = long long; const int N = 1e5 + 5; const ll LINF = 1e18 + 5; int n, x[N], y[N], d; ll U, D, L, R, prex[N], prey[N]; ll f(ll a, ll b) { int p = upper_bound(x+1, x+n+1, a) - x - 1, q = upper_bound(y+1, y+n+1, b) - y - 1; return a*p - prex[p] + b*q - prey[q] - (n-p)*a + (prex[n] - prex[p]) - (n-q)*b + (prey[n] - prey[q]); } ll cal(ll x) { ll l = max(x-U, L-x), r = min(x-D, R-x), ans = LINF; while(l<=r) { ll mid = (2*l+r)/3, mmid = (2*r+l+2)/3; ll fmid = f(x, mid), fmmid = f(x, mmid); if(fmid<=fmmid) { ans = min(ans, fmid); r = mmid - 1; } else { ans = min(ans, fmmid); l = mid + 1; } } return ans; } int main() { while(~scanf("%d", &n)) { for(int i=1; i<=n; i++) scanf("%d%d", x+i, y+i); scanf("%d", &d); U = LINF, D = -LINF, L = -LINF, R = LINF; for(int i=1; i<=n; i++) { ll cx = x[i] + y[i], cy = x[i] - y[i]; U = min(U, cy+d); D = max(D, cy-d); L = max(L, cx-d); R = min(R, cx+d); } if(D>U||L>R) { puts("impossible"); continue; } sort(x+1, x+n+1); sort(y+1, y+n+1); for(int i=1; i<=n; i++) prex[i] = prex[i-1] + x[i], prey[i] = prey[i-1] + y[i]; ll l = (L+D)/2, r = (R+U)/2, ans = LINF; while(l<=r) { ll mid = (2*l+r)/3, mmid = (2*r+l+2)/3; ll fmid = cal(mid), fmmid = cal(mmid); if(fmid<=fmmid) { ans = min(ans, fmid); r = mmid - 1; } else { ans = min(ans, fmmid); l = mid + 1; } } printf("%lld\n", ans); } return 0; }
|