zoukankan      html  css  js  c++  java
  • (曼哈顿距离)CodeForces

    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<<"
    "<<annum;
    68     return 0;
    69 }
  • 相关阅读:
    各种排序算法的时间复杂度
    svn版本管理系统出现的问题解决办法
    算法时间复杂度
    js处理时间戳显示的问题
    cache缓存的BUG
    使用phpstorm提交svn代码版本管理系统遇到的问题解决办法
    20161101.20161115这两周的开发总结
    mac 上安装 redis
    终极 shell zsh
    在 mac 上利用 homebrew 安装软件
  • 原文地址:https://www.cnblogs.com/quintessence/p/6993416.html
Copyright © 2011-2022 走看看