标签:
Radar Installation
Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u
Submit Status Practice OpenJ_Bailian 1328
Description
Assume the coasting is an infinite straight line. Land is in one side of coasting, sea in the other. Each small island is a point locating in the sea side. And any radar installation, locating on the coasting, can only cover d distance, so an island in the sea can be covered by a radius installation, if the distance between them is at most d.
We use Cartesian coordinate system, defining the coasting is the x-axis. The
sea side is above x-axis, and the land side below. Given the position of each
island in the sea, and given the distance of the coverage of the radar
installation, your task is to write a program to find the minimal number of
radar installations to cover all the islands. Note that the position of an
island is represented by its x-y coordinates.
Figure A Sample Input of Radar Installations
Input
The input consists of several test cases.
The first line of each case contains two integers n (1<=n<=1000) and d,
where n is the number of islands in the sea and d is the distance of coverage
of the radar installation. This is followed by n lines each containing two
integers representing the coordinate of the position of each island. Then a
blank line follows to separate the cases.
The input is terminated by a line containing pair of zeros
Output
For each test case output one line consisting of the test case number followed by the minimal number of radar installations needed. "-1" installation means no solution for that case.
Sample Input
3 2
1 2
-3 1
2 1
1 2
0 2
0 0
Sample Output
Case 1: 2
Case 2: 1
题意:
在一个坐标平面上有n个点,他们的y坐标都大于0。雷达可以覆盖范围为d的区域,问在y=0这条直线上至少设置多少个雷达,可以覆盖完所有给出的n个点。
输入:
多组数据,第一行是n和d,之后n行是每个点的x坐标与y坐标。
输出:
最少的雷达数。
分析:
首先进行转化,求出每一个点所对应的一段y=0上的区间,在这段区间上放置雷达将会覆盖这个点。然后再对这n个区间按照左端的从小到大进行排序。r_index变量表示当前所有区间的最右边界,如果下一个区间的左端点不比r_index大,则用该区间的有端点的值更新r_index = min(r_index,右端点的值)。如果下一个区间的左端点比r_index大,则ans++,并且它的右端点的值赋给r_index。按这样的方式遍历完所有区间就可以得出答案。
1 #include <cstdio> 2 #include <iostream> 3 #include <cstring> 4 #include <vector> 5 #include <string> 6 #include <algorithm> 7 #include <cmath> 8 using namespace std; 9 const int MAX_N = 1000; 10 struct scope{double l,r;}s[MAX_N + 10]; 11 bool cmp(const scope& s1,const scope& s2){ 12 return s1.l < s2.l; 13 } 14 int main(){ 15 int n,R; 16 int Case = 0; 17 while(scanf("%d%d",&n,&R),n + R){ 18 int ans = 1; 19 int x,y; 20 for(int i = 0 ; i < n ; i++){ 21 scanf("%d%d",&x,&y); 22 s[i].l = x - sqrt(R * R - y * y); 23 s[i].r = x + sqrt(R * R - y * y); 24 if(y > R || R <= 0) ans=-1; 25 } 26 sort(s,s + n,cmp); 27 double r_index = s[0].r; 28 for(int i = 1 ; i < n && ans != -1 ; i++){ 29 if(s[i].l > r_index){ 30 ans++; 31 r_index = s[i].r; 32 } 33 else if(s[i].r < r_index){ 34 r_index = s[i].r; 35 } 36 } 37 printf("Case %d: %d\n",++Case, ans); 38 } 39 return 0; 40 }
标签:
原文地址:http://www.cnblogs.com/cyb123456/p/5783288.html