(曼哈顿距离)CodeForces - 491B New York Hotel

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

Example

Input
10 10
2
1 1
3 3
2
1 10
4 4
Output
6
2

 

题意:

坐标系中n*m的范围 x [1,m] y[1,n] 初始有若干点。接下来给出若干个坐标,每次求初始给出的点到该点的所有曼哈顿距离(只能沿平行于坐标轴的方向走)中的最大值。在所有结果中求最小值,并输出对应编号。

 

思路:

利用曼哈顿距离的一个性质,对于基准点A,如果B,C在A同侧(即与A的横坐标之差、纵坐标之差正负值分别相同)。则B、C之间的曼哈顿距离等于A、B之间的曼哈顿距离与A、C之间的曼哈顿距离之差。

因此可以先取(0,0)为基准点(由于坐标的范围,显然都在(0,0)右上侧),预处理出距离(0,0)曼哈顿距离最小、最大的两个大小。这样对于每次给出的坐标,只需要计算其与基准点曼哈顿距离与之前的两个最值差的绝对值中大的一个即可。这些算出的结果中最小的即为所求。

 1 #include <iostream>
 2 #include <string>
 3 #include <algorithm>
 4 #include <cstring>
 5 #include <cstdio>
 6 #include <cmath>
 7 #include <queue>
 8 #include <set>
 9 #include <map>
10 #include <list>
11 #include <vector>
12 #include <stack>
13 #define mp make_pair
14 //#define P make_pair
15 #define MIN(a,b) (a>b?b:a)
16 //#define MAX(a,b) (a>b?a:b)
17 typedef long long ll;
18 typedef unsigned long long ull;
19 const int MAX=1e5+5;
20 const int MAX_V=1e9+1;
21 const int INF=2e9+500;
22 const double M=4e18;
23 using namespace std;
24 const int MOD=1e9+7;
25 typedef pair<ll,int> pii;
26 const double eps=0.000000001;
27 #define rank rankk
28 ll n,m,c,h;
29 ll x[5]={0,MAX_V};
30 ll dismin[5],dismax[5];
31 ll cal(ll x1,ll x2,ll y1,ll y2)
32 {
33     return abs(x1-x2)+abs(y1-y2);
34 }
35 int main()
36 {
37     ios::sync_with_stdio(false);
38     cin>>n>>m>>c;
39     fill(dismin,dismin+5,INF);
40     fill(dismax,dismax+5,-INF);
41     for(ll i=1;i<=c;i++)
42     {
43         ll p,q;
44         cin>>p>>q;
45         for(int j=0;j<2;j++)
46         {
47             dismin[j]=min(dismin[j],abs(p-x[j])+q);
48             dismax[j]=max(dismax[j],abs(p-x[j])+q);
49         }
50     }
51     cin>>h;ll an=INF,annum=0;
52     for(ll t=1;t<=h;t++)
53     {
54         ll p,q;
55         cin>>p>>q;
56         ll lin=0;
57         for(ll j=0;j<2;j++)
58         {
59             lin=max(lin,abs(cal(x[j],p,0,q)-dismin[j]));
60             lin=max(lin,abs(cal(x[j],p,0,q)-dismax[j]));
61         }
62         if(lin<an)
63         {
64             an=lin;annum=t;
65         }
66     }
67     cout<<an<<"\n"<<annum;
68     return 0;
69 }

 

转载于:https://www.cnblogs.com/quintessence/p/6993416.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值