UVA 1468
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 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)dist(q, A) and dist(p, B)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) = 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 integers M and n ( 2M60, 000 and 2n50, 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 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
题意:
在一个m*m方格的地图上有n个餐馆,其中有最左和最右两个公寓a,b,公寓里面也有一个餐馆,现在你需要确定有多少个好位置,好位置的标准是没有其他的餐馆会比这个位置离a近同时也离b近
思路:
加入已经有一个餐馆(x,y),那么同x的其他点只要离a的y相对距离更近,那么这些点就是可行的,再考虑到已经存在的所有餐馆都会对你新选的地址造成影响,所以你可以假设位置,比如存在(x-1,y)那么你可以把它假定为存在一点f(x-1)+1是对右边造成的影响
#include <iostream> #include <algorithm> #include <cmath> #include <cstdio> #define maxn 70000 using namespace std; struct node { int x,y; }a,b,c; int h; int f[maxn]; const int INF=1e9; int main() { int m,n; int T; cin>>T; while(T--) { fill(f,f+maxn,INF); cin>>m>>n; cin>>a.x>>a.y>>b.x>>b.y; h=b.y; for(int i=0;i<n-2;i++) { cin>>c.x>>c.y; f[c.x]=min(f[c.x],abs(c.y-h)); } f[a.x]=f[b.x]=0;int ans=0; for(int i=a.x+1;i<b.x;i++) f[i]=min(f[i],f[i-1]+1); for(int j=b.x-1;j>a.x;j--) f[j]=min(f[j],f[j+1]+1); for(int k=a.x+1;k<b.x;k++) if(f[k]!=0) { if(f[k]-1<=h&&f[k]-1<=m-h-1) ans+=f[k]*2-2; else { if(f[k]-1<=h&&f[k]-1>=m-h-1) ans+=f[k]+m-h-1; else if(f[k]-1<=m-h-1&&f[k]-1>=h) ans+=f[k]+h-1; else ans+=m-1; } ans++; } cout<<ans<<endl; } return 0; }