题目链接
Think of New York as a rectangular grid consisting of N vertical avenues numerated from 1 to N and M horizontal streets numerated 1to M. C friends are staying at C hotels located at some street-avenue crossings. They are going to celebrate birthday of one of them in the one of H restaurants also located at some street-avenue crossings. They also want that the maximum distance covered by one of them while traveling to the restaurant to be minimum possible. Help friends choose optimal restaurant for a celebration.
Suppose that the distance between neighboring crossings are all the same equal to one kilometer.
The first line contains two integers N и M — size of the city (1 ≤ N, M ≤ 109). In the next line there is a single integer C (1 ≤ C ≤ 105) — the number of hotels friends stayed at. Following C lines contain descriptions of hotels, each consisting of two coordinates x andy (1 ≤ x ≤ N, 1 ≤ y ≤ M). The next line contains an integer H — the number of restaurants (1 ≤ H ≤ 105). Following H lines contain descriptions of restaurants in the same format.
Several restaurants and hotels may be located near the same crossing.
In the first line output the optimal distance. In the next line output index of a restaurant that produces this optimal distance. If there are several possibilities, you are allowed to output any of them.
10 10 2 1 1 3 3 2 1 10 4 4
6 2
题解:先分别求出每个餐厅离它最远的人的距离。假设有一个餐厅的坐标为(a,b)。
那么在这个餐厅左上角的人到它的距离为(a-x+b-y);
在这个餐厅右上角的人到它的距离为(a-x+y-b);
在这个餐厅左下角的人到它的距离为(x-a+b-y);
在这个餐厅右下角的人到它的距离为(x-a+b-y);
分别求出它左上角的点(-x-y)的最大值,它右上角的点(-x+y)的最大值,它左下角的点(x-y)的最大值,它右下角的点(x+y)的最大值,就可以求出离它最远的点的距离。
但是餐厅这么多,分别求每个餐厅的四个角的最大值显然是不现实的。
其实我们可以忽略四个象限的限制,求出所有的点的(-x-y)的最大值,(-x+y)的最大值,(x-y)的最大值,(x+y)的最大值,这样是不会影响最后的答案的。
我们把左上角,右上角,左下角,右下角分别看成第一,二,三,四象限。一,三象限的点之间是没有影响的,二、四象限的点之间也是没有影响的。其余象限之间的点会有影响,但是不会影响最后的答案。
下面我来证明一下,以三,四象限为例:
设餐厅的坐标为(a,b)
假设第四象限有一个点(x1,y1),第三象限有一个点(x2,y2)。
若x2+y2>x1+y1
这时第三象限的点影响了第四象限的值,但是最后的答案是不会受到影响的。
第四象限的点的值为:x1-a+y1-b
第三象限的点的值为:x2-a+b-y2
(x1-a+y1-b)-(x2-a+b-y2)=(x1+y1)-(x2+y2)+2*y2-2*b;
因为(x1+y1)-(x2+y2)<0且 y2-b<0
所以上面那么式子是小于0的,所以(x2-a+b-y2)>(x1-a+y1-b)
所以最后的答案没有受到影响。
代码如下:
#include<stdio.h> #include<iostream> #include<string.h> #include<algorithm> #include<string> #include<stack> #include<math.h> #include<vector> #include<set> #include<map> #define nn 110000 #define inff 0x7fffffff #define eps 1e-8 #define mod 1000000007 typedef long long LL; const LL inf64=LL(inff)*inff; using namespace std; int n,m,c,h; int main() { int i; int x,y; while(scanf("%d%d",&n,&m)!=EOF) { int a1,a2,a3,a4; a1=a2=a3=a4=-inff; scanf("%d",&c); for(i=1;i<=c;i++) { scanf("%d%d",&x,&y); a1=max(a1,-x-y),a2=max(a2,-x+y); a3=max(a3,x-y),a4=max(a4,x+y); } int ans=inff; int id; scanf("%d",&h); for(i=1;i<=h;i++) { scanf("%d%d",&x,&y); int ix=max(x+y+a1,x-y+a2); int iy=max(-x+y+a3,-x-y+a4); if(max(ix,iy)<ans) { ans=max(ix,iy); id=i; } } printf("%d\n",ans); printf("%d\n",id); } return 0; }