codeforces 491B New York Hotel(贪心,数学)

题目链接

New York Hotel
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Think of New York as a rectangular grid consisting of N vertical avenues numerated from 1 to N and M horizontal streets numerated 1to MC 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.

Input

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 ≤ N1 ≤ 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.

Output

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.

Sample test(s)
input
10 10
2
1 1
3 3
2
1 10
4 4
output
6
2
题意:n条垂直的街道,从左到右标号为(1到n),m条水平的街道,从上到下标号为(1到m),第i条垂直的街道与第j条水平的街道的交点的坐标为(i,j)。有c个人,已知每个人的坐标,有h家餐厅,已知h家餐厅的坐标。现在要一家餐厅,使得离这家餐厅最远的人的距离最小。输出最小值和这家餐厅。(点(x,y)到点(i,j)的距离为abs(x-i)+abs(y-j))。

题解:先分别求出每个餐厅离它最远的人的距离。假设有一个餐厅的坐标为(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;
}




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值