UVaLive 4851 UVa 1468 - Restaurant (思维)

来源:互联网 发布:php函数手册 编辑:程序博客网 时间:2024/06/06 01:45

Restaurant

Time Limit:3000MS Memory Limit:Unknown 64bit IO Format:%lld & %llu

[Submit]  [Go Back]  [Status]  

Description

Download as PDF Mr. Kim is planning to open a new restaurant. His city is laid out as a grid with sizeMxM. Therefore, every road is horizontal or vertical and the horizontal roads (resp., the vertical roads) are numbered from 0 toM - 1. For profitability, all restaurants are located near road junctions. The city has two big apartments which are located on the same horizontal road. The figure below shows an example of a city map with size11 x 11. A circle represents an existing restaurant and a circle labeled with `A' or `B' represents the location of an apartment. Notice that a restaurant is already located at each apartment. Each road junction is represented by the coordinate of the ordered pair of a vertical road and a horizontal road. The distance between two locations(x1, y1) and (x2, y2) is computed as | x1 - x2| + | y1 - y2|. In the figure below, the coordinates of A and B are (0, 5) and (10, 5), respectively.
\epsfbox{p4851.eps}

Mr. Kim knows that the residents of the two apartments frequently have a meeting. So, he thinks that the best location of a new restaurant is halfway between two apartments. Considering lease expenses and existing restaurants, however, he can't select the optimal location unconditionally. Hence he decides to regard a location satisfying the following condition as agood place. Let dist(p,q) be the distance between p andq.

A location p is a good place if for each existing restaurant's location q,dist(p, A) < dist(q,A) or dist(p, B) < dist(q, B). In other words, p is not a good place if there exists an existing restaurant's locationq such that dist(p,A)$ \ge$dist(q,A) and dist(p, B)$ \ge$dist(q,B).

In the above figure, the location (7, 4) is a good place. But the locationp = (4, 6) is not good because there is no apartment which is closer top than the restaurant at q = (3, 5), i.e.,dist(p, A) = 5$ \ge$dist(q,A) = 3 and dist(p, B) = 7$ \ge$dist(q,B) = 7. Also, the location (0, 0) is not good due to the restaurant at(0, 5). Notice that the existing restaurants are positioned regardless of Mr. Kim's condition.

Given n locations of existing restaurants, write a program to compute the number of good places for a new restaurant.

Input

Your program is to read the input from standard input. The input consists of T test cases. The number of test cases T is given in the first line of the input. Each test case starts with a line containing two integersM and n ( 2$ \le$M$ \le$60, 000 and2$ \le$n$ \le$50, 000), which represent the size of a city map and the number of existing restaurants, respectively. The (i + 1)-th line of a test case contains two integers xi and yi (i = 1, 2,..., n and 0$ \le$xi,yi < M), which represents the coordinate of thei-th existing restaurant. Assume that all restaurants have distinct coordinates and that the two apartments A and B are positioned at the locations of 1-st restaurant and 2-nd restaurant. Notice that A and B are placed on the same horizontal line.

Output

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

The following shows sample input and output for two test cases.

Sample Input

26 31 34 30 211 110 510 54 92 87 85 63 55 33 27 29 1

Sample Output

216

Source

Daejeon 2010-2011


题意:

有一个M*M的网格,坐标[0...M-1,0...M-1] 网格里面有两个y坐标相同的宾馆A和B,以及n个餐厅,宾馆AB里面各有一个餐厅,编号1,2,其他餐厅编号3-n,现在你打算新开一家餐厅,需要考察一下可能的位置,一个位置p是“好位置”的条件是:当且仅当对于已有的每个餐厅q,要么p比q离A近,要么p比q离B近,即dist(p,A) < dist(q,A) 或者 dist(p,B) < dist(q,B)  问“好位置”的个数


思路:

首先能够肯定的是好位置在AB之间,所以就是考虑AB中间的每一列有多少位置是好位置

设A,B坐标为 (x1, y0) (x2, y0) (y坐标相同)

对于AB连线之间的一个位置 (xi, y0), 设他所在列的好位置个数为h[i]

则h[i] = min(abs(yj-y0), h[i-1]+1, h[i+1]+1) - 1; yj为i列上的所有餐厅的y坐标



#include <cstdio>#include <iostream>#include <vector>#include <algorithm>#include <cstring>#include <string>#include <map>#include <cmath>#include <queue>#include <set>using namespace std;//#define WIN#ifdef WINtypedef __int64 LL;#define iform "%I64d"#define oform "%I64d\n"#define oform1 "%I64d"#elsetypedef long long LL;#define iform "%lld"#define oform "%lld\n"#define oform1 "%lld"#endif#define S64I(a) scanf(iform, &(a))#define P64I(a) printf(oform, (a))#define P64I1(a) printf(oform1, (a))#define REP(i, n) for(int (i)=0; (i)<n; (i)++)#define REP1(i, n) for(int (i)=1; (i)<=(n); (i)++)#define FOR(i, s, t) for(int (i)=(s); (i)<=(t); (i)++)const int INF = 0x3f3f3f3f;const double eps = 10e-9;const double PI = (4.0*atan(1.0));const int maxm = 60000 + 20;int h[maxm];int main() {    int T;    scanf("%d", &T);    while(T--) {        int m, n;        scanf("%d%d", &m, &n);        int x1, y1, x2, y2;        scanf("%d%d%d%d", &x1, &y1, &x2, &y2);        if(x1 > x2) swap(x1, x2);        for(int i=x1; i<=x2; i++) h[i] = m;        for(int i=3; i<=n; i++) {            int x, y;            scanf("%d%d", &x, &y);            h[x] = min(h[x], abs(y-y1));        }        h[x1] = 0;        for(int i=x1+1; i<x2; i++) {            h[i] = min(h[i], h[i-1]+1);        }        h[x2] = 0;        for(int i=x2-1; i>x1; i--) {            h[i] = min(h[i], h[i+1]+1);        }        LL ans = 0;        for(int i=x1+1; i<x2; i++) if(h[i] > 0) {            ans++;            ans += min(y1, h[i]-1);            ans += min(m-y1, h[i]-1);        }        P64I(ans);    }    return 0;}







0 0