Radar Installation
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 76972 | Accepted: 17240 |
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
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
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,若行,输出雷达数量(x轴当做航向)
思路:先以每一个点为中心画弧,与x轴相交两点成一块区域,则在区域内任意一点为圆心画圆都可以覆盖那个点,将所有的区间按照每个区间最左端从小到大排序
从起始区间开始,1:若上一个区间的最右端小于当前区间最左端,上一个雷达将无法覆盖当前区间,则需要换一个新雷达来覆盖当前的区间
2:若1条件不成立,即上一个区间最右端大于当前区间最左端,雷达能覆盖当前区间,就需要取两个区间的公共部分,即比较两个区间最右端大小;那个小取哪个
AC代码:
#define _CRT_SECURE_NO_DEPRECATE #include<iostream> #include<algorithm> #include<vector> #include<math.h> using namespace std; struct section { double left; double right; //bool operator < (const section& b) const //{ // return left < b.left; //} }; bool cmp(const struct section&a, const struct section&b) { return a.left < b.left; } int main() { int n, d,j=1; while (scanf("%d%d", &n,&d) && n) { int what = 0; vector<section>s(n); for (int i = 0;i < n;i++) { double x, y; //cin >> x >> y; scanf("%lf%lf",&x,&y); if (y > d) { what = 1; continue; } if (what)continue; double r = sqrt(d * d - y * y); s[i].left = x - r; s[i].right= x + r; } if (what) { cout << "Case "<< j++ << ": " << -1 << endl; continue; } sort(s.begin(), s.end(),cmp); double end = -INT_MAX; int radar = 0; for (vector<section>::iterator it = s.begin();it != s.end(); it++) { if (end < it->left) {//如果上一个区间范围的最右端小于当前区间最左端,则需第二个雷达 radar++; end = it->right; } else if (end>it->right) { end = it->right; } } cout<< "Case " << j++ << ": " << radar << endl; } return 0; }