UVaLive 4851 UVa 1468 - Restaurant (思维)
来源:互联网 发布:php函数手册 编辑:程序博客网 时间:2024/06/06 01:45
Restaurant
[Submit] [Go Back] [Status]
Description
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)dist(q,A) and dist(p, B)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) = 5dist(q,A) = 3 and dist(p, B) = 7dist(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 ( 2M60, 000 and2n50, 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 0xi,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
题意:
有一个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;}
- UVaLive 4851 UVa 1468 - Restaurant (思维)
- uvalive 4851 Restaurant(扫描法)
- UvaLive-4851-Restaurant
- UVALive - 4851 Restaurant
- UVALive - 4851 Restaurant 扫描
- UVA 1468 - Restaurant(推理)
- uva 1468 - Restaurant(贪心)
- UVa 1468 - Restaurant(贪心 + 扫描)
- UVA 1468 Restaurant ?
- HDOJ 4883 TIANKENG’s restaurant(思维)
- UVaLive LA 5052 UVa 1481 - Genome Evolution (很巧妙的思维)
- ACM: uva 1468 - Restaurant
- UVALive 6897Exploration(逆向思维)
- UVALive 6694 - Toy Boxes(思维+二分)
- 【UVALive 7364】Robots(逆向思维+搜索)
- UVALive 7417 思维
- UVALive 7339 Owllen【思维】
- UVALive-7601 Football(思维)
- android中sqlite中判断表或视图是否存在
- JS window.open()属性
- Error_code: 1236 log event entry exceeded max_allowed_packet 错误解决办法
- Android内存分析和调优
- HDOJ 4858 项目管理
- UVaLive 4851 UVa 1468 - Restaurant (思维)
- js兼容多浏览器的关闭当前页面
- Android中的定时器AlarmManager简介
- 【python】第五章——条件、循环和其他语句
- MATLAB入门学习笔记(一) 矩阵的生成及操作
- 常用网络命令(十二)brctl
- Android平台和java平台 DES、3DES、RSA加密解密互通程序及其不能互通的原因
- LeetCode Sum Root to Leaf Numbers
- 【Leetcode长征系列】Maximum Subarray