3142 曼哈顿的噪音
哈顿城正在施工!
城市里一共有n个施工点,施工点总是会发出莫名其妙的噪音,特别是当两个施工点距离足够远时还会发生神奇的反应,让噪音变得更大。
所以现在曼哈顿的区长请你来计算一下两两施工点之间曼哈顿距离的最大值是多少。
坐标为的施工点和坐标为
的施工点曼哈顿距离为|x2−x1|+|y2−y1|
输入
第一行一个整数n(n<=50000)
接下来n行,每行两个整数表示施工点的坐标。
坐标绝对值<=1e6
输出
一行一个整数表示最大的曼哈顿距离。
数据范围
对于30%的数据: n<=300
对于60%的数据: n<=3000
对于100%的数据: n<=50000
输入样例
5
1 1
3 5
2 7
8 1
4 4
输出样例
12
解析:
对于式子|x1−x2|+|y1−y2||x1−x2|+|y1−y2|,有以下四种情况:
1)x1−x2≥0,y1−y2≥0
|x1−x2|+|y1−y2|=x1−x2+y1−y2=(x1+y1)−(x2+y2)
2) x1−x2<0,y1−y2≥0x1−x2<0,y1−y2≥0
|x1−x2|+|y1−y2|=x2−x1+y1−y2=(x2−y2)−(x1−y1)
3) x1−x2≥0,y1−y2<0
|x1−x2|+|y1−y2|=x1−x2+y2−y1=(x1−y1)−(x2−y2)
4) x1−x2<0,y1−y2<0
|x1−x2|+|y1−y2|=x2−x1+y2−y1=(x2+y2)−(x1+y1)
所以就是要求max{max{xi+yi}−min{xi+yi},max{xi−yi}−min{xi−yi}}
放代码:
#include<iostream>
#include<cstdio>
#include <algorithm>
#include <cstring>
#include <string>
#include <queue>
#include <cmath>
#include <map>
#include <set>
#define debug cout<<0<<endl
#define ll long long
const int MAXN = 1e5 + 10;
const int MOD = 1e9 + 7;
using namespace std;
struct point {
int x, y;
} a[MAXN];
bool cmp1(point m, point n) {
return m.x > n.x;
}
bool cmp2(point m, point n) {
return m.y > n.y;
}
int main() {
int N;
cin >> N;
int INF = 1e9;
int maxxPlus = -INF;
int minnPlus = INF;
int maxxMins = -INF;
int minnMins = INF;
for (int i = 1,x,y; i <= N; i++) {
cin >> x >> y;
maxxPlus = max(maxxPlus, x+y);
minnPlus = min(minnPlus, x+y);
maxxMins = max(maxxMins, x-y);
minnMins = min(minnMins, x-y);
}
cout << max(maxxPlus-minnPlus, maxxMins-minnMins);
return 0;
}