贪心 + 计算几何 --- Radar Installation
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.
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.
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
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
题目来源#
http://poj.org/problem?id=1328
解题思路#
以每个岛的坐标为圆心画圆,会与x轴有2个交点,那么这2个点就是能覆盖该岛的雷达x 坐标区间,问题就转变成对一组区间,找最少数目的点,使得所有区间中都有一点。把包含某区间的区间删掉(如果一个点使得子区间得到满足, 那么该区间也将得到满足),这样所有区间的终止位置严格递增。
每次迭代对于第一个区间, 选择最右边一个点, 因为它可以让较多区间得到满足, 如果不选择第一个区间最右一个点(选择前面的点), 那么把它换成最右的点之后, 以前得到满足的区间, 现在仍然得到满足, 所以第一个区间的最右一个点为贪婪选择, 选择该点之后, 将得到满足的区间删掉, 进行下一步迭代, 直到结束。
代码:
#include <algorithm> #include <cmath> #include <cstdio> #include <iostream> using namespace std; #define MAX 1010 struct Point { float x, y; float l, r; bool marked; }; Point points[MAX]; bool cmp(Point a, Point b) { return a.r < b.r; } int main() { int case_cnt = 1; int n; float r; while (cin >> n >> r) { if (n == 0 && r == 0) { break; } int i, j; int cnt = 0; if (r <= 0) cnt = -1; for (i = 0; i < n; i++) { scanf("%f%f", &points[i].x, &points[i].y); // 血的教训,cout会超时 if (points[i].y > r) cnt = -1; points[i].l = points[i].x - sqrt(r * r - points[i].y * points[i].y); points[i].r = points[i].x + sqrt(r * r - points[i].y * points[i].y); } printf("Case %d: ", case_cnt++); if (cnt == -1) { cout << "-1" << endl; continue; } for (i = 0; i < n; i++) points[i].marked = false; sort(points, points + n, cmp); bool covered = false; for (i = 0; i < n && cnt >= 0; i++) { if (points[i].marked) { continue; } for (j = 0; j < n; j++) { if (points[j].marked) { continue; } if (points[j].l <= points[i].r) { points[j].marked = true; covered = true; } else { break; } } if (covered) cnt++; covered = false; } cout << cnt << endl; } return 0; }
作者:北岛知寒
出处:https://www.cnblogs.com/crazyacking/p/3728279.html
版权:本作品采用「署名-非商业性使用-相同方式共享 4.0 国际」许可协议进行许可。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· DeepSeek如何颠覆传统软件测试?测试工程师会被淘汰吗?