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

在一个网格城市中,给定多个酒店位置和餐厅位置,任务是找到一个餐厅作为聚会地点,使得从任意一个酒店出发到达该餐厅的最大距离最小。本文提供了一种有效的方法来解决这个问题,并附带AC代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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 <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <string>
#include <algorithm>
#include <cmath>
#include <functional>
#include <stack>
#include <queue>
#include <vector>
#include <map>
#include <set>
//#include <bits/stdc++.h>
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;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值