Radar Installation(POJ 1328 区间贪心)

Radar Installation
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 68578   Accepted: 15368

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

需要判断d<0,a[i].y>d情况。
首先,按照x坐标排序,对于每个岛屿求出雷达所能放置的区间,然后对这些进行处理,x1,x2;
设当前雷达放置位置为nowx,对于下一个区间,如果写x1>nowx,显然多需要一个雷达,反之如果nowx>x1,nowx=min(nowx,x2);

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <algorithm>
 4 #include <cstring>
 5 #include <cmath>
 6 using namespace std;
 7 struct node
 8 {
 9     int x,y;
10 }a[1000+5];
11 bool cmp(node q,node p)
12 {
13     if(q.x==p.x)
14         return q.y>=p.y;
15     return q.x<p.x;
16 }
17 int main()
18 {
19     int n,d;
20     int i,j;
21     int k=1;
22     freopen("in.txt","r",stdin);
23     while(scanf("%d%d",&n,&d))
24     {
25         int coun=1;
26         if(n==0&&d==0)
27             break;
28         bool flag=0;
29         for(i=0;i<n;i++)
30         {
31             scanf("%d%d",&a[i].x,&a[i].y);
32             if(a[i].y>d)
33                 flag=1;
34         }
35         if(flag||d<=0)
36         {
37             printf("Case %d: -1
",k++);
38             continue;
39         }
40         sort(a,a+n,cmp);
41         double nowx=sqrt(double(d*d-a[0].y*a[0].y))+a[0].x;
42         double x1,x2,temp;
43         for(i=1;i<n;i++)
44         {
45             temp=sqrt(double(d*d-a[i].y*a[i].y));
46             x1=a[i].x-temp;
47             x2=a[i].x+temp;
48             if(x1>nowx)
49             {
50                 nowx=x2;
51                 coun++;
52             }
53             else if(nowx>x2)
54                 nowx=x2;
55         }
56         printf("Case %d: %d
",k++,coun);
57     }
58 }
原文地址:https://www.cnblogs.com/a1225234/p/5165532.html