Codeforces 491B. New York Hotel 【枚举】【曼哈顿距离】

Description

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

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 and y (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.

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.

Example

Input

10 10
2
1 1
3 3
2
1 10
4 4

Output

6
2

题意:

一个二维网状格点,先给一个n,然后有n个坐标,表示最开始n个人的坐标,然后给一个m,然后有m个坐标,现在要从m个坐标中选出一个坐标,使得这个坐标到n个坐标的最大距离 最小,输出最大距离最小是多少,以及最后选出的是m个点中的哪一个。

思路:

首先,很明显答案应该是min{|N[i].x-M[j].x|+|N[i].y-M[j].y|},1<=i<=n,1<=j<=m
问题是,我们如何能快速求出,对于点(M[j].x,M[j].y),距离它最远的距离是多少
其实我们能够发现,|N[i].x-M[j].x|+|N[i].y-M[j].y|有4种去绝对值的方式。
所以我们直接维护4种取了绝对值的最大值,然后对于某次查询,直接取最大的。

ac代码:

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
//#include 
using namespace std;
typedef long long ll;
const int maxn = 550;
const int MOD = 1e9 + 7;
const double eps = 1e-8;
const ll INF = 0x3f3f3f3f;
ll maxs(ll x1, ll x2) {
    return (x1 > x2) ? x1 : x2;
}
//最大距离尽可能小
int main()
{   //两点间距离存在4种情况(x1-x2, y1-y2), (x1-x2, y2-y1), (x2-x1, y1-y2), (x2-x1, y2-y1)
    int n, m; scanf("%d %d", &n, &m);
    int hotel, restaurant;
    scanf("%d", &hotel);
    ll tmp1, tmp2;
    int first = 1;
    ll ff, zz, fz, zf;
    for (int i = 0; i < hotel; ++i)
    {
        scanf("%lld %lld", &tmp1, &tmp2);
        if (first)
        {
            ff = -tmp1 - tmp2;
            zz = tmp1 + tmp2;
            fz = -tmp1 + tmp2;
            zf = tmp1 - tmp2;
            first = 0;
        }
        else
        {
            ff = maxs(ff, -tmp1 - tmp2);
            zz = maxs(zz, tmp1 + tmp2);
            fz = maxs(fz, -tmp1 + tmp2);
            zf = maxs(zf, tmp1 - tmp2);
        }
    }
    scanf("%d", &restaurant);
    ll result = INF << 32;
    int flag;
    for (int i = 0; i < restaurant; ++i)
    {
        scanf("%lld %lld", &tmp1, &tmp2);
        ll tmp = maxs(maxs(tmp1 + tmp2 + ff, tmp1 - tmp2 + fz), maxs(-tmp1 + tmp2 + zf, -tmp1 - tmp2 + zz));
        if (tmp < result)
        {
            result = tmp;
            flag = i + 1;
        }
    }
    printf("%lld\n%d\n", result, flag);
    return 0;
}

你可能感兴趣的:(枚举暴力,思维)