Luogu P5098 Cave Cows 3

Subject to the effect

  There \ (n-\) coordinates \ ((x_i, y_i) \) , do the most Manhattan distance between any two coordinates.
  \ (. 1 \ n-Leq \ Leq 50000 \) , \ (-. 1 \ ^ 10. 6 Times \ Leq X1, X2, Y1, Y2 \ Leq. 1 \ ^ 10. 6 Times \) .

answer

  我们把\ (| x_1 - x_2 | + | y_1 - y_2 | \)分成四种情况:
\ [\ left \ {\ begin {matrix} | x_1 - x_2 | + | Y_1 - y_2 | = X_1 - y_1 + x_2 - y_2 = (x_1 + y_1) - (x_2 + y_2), \ color {blue} {(x_1 \ GEQ x_2, y_1 \ GEQ y_2)} \\ | x_1 - x_2 | + | Y_1 - y_2 | = X_1 - x_2 + y_2 - y_1 = (x_1 - y_1) - (x_2 - y_2), \ color {blue} {(x_1 \ GEQ x_2, y_1 <y_2)} \\ | x_1 - x_2 | + | Y_1 - y_2 | = X_2 - x_1 + y_1 - y_2 = - ((x_1 - y_1) - (x_2 - y_2)), \ color {blue} {(x_1 <x_2, y_1 \ GEQ y_2)} \\ | x_1 - x_2 | + | Y_1 - y_2 | = X_2 - x_1 + y_2 - y_1 = - ((x_1 + y_2) - (x_2 + y_2)), \ color {blue} {(x_1 <x_2, y_1 <y_2)} \ end {matrix} \ right \].
  分别求\ (x_i - y_i \)的最大值和最小值即可.

#include <iostream>
#include <cstdio>

using namespace std;

int n;
int x, y;
int maxp = -(1 << 30), minp = 1 << 30, maxd = -(1 << 30), mind = 1 << 30;

int main()
{
    scanf("%d", &n);
    for (int i = 1; i <= n; ++i)
    {
        scanf("%d%d", &x, &y);
        minp = min(minp, x + y);
        maxp = max(maxp, x + y);
        mind = min(mind, x - y);
        maxd = max(maxd, x - y);
    }
    printf("%d", max(max(maxp - minp, -(maxp - minp)), max(maxd - mind, -(maxd - mind))));
    return 0;
}

Guess you like

Origin www.cnblogs.com/kcn999/p/12238315.html