Radar Installation 贪心

Language:
Radar Installation
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 42461   Accepted: 9409

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
 1 #include<stdio.h>
 2 #include<string.h>
 3 #include<math.h>
 4 #include<algorithm>
 5 #include<iostream>
 6 #include<stack>
 7 #include<math.h>
 8 using namespace std;
 9 
10 struct node
11 {
12     double start;
13     double end;
14 }coor[1001];//记录每个区间的端点
15 int cmp(const struct node a,const struct node b)
16 {
17     return a.start < b.start;
18 }
19 int t,r;
20 stack <node> st;//用栈存每个区间,
21 
22 int cal(int ans)
23 {
24     while(!st.empty())
25         st.pop();
26     for(int i = t-1; i >= 0; i--)
27         st.push(coor[i]);
28     while(st.size() >= 2)//当栈中至少存在两个区间时
29     {
30         struct node tmp1 = st.top();
31         st.pop(); 
32         struct node tmp2 = st.top();
33         if(tmp1.end >= tmp2.start)//当取出的两个区间有公共部分时
34         {
35             st.pop();//tmp2出栈
36             struct node tmp;
37             tmp.start = max(tmp1.start, tmp2.start);//注意取公共部分时,起始点取较大者
38             tmp.end = min(tmp1.end, tmp2.end);//终点取较小者
39             st.push(tmp);//将公共部分入栈
40             ans--;//每两个区间交一次,雷达个数减一次
41         }
42     }
43     return ans;
44 }
45 int main()
46 {
47 
48     int cor_x[1001],cor_y[1001];
49     double add;
50     int cnt = 1;
51     while(~scanf("%d %d",&t,&r))
52     {
53         int ok = 1;//判断小岛的坐标是否合法,
54         if(t == 0 && r == 0) break;
55         for(int i = 0; i < t; i++)
56         {
57             scanf("%d %d",&cor_x[i],&cor_y[i]);
58             if(cor_y[i] > r)//若小岛纵坐标大于半径则不合法
59                 ok = 0;
60         }
61         if(ok == 0)
62         {
63             printf("Case %d: -1
", cnt++);
64             continue;
65         }
66         for(int i = 0; i < t; i++)
67         {
68             //以每个小岛为圆心,r为半径画圆,coor[]存该圆与x轴相交的区间
69             add = sqrt(r*r-cor_y[i]*cor_y[i]);
70             coor[i].start = cor_x[i] - add;
71             coor[i].end = cor_x[i] + add;
72         }
73         sort(coor,coor+t,cmp);//对这些区间按起始点从小到大排序
74         int ans = t;
75         ans = cal(ans);
76         printf("Case %d: %d
",cnt++,ans);
77     }
78     return 0;
79 }
View Code
原文地址:https://www.cnblogs.com/LK1994/p/3232552.html