POJ 1328 Radar Installation(贪心)
2012-02-22 22:05
351 查看
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, 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
Sample Output
Source
Beijing 2002
解题报告:求出每个点的雷达所能安放的范围,再利用经典的活动安排的贪心算法即可
代码如下:
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 30071 | Accepted: 6607 |
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
Source
Beijing 2002
解题报告:求出每个点的雷达所能安放的范围,再利用经典的活动安排的贪心算法即可
代码如下:
#include <iostream> #include <cstring> #include <cstdio> #include <cstdlib> #include <cmath> using namespace std; const int N = 1010; struct point { double x; double y; }p ; //点的信息 struct node { double begin; double end; }a ; //雷达所能安放的位置范围 int visit ; int cmp(const void *a, const void *b)//排序函数 { return (*(struct node *)a).end > (*(struct node *)b).end ? 1 : -1; } double Max(double x, double y) { return x > y ? x : y; } int main() { int n, d, i, j, ans, count = 0; double max, len; while (scanf("%d%d", &n, &d) != EOF && n && d) { max = 0; memset(p, 0, sizeof(p)); for (i = 0; i < n; ++i) { scanf("%lf%lf", &p[i].x, &p[i].y); max = Max(max, p[i].y);//找出纵坐标的最大,以便比较和雷达扫描半径的大小 } getchar(); getchar(); count ++; if (max > d || d < 0) { printf("Case %d: -1\n", count); } else { memset(a, 0, sizeof(a)); memset(visit, 0, sizeof(visit)); for (i = 0; i < n; ++i) { len = sqrt(1.0 * d * d - 1.0 * p[i].y * p[i].y); a[i].begin = p[i].x - len; a[i].end = p[i].x + len; } qsort(a, n, sizeof(a[0]), cmp); ans = 0; for (i = 0; i < n; ++i) { if (!visit[i]) { visit[i] = 1; for (j = 0; j < n; ++j)//使用的是经典的活动安排的贪心算法 { if (!visit[j] && a[j].begin <= a[i].end) { visit[j] = 1; } } ans ++; } } printf("Case %d: %d\n", count, ans); } } return 0; }
相关文章推荐
- 贪心-poj-1328-Radar Installation
- Poj 1328 贪心之区间选点+勾股定理**
- POJ——1328(贪心)
- POJ 1328 - Radar Installation [贪心]
- 【占坑】POJ 1328做题笔记 #贪心#
- POJ 1328 Radar Installation(贪心)
- 文章标题 POJ 1328 : Radar Installation(贪心)
- POJ 1328(贪心)
- POJ 1328 Radar Installation 贪心
- POJ 1328 Radar Installation 区间贪心
- POJ 1328(Radar Installation 区间贪心)
- POJ 1328 Radar Installation(区间贪心)
- poj1328 Radar Installation(贪心)
- poj 1328 Radar Installation (贪心)
- poj 1328 Radar Installation(nyoj 287 Radar):贪心
- poj 1328 Radar Installation(贪心)
- POJ 1328 Radar Installation 贪心最少区间问题
- POJ1328 Radar Installation 贪心
- POJ 1328 Radar Installation(贪心)
- POJ 1328 - Radar Installation [贪心]