Radar Installation(贪心)

Radar Installation

Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 56826   Accepted: 12814

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<iostream>
 3 #include<algorithm>
 4 #include<math.h>
 5 using namespace std;
 6 struct island
 7 {
 8     double x , y ;
 9     double left , right ;
10 }a[2000];
11 int n ;
12 int d ;
13 bool cmp (island a , island b)
14 {
15     return a.x < b.x ;
16 }
17
18 int main ()
19 {
20   //  freopen ("a.txt" , "r" , stdin) ;
21     int cnt ;
22     int ans = 1 ;
23     bool flag ;
24
25     while (~ scanf ("%d%d" , &n , &d)) {
26             if (n == 0 && d == 0)
27                 break ;
28             flag = 0 ;
29
30         for (int i = 0 ; i < n ; i++) {
31             scanf ("%lf%lf" , &a[i].x , &a[i].y) ;
32             if (a[i].y > 1.0 * d || a[i].y < 0 || d <= 0) {
33                 flag = 1 ;
34             }
35             if (!flag) {
36                 a[i].left = (double) a[i].x - sqrt (1.0 * d * d - a[i].y * a[i].y) ;
37                 a[i].right = (double) a[i].x + sqrt (1.0 * d * d - a[i].y * a[i].y) ;
38             }
39         }
40         if (flag) {
41             printf ("Case %d: -1\n" , ans++) ;
42             continue ;
43         }
44         sort (a , a + n , cmp) ;
45         cnt = 1 ;
46         double l = a[0].left , r = a[0].right ;
47         for (int i = 1 ; i < n ; i++) {
48                     if (a[i].left > r) {
49                             cnt++ ;
50                             l = a[i].left ;
51                             r = a[i].right ;
52                         }
53                     else {
54                         l = a[i].left ;
55                         r = a[i].right < r ? a[i].right : r ;
56                     }
57                 }
58         printf ("Case %d: %d\n" , ans++ , cnt) ;
59     }
60     return 0 ;
61 }

别忘记每次都要跟新放radar的区间 ,orz

时间: 2024-08-10 21:14:09

Radar Installation(贪心)的相关文章

POJ 1328、Radar Installation 贪心

jQuery的属性操作非常简单,下面以一个a元素来说明属性的获取/设置/删除操作 <body> <a>jquery.com</a> </body> 添加属性 $('a').attr('href', 'http://www.jquery.com') 添加多个属性 $('a').attr({'href':'http://www.jquery.com', 'title':'jquery.com'}) 获取属性 $('a').attr('href') class属性

POJ 1328 Radar Installation 贪心题解

本题是贪心法题解,不过需要自己观察出规律,这就不容易了,很容易出错. 一般网上做法是找区间的方法. 这里给出一个独特的方法: 1 按照x轴大小排序 2 从最左边的点循环,首先找到最小x轴的圆 3 以这个圆判断可以包括右边的多少个圆,直到不可以包括下一个点,那么继续第2步,画一个新圆. 看代码吧,应该很清晰直观的了. 效率是O(n),虽然有嵌套循环,但是下标没有重复,一遍循环就可以了,故此是O(n). #include <stdio.h> #include <cmath> #incl

Poj 1328 Radar Installation 贪心

题目链接:http://poj.org/problem?id=1328 Radar Installation Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 52768   Accepted: 11867 Description Assume the coasting is an infinite straight line. Land is in one side of coasting, sea in the othe

[ACM] POJ 1328 Radar Installation (贪心,区间选点问题)

Radar Installation Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 51131   Accepted: 11481 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 loca

Radar Installation(贪心)

Radar Installation Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 54295   Accepted: 12208 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 loca

poj 1328 Radar Installation(贪心+快排)

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, s

Radar Installation(贪心,可以转化为今年暑假不ac类型)

Radar Installation Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 20000/10000K (Java/Other) Total Submission(s) : 54   Accepted Submission(s) : 28 Problem Description Assume the coasting is an infinite straight line. Land is in one side of co

POJ 1328 Radar Installation 贪心算法

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, s

【POJ1328】Radar Installation 贪心

题意: 有n个在x轴上方的小岛的坐标,还给出雷达的范围,现在要求在x轴上放尽量少的雷达使岛都被覆盖到,问最少数量. 题解: 贪心.确定每个岛在x轴上的映射范围(此范围内有雷达则能扫到小岛),然后按右界排序,然后类似于单调队列(当然要水多了),把雷达尽量往右放. 细节: 注意一: 雷达不用非得在整点上,别被示意图骗了. 注意二: 如果数据有误输出-1 一.某些点扫不到 二.某些点在x轴下面 三.d<0 代码: #include <cmath> #include <cstdio>