hdu 5626 Clarke and points (曼哈顿距离变换,拆点)
http://acm.split.hdu.edu.cn/showproblem.php?pid=5626
题意:给出n(1E6)个点的二维坐标,问距离最远的两个点的距离是多少。
思路:对曼哈顿距离进行变换。
先看曼哈顿距离的定义
|x1−x2|+|y1−y2|
拆绝对值
x1−x2+y1−y2或x1−x2+y2−y1
x2−x1+y1−y2或x2−x1+y2−y1
即|x1+y1−(x2+y2)|或|x1−y1−(x2−y2)|
设x1+y1为x′,x1−y1为y′
则|x1′−x2′|或|y1′−y2′|
所以原要求1转化为
max(|x1′−x2′|,|y1′−y2′|)<=c
然后分别对x,y排序即可..最大的距离一定是y[n-1]-y[0]或者x[n-1]-x[0]
/* ***********************************************
Author :111qqz
Created Time :2016年02月24日 星期三 23时42分15秒
File Name :code/hdu/5626.cpp
************************************************ */
1#include <cstdio>
2#include <cstring>
3#include <iostream>
4#include <algorithm>
5#include <vector>
6#include <queue>
7#include <set>
8#include <map>
9#include <string>
10#include <cmath>
11#include <cstdlib>
12#include <ctime>
13#define fst first
14#define sec second
15#define lson l,m,rt<<1
16#define rson m+1,r,rt<<1|1
17#define ms(a,x) memset(a,x,sizeof(a))
18typedef long long LL;
19#define pi pair < int ,int >
20#define MP make_pair
1using namespace std;
2const double eps = 1E-8;
3const int dx4[4]={1,0,0,-1};
4const int dy4[4]={0,-1,1,0};
5const int inf = 0x3f3f3f3f;
6const int N=1E6+7;
7LL seed;
8int n;
9int x[N],y[N];
1inline LL rand(LL l,LL r)
2{
3 static LL mo =1E9+7,g=78125;
4 return l+((seed*=g)%=mo)%(r-l+1);
5}
6int main()
7{
8 #ifndef ONLINE_JUDGE
9 freopen("code/in.txt","r",stdin);
10 #endif
11 ios::sync_with_stdio(false);
12 int T;
13 cin>>T;
14 while (T--)
15 {
16 cin>>n>>seed;
17 for ( int i = 0 ; i < n ; i++)
18 {
19 int xx,yy;
20 xx = rand(-1000000000,1000000000);
21 yy = rand(-1000000000,1000000000);
22 x[i] = xx-yy;
23 y[i] = xx+yy;
24 }
25 sort(x,x+n);
26 sort(y,y+n);
27 int ans = -1;
28 ans = max(ans,y[n-1]-y[0]);
29 ans = max(ans,x[n-1]-x[0]);
30 cout<<ans<<endl;
31 }
1 #ifndef ONLINE_JUDGE
2 fclose(stdin);
3 #endif
4 return 0;
5}