Sicily 7973. Hot Dogs in Manhattan

7973. Hot Dogs in Manhattan

Constraints

Time Limit: 5 secs, Memory Limit: 256 MB

Description

The two friends Barack and Mitt have both decided to set up their own hot dog stand in Manhattan. They wish to find the two optimal locations for their stands.
First of all, they both want to put their stand at an intersection, since that gives them maximum exposure. Also, this being Manhattan, there are already quite a few stands in the city, also at intersections. If they put up a stand close to another (or each other’s) stand, they might not get that many customers. They would therefore like to put their stands as far from other stands as possible.
We model Manhattan as a finite square grid, consisting of w vertical streets and h horizontal streets. The vertical streets run from x = 0 to x = w - 1, while horizontal streets run from y = 0 to y = h - 1. All pairs of consecutive parallel streets are separated by the same distance, which we set as the unit distance. The distance between two intersections (x1, y1) and (x2, y2) is then given by |x1 - x2| + |y1 - y2|.
We indicate an intersection’s suitability by its privacy, which is the minimum of all distances from this intersection to all other hot dog stands. Barack and Mitt would like to find two intersections with the maximum amount of privacy, i.e. such that the smallest of the two privacies is as large as possible. Note that the privacy of Barack’s location can be determined by the distance to Mitt’s location and vice versa.

Input

On the first line one positive number: the number of test cases, at most 100. After that per test case:

  • one line with three space-separated integers n, w and h (0 <= n <= 1 000 and 2 <= w, h <=1 000): the number of hot dog stands already in place and the number of vertical and horizontal streets, respectively.
  • n lines, each with two space-separated integers xi and yi (0 <= xi < w and 0 <= yi < h): the intersection where the i-th hot dog stand is located.

All hot dog stands are at different intersections. At least two intersections do not contain a stand.

Output

Per test case:

  • one line with one integer: the maximum privacy that Barack and Mitt can both obtain.

Sample Input

3
1 4 4
0 1
6 6 6
0 0
1 1
2 2
3 3
4 4
5 5
2 8 3
0 0
7 0

Sample Output

4
5
3

Hint

These sample cases are illustrated below. Only for the first case, the optimal placement of the two new stands is given (indicated by the dashed outlines).

Problem Source

2013年每周一赛第五场暨校赛模拟赛III/BAPC 2012

// Problem#: 7973
// Submission#: 3593658
// The source code is licensed under Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License
// URI: http://creativecommons.org/licenses/by-nc-sa/3.0/
// All Copyright reserved by Informatic Lab of Sun Yat-sen University
// Solution to Hot Dogs in Manhattan
// Author: Thomas Beuman

// Time complexity: O(w*h*log(w+h))
// Memory: O(w*h)

// @EXPECTED_RESULTS@: CORRECT

/*
Solution outline:

First determine all current privacies by doing a "floodfill":
The Manhattan distance is the length of the shortest path along the grid.
Starting from all given points, use BFS find the shortest pathlength for all other points.

Do a binary search over the final answer:
Set a target privacy; consider all points that have this privacy or a larger one.
Now we want to see if there are two such points that are far enough apart.
For this, we only need to consider points which have the smallest/largest y-coordinate for each x-coordinate.
These are at most 2w such points; we can then try all pairs.
*/

#include <cstdio>
#include <cstring>
#include <cstdlib>
using namespace std;

int Privacy[1000][1000];
int BFSqueue[1000000][2];
int ToCheck[2000][2];

int main()
{ int cases, casenr, n, w, h, i, j, x, y, x2, y2, p, current, last, m;
  int lowerbound, upperbound, target;
  int dx[4] = {0, 0, 1, -1}, dy[4] = {1, -1, 0, 0};
  bool possible;
  scanf("%d", &cases);
  for (casenr = 1; casenr <= cases; casenr++)
  { scanf("%d %d %d", &n, &w, &h);
    memset(Privacy, 127, sizeof(Privacy)); // Set all privacies to "infinity"
    for (i = 0; i < n; i++)
    { scanf("%d %d", &x, &y);
      Privacy[x][y] = 0;
      BFSqueue[i][0] = x;
      BFSqueue[i][1] = y;
    }
    // Calculate all current privacies with BFS
    last = n;
    for (current = 0; current < last; current++)
    { x = BFSqueue[current][0];
      y = BFSqueue[current][1];
      p = Privacy[x][y];
      for (i = 0; i < 4; i++) // Check all neighboring points
      { x2 = x + dx[i];
        y2 = y + dy[i];
        if (x2 >= 0 && x2 < w && y2 >= 0 && y2 < h && Privacy[x2][y2] > p+1)
        { Privacy[x2][y2] = p+1;
          BFSqueue[last][0] = x2;
          BFSqueue[last][1] = y2;
          last++;
        }
      }
    }
    // Binary search over answer
    lowerbound = 1;
    upperbound = w+h-1;
    while (lowerbound + 1 < upperbound)
    { target = (lowerbound + upperbound) / 2;
      m = 0;
      // For every column, only consider the two points with highest/lowest y-coordinate
      for (x = 0; x < w; x++)
      { for (y = 0; y < h && Privacy[x][y] < target; y++);
        if (y < h)
        { ToCheck[m][0] = x;
          ToCheck[m][1] = y;
          m++;
          for (y = h-1; Privacy[x][y] < target; y--);
          ToCheck[m][0] = x;
          ToCheck[m][1] = y;
          m++;
        }
      }
      // Try every possible combination
      possible = false;
      for (i = 1; i < m; i++)
        for (j = 0; j < i; j++)
          if (abs(ToCheck[i][0] - ToCheck[j][0]) + abs(ToCheck[i][1] - ToCheck[j][1]) >= target)
            possible = true;
      if (possible)
        lowerbound = target;
      else
        upperbound = target;
    }
    printf("%d\n", lowerbound);
  }
  return 0;
}                                 


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值