码迷,mamicode.com
首页 > 其他好文 > 详细

Luogu P5098 Cave Cows 3

时间:2020-01-28 17:52:04      阅读:58      评论:0      收藏:0      [点我收藏+]

标签:pac   ace   name   splay   spl   ios   amp   最小值   main   

题目大意

??有\(n\)个坐标\((x_i, y_i)\),求任意两个坐标之间的最大曼哈顿距离。
??\(1 \leq n \leq 50000\)\(-1 \times 10^6 \leq x1,x2,y1,y2 \leq 1 \times 10^6\)
?

题解

??我们把\(|x_1 - x_2| + |y_1 - y_2|\)分成四种情况:
\[\begin{aligned} &|x_1 - x_2| + |y_1 - y_2| = x_1 - x_2 + y_1 - y_2 = (x_1 + y_1) - (x_2 + y_2) &(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) &(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)) &(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)) &(x_1 < x_2, y_1 < y_2) \end{aligned}\]
??显然分别求\(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;
}

Luogu P5098 Cave Cows 3

标签:pac   ace   name   splay   spl   ios   amp   最小值   main   

原文地址:https://www.cnblogs.com/kcn999/p/12238315.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!