ACM: uva 1468 - Restaurant

来源:互联网 发布:网络延迟多少ms算正常 编辑:程序博客网 时间:2024/06/05 10:39

Restaurant

Mr. Kim is planning to open a new restaurant. His city is laidout as a grid withsize M x M.Therefore, every road is horizontal or vertical and the horizontalroads (resp., the vertical roads) are numbered from 0to M - 1. Forprofitability, all restaurants are located near road junctions. Thecity has two big apartments which are located on the samehorizontal road. The figure below shows an example of a city mapwithsize 11 x 11.A circle represents an existing restaurant and a circle labeledwith `A' or `B' represents the location of an apartment. Noticethat a restaurant is already located at each apartment. Each roadjunction is represented by the coordinate of the ordered pair of avertical road and a horizontal road. The distance between twolocations (x1y1and (x2y2iscomputedas x1 x2|+| y1 y2|.In the figure below, the coordinates of A and Bare (0, 5) and(10, 5),respectively.

ACM: <wbr>uva <wbr>1468 <wbr>- <wbr>Restaurant

 

Mr. Kim knows that the residents of the two apartmentsfrequently have a meeting. So, he thinks that the best location ofa new restaurant is halfway between two apartments. Consideringlease expenses and existing restaurants, however, he can't selectthe optimal location unconditionally. Hence he decides to regard alocation satisfying the following condition asa good place.Let dist(pqbethe distancebetween p and q.

A location p isa good place if for each existingrestaurant'slocation qdist(pA)<<I>dist(qAor dist(pB)< dist(qB).In other words, p is not agood place if there exists an existing restaurant'slocation q suchthat dist(pA)dist(qAand dist(pB)dist(qB).

In the above figure, the location (7,4) is a good place. But thelocation p = (4,6) is not good because there is no apartment whichis closer to p than therestaurant at q = (3, 5),i.e., dist(p,A) = 5dist(qA) =3 and dist(pB)= 7 dist(qB) = 7. Also, thelocation (0, 0) is not good dueto the restaurant at (0, 5). Notice that theexisting restaurants are positioned regardless of Mr. Kim'scondition.

Given n locations ofexisting restaurants, write a program to compute the number of goodplaces for a new restaurant.

Input 

Your program is to read the input from standard input. The inputconsists of T test cases.The number of testcases T is given in thefirst line of the input. Each test case starts with a linecontaining twointegers M and n 2M 60, 000 and 2 n50, 000), which represent the size of a city map and the number ofexisting restaurants, respectively.The (i + 1)-th line of atest case contains twointegers xi and yi i =1,2,..., n and 0xiyi M),which represents the coordinate of the i-thexisting restaurant. Assume that all restaurants have distinctcoordinates and that the two apartments A and B are positioned atthe locations of 1-st restaurant and 2-nd restaurant. Notice that Aand B are placed on the same horizontal line.

Output 

Your program is to write to standard output. Print exactly oneline for each test case. Print the number of good places which canbe found in a given city map.

The following shows sample input and output for two testcases.

SampleInput 

2
6 3
1 3
4 3
0 2
11 11
0 5
10 5
4 9
2 8
7 8
5 6
3 5
5 3
3 2
7 2
9 1

SampleOutput 

2
16

题意:在一个M*M的矩形范围内, 有着A,B两间apartment, 有n-2间restaurant,现在要你新建一件restaurant,

     要求你的restaurant要么比其它的restaurant靠近A,要么比其它的restaurant靠近B. 距离是哈密顿距离.

    要你计算出有多少个位置适合间这样的一间restaurant.

 

解题思路:

    1. A,B在同一纵坐标, 横坐标的上下界是A,B的横坐标, 从左到右根据横坐标进行扫描, 找出每个横坐标

       出现的大于A,B纵坐标的做小纵坐标和小于A,B纵坐标的最大纵坐标.(靠近AB直线).

    2. 从两个方向一次扫描合法的区域, 统计合法位置数量即可.

 

代码:

#include <cstdio>
#include <iostream>
#include <cstring>
using namespace std;
#define min(a,b) (a)<(b)?(a):(b)
#define max(a,b) (a)>(b)?(a):(b)
#define MAX 60005
typedef long long ll;
const int INF = (1<<29);

int M, n;
int y[MAX], miny[MAX], maxy[MAX];
int xa, ya, xb, yb;

inline void swap(int &a, int&b)
{
 int temp = a;
 a = b;
 b = temp;
}

int main()
{
// freopen("input.txt", "r", stdin);
 int caseNum, i;
 scanf("%d", &caseNum);
 while(caseNum--)
 {
  scanf("%d %d",&M, &n);
  scanf("%d %d %d %d",&xa, &ya, &xb,&yb);
  if(xa > xb)swap(xa, xb);

  for(i = xa+1; i< xb; ++i)
  {
   miny[i] =INF;
   maxy[i] =-INF;
  }

  int tx, ty;
  for(i = 2; i <n; ++i)
  {
   scanf("%d%d", &tx, &ty);
   if(ty>= ya) miny[tx] = min(ty, miny[tx]);
   if(ty<= ya) maxy[tx] = max(ty, maxy[tx]);
  }

  y[xa] = 0;
  for(i = xa+1; i< xb; ++i)
   y[i] =min(miny[i]-ya, ya-maxy[i]);
  for(i = xa+1; i< xb; ++i)
   y[i] =min(y[i-1]+1, y[i]);
  y[xb] = 0;
  for(i = xb-1; i> xa; --i)
   y[i] =min(y[i+1]+1, y[i]);

  ll ans = 0;
  for(i = xa+1; i< xb; ++i)
  {
   if(y[i])
   {
    ans++;
    ans+= min(y[i]-1, M-1-ya);
    ans+= min(y[i]-1, ya);
   }
  }

  printf("%lld\n", ans);
 }

 return 0;
}

0 0
原创粉丝点击