poj 1328 Radar Installation (逆思维+贪心)
来源:互联网 发布:中国纯爱电影知乎 编辑:程序博客网 时间:2024/06/03 20:07
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
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
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
The input is terminated by a line containing pair of zeros
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.
3 21 2-3 12 11 20 20 0
Case 1: 2Case 2: 1
大致题意:将一条海岸线看成X轴,X轴上面是大海,海上有若干岛屿,给出雷达的覆盖半径和岛屿的位置,要求在海岸线上建雷达,在雷达能够覆盖全部岛屿情况下,求雷达的最少使用量。
思路:
以每个小岛做圆心,半径为d,交在线上,用一个区间表示,这一段的雷达都有影响到这个小岛,逆向思维。然后就是贪心找这一堆线段,最少几个点可以覆盖到所有线段,按照右端点排序从小到大,枚举每个区间,这个区间后面的区间的所有右端点一定比这个区间的右端点大,然后枚举所有他后面的区间,如果后面的左端点小于这个区间的右端点,说明这个区间有交集,用一个雷达就行,把之前合在一起的都标记掉。
#include <iostream>#include <cstring>#include <algorithm>#include <cstdio>#include <cmath>using namespace std;const int maxn = 1e3 + 5;int n, d, book[maxn];struct node{ int l, r;}a[maxn];int cmp(node a, node b){return a.r < b.r;}int main(){int n, k, ca = 1;while(~scanf("%d%d", &n, &d), n+k){memset(book, 0, sizeof(book));int x, y, flag = 0;for(int i = 1; i <= n; i++){scanf("%d%d", &x, &y);if(y > d) flag = 1;else{int len = sqrt(d*d - y*y);a[i].l = x - len;a[i].r = x + len;}}if(flag) {cout << -1 << endl;continue;}sort(a+1, a+1+n, cmp);int ans = 0;for(int i = 1; i <= n; i++){if(book[i]) continue;book[i] = 1;for(int j = i+1; j <= n; j++){if(a[j].l <= a[i].r)book[j] = 1;}ans++;}printf("Case %d: %d\n", ca++, ans);}return 0;}
阅读全文
0 0
- poj 1328 Radar Installation (逆思维+贪心)
- POJ 1328 Radar Installation(贪心)
- POJ 1328 Radar Installation 贪心
- POJ 1328 Radar Installation 贪心
- POJ 1328 贪心 Radar Installation
- POJ 1328 Radar Installation 贪心
- POJ | 1328 Radar Installation | 贪心
- POJ 1328 Radar Installation 贪心
- 贪心-poj-1328-Radar Installation
- POJ 1328 Radar Installation (贪心)
- POJ 1328 Radar Installation 贪心
- POJ 1328 Radar Installation 贪心
- POJ 1328、Radar Installation 贪心
- poj 1328 Radar Installation(贪心)
- POJ 1328 Radar Installation 贪心
- POJ 1328 Radar Installation 贪心
- POJ 1328 Radar Installation 贪心
- Poj 1328 Radar Installation 贪心
- POJ2503
- 栈 入栈序列与出栈序列 合法性 的一个有趣问题
- Moreau
- EOJ-大学生程序设计邀请赛(华东师范大学)-I-七巧板
- 语音播报Android导航SDK
- poj 1328 Radar Installation (逆思维+贪心)
- 模仿QQ带侧边栏框架搭建
- 堆有关的面试题(后续补充)
- 一种仓库扫码出库系统的实现
- 【Unity随笔】Unity之ugui简单背包实现与Json读取物品信息
- Partitioner编程
- 操作系统原理与实践”9--终端设备的控制
- Android Home键之后启动Activity延迟5秒
- html5的download