BZOJ3170: [Tjoi2013] squirrel Party (Chebyshev distance to Manhattan)

Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 1524 Solved: 803[Submit][Status][Discuss] ...
Description
Input
Output
Sample Input
Sample Output
HINT
Source
Time Limit: 10 Sec Memory Limit: 128 MB
Submit: 1524 Solved: 803
[Submit][Status][Discuss]

Description

There are N little squirrels. Their household uses a point x,y to express that the distance between the two points is defined as: the point (x,y) and the eight points around it, that is, four points up, down, left, right, and four points diagonally. The distance is 1. Now N squirrels have to go to a squirrels' house to ask for the shortest distance.

Input

The first line gives the number n, indicating how many squirrels there are. 0<=N<=10^5
Next N lines, each line gives x,y the coordinates of its home.
-10^9<=x,y<=10^9

Output

Indicates the distance and minimum for the party.

Sample Input

6
-4 -1
-1 -2
2 -4
0 2
0 3
5 -2

Sample Output

20

HINT

Source

emmm, the topic gives the Chebyshev distance. We need to convert it into Manhattan distance

For each point $(x, y) $in the coordinates, it is converted to Manhattan distance followed by $(\ frac, \frac)$

Then enumerate a point of $k $. What we need to calculate is $\ sum ^ n | x | K - x | + | y | K - y ||$

After brute force splitting, we found that prefixes and optimizations can be used

The code is very beautiful.

#include<cstdio> #include<algorithm> #define LL long long using namespace std; const int MAXN = 1e5 + 10; const LL INF = 1e18 + 10; inline int read() { char c = getchar();int x = 0,f = 1; while(c < '0' || c > '9'){if(c == '-')f = -1;c = getchar();} while(c >= '0' && c <= '9') return x * f; } LL N, x[MAXN], y[MAXN], sortx[MAXN], sorty[MAXN], sumx[MAXN], sumy[MAXN]; LL QueryX(int l, int r) { return sumx[r] - sumx[l - 1]; } LL QueryY(int l, int r) { return sumy[r] - sumy[l - 1]; } LL calc(LL k) { LL posx = lower_bound(sortx + 1, sortx + N + 1, x[k]) - sortx, posy = lower_bound(sorty + 1, sorty + N + 1, y[k]) - sorty; return posx * sortx[posx] - QueryX(1, posx) - (N - posx) * sortx[posx] + QueryX(posx + 1, N) + posy * sorty[posy] - QueryY(1, posy) - (N - posy) * sorty[posy] + QueryY(posy + 1, N); } int main() { #ifdef WIN32 freopen("a.in", "r", stdin); #endif N = read(); for(int i = 1; i <= N; i++) { int a = read(), b = read(); x[i] = sortx[i] = a + b, y[i] = sorty[i] = a - b; } sort(sortx + 1, sortx + N + 1); sort(sorty + 1, sorty + N + 1); for(int i = 1; i <= N; i++) sumx[i] = sumx[i - 1] + sortx[i], sumy[i] = sumy[i - 1] + sorty[i]; LL ans = INF; for(int i = 1; i <= N; i++) ans = min(ans, calc(i)); printf("%lld", ans >> 1); return 0; }

13 February 2020, 16:25 | Views: 9379

Add new comment

For adding a comment, please log in
or create account

0 comments