UVaLive 4851 UVa 1468 - Restaurant (思维)

本文介绍了一种基于城市网格地图的餐厅选址优化算法,通过计算距离和比较现有餐厅位置,确定了理想的新餐厅位置数量。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

[]  [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 size MxM. Therefore, every road is horizontal or vertical and the horizontal roads (resp., the vertical roads) are numbered from 0 to M - 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 size 11 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 a good place. Let dist(p, q) be the distance between p and q.

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 location q 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 location p = (4, 6) is not good because there is no apartment which is closer to p 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 integers M and n ( 2$ \le$M$ \le$60, 000 and 2$ \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 the i-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

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

Sample Output

2
16

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 WIN
typedef __int64 LL;
#define iform "%I64d"
#define oform "%I64d\n"
#define oform1 "%I64d"
#else
typedef 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;
}







评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值