标签:
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 76972 | Accepted: 17240 |
Description
Input
Output
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; }
标签:
原文地址:http://www.cnblogs.com/ZefengYao/p/5819559.html