Luogu P5098 Cave Cows 3
Posted kcn999
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Luogu P5098 Cave Cows 3相关的知识,希望对你有一定的参考价值。
题目大意
??有(n)个坐标((x_i, y_i)),求任意两个坐标之间的最大曼哈顿距离。
??(1 leq n leq 50000),(-1 imes 10^6 leq x1,x2,y1,y2 leq 1 imes 10^6)。
?
题解
??我们把(|x_1 - x_2| + |y_1 - y_2|)分成四种情况:
[egin{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的主要内容,如果未能解决你的问题,请参考以下文章
bzoj3381[Usaco2004 Open]Cave Cows 2 洞穴里的牛之二*
05:Cave Cows 1 ?????????????????????
bzoj3383[Usaco2004 Open]Cave Cows 4 洞穴里的牛之四*
Bzoj 3380: [Usaco2004 Open]Cave Cows 1 洞穴里的牛之一