POJ1328 Radar Installation(贪心)
2016-09-28 10:52
246 查看
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
好题!真是一道好题啊!这一道题的意思就是说,在x轴上方是海域,下方和x轴都属于陆地。现在给定一些海岛坐标,并且给出雷达可以搜索的长度,要求雷达尽可能覆盖海岛,并且所用雷达数目最少。这道题,一开始想真的觉得好困惑,这咋整?后来慢慢形成思路,我把他们全部按照x轴从小到大排序,从最左边开始贪心,在雷达包含选定的海岛的情况下,尽可能放入更多的海岛。好,这是一个初始方案,接下来写起来就没这么简单咯。对了,雷达肯定是在x轴上的,这样照到的海域肯定会比在x轴下面要大!
问题1,选定第一个海岛之后,他在x轴上有两个交点,然后如果放入第二个海岛,这个交点的范围会一直缩小的,直到其他全部都不能放入这个交点范围内。而我一开始却只保留了右边的交点,因为我是以x轴从小到大排序的嘛,天真的以为只要下一个海岛左交点小于这个右交点就行了,但是发现我左边的交点也是变化的,也需要算出来才行。
问题2,排序的时候还要注意,如果x轴是相同的,那就按照y的大小从大到小排序,这样最上面那个点和x轴的两个交点的范围是相对于下面的点来说是比较窄的,可以减少一下范围的更新次数嘛。。。
问题3,我也是看到别人给的数据才想的,那就是如果真的这么奇葩他的海岛给的y点是小于0的,也就是在陆地上的怎么办,然后就是我一开始是用的m+n来结束while读入循环,然后发现他给我出了这样一组数据,3 -3,三个岛,雷达范围是-3?什么鬼?反正这样也会让我的程序结束,这样的数据我觉得是不合法的,但是他给出来了就改一改吧,毕竟acm本来就是个坑。
然后我自己还有各种各样的粗心大意的错误,TLE了两发,while循环那忘记要j++了。。。RE的两发,这个。。。我也不知道为什么。。。wa了一发,还是太粗心。。。然后,终于终于过了!
代码如下:
看到代码才想起,就是我们判定下一个海岛是否在这个范围内,有三种情况,一种是,下一个海岛的左交点坐标在范围内,一种是右交点在范围内,还有一种!!!就是下一个海岛的左交点小于范围的左交点,右交点大于范围的右交点!相当于下一个海岛的左右范围比原本的范围还要大!这也是有交集的!!!脑子不够用我当时就是少了这种情况啊!!!好像就是因为这个wa了一发。
一年多了,重写了一次,发现还是不会。。。不过代码风格变得更清晰了
#include<iostream>
#include<algorithm>
#include<stdio.h>
#include<cmath>
#include<utility>
#include<vector>
using namespace std;
#define eps 1e-7
typedef pair<double, double> pii;
vector <pii> v;
int n, d;
int main() {
int Case = 1, x, y;
while(cin >> n >> d) {
bool flag = 1;
v.clear();
if(n == 0 && d == 0)
break;
if(d < 0)
flag = 0;
for(int i = 0; i < n; i++) {
cin >> x >> y;
if(y <= 0)
continue;
if(y > d)
flag = 0;
if(!flag)
continue;
double tmp = sqrt((double)d * d - (double)y * y);
v.push_back(pii(x - tmp, x + tmp));
}
if(flag) {
int ans = 0;
sort(v.begin(), v.end());
for(int i = 0; i < v.size(); i++) {
double l = v[i].first;
double r = v[i].second;
int k = i + 1;
while(k < v.size() && v[k].second >= l && v[k].first <= r) {
l = max(v[k].first, l);
r = min(v[k].second, r);
k++;
}
i = k - 1;
ans++;
}
printf("Case %d: %d\n", Case++, ans);
} else {
printf("Case %d: -1\n", Case++);
}
}
return 0;
}
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 77816 | Accepted: 17409 |
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
好题!真是一道好题啊!这一道题的意思就是说,在x轴上方是海域,下方和x轴都属于陆地。现在给定一些海岛坐标,并且给出雷达可以搜索的长度,要求雷达尽可能覆盖海岛,并且所用雷达数目最少。这道题,一开始想真的觉得好困惑,这咋整?后来慢慢形成思路,我把他们全部按照x轴从小到大排序,从最左边开始贪心,在雷达包含选定的海岛的情况下,尽可能放入更多的海岛。好,这是一个初始方案,接下来写起来就没这么简单咯。对了,雷达肯定是在x轴上的,这样照到的海域肯定会比在x轴下面要大!
问题1,选定第一个海岛之后,他在x轴上有两个交点,然后如果放入第二个海岛,这个交点的范围会一直缩小的,直到其他全部都不能放入这个交点范围内。而我一开始却只保留了右边的交点,因为我是以x轴从小到大排序的嘛,天真的以为只要下一个海岛左交点小于这个右交点就行了,但是发现我左边的交点也是变化的,也需要算出来才行。
问题2,排序的时候还要注意,如果x轴是相同的,那就按照y的大小从大到小排序,这样最上面那个点和x轴的两个交点的范围是相对于下面的点来说是比较窄的,可以减少一下范围的更新次数嘛。。。
问题3,我也是看到别人给的数据才想的,那就是如果真的这么奇葩他的海岛给的y点是小于0的,也就是在陆地上的怎么办,然后就是我一开始是用的m+n来结束while读入循环,然后发现他给我出了这样一组数据,3 -3,三个岛,雷达范围是-3?什么鬼?反正这样也会让我的程序结束,这样的数据我觉得是不合法的,但是他给出来了就改一改吧,毕竟acm本来就是个坑。
然后我自己还有各种各样的粗心大意的错误,TLE了两发,while循环那忘记要j++了。。。RE的两发,这个。。。我也不知道为什么。。。wa了一发,还是太粗心。。。然后,终于终于过了!
代码如下:
#include<cstdio> #include<algorithm> #include<cstring> #include<cmath> using namespace std; int mark[1005]; int n, m, ppp; struct node { int x, y; double left,right; bool operator < (const node &a)const { if(x != a.x) return x < a.x; else return y > a.y; } }island[1005]; void func(int i) { ppp++; mark[i] = 1; double LEFT = island[i].left, RIGHT = island[i].right; int j = i + 1; while(j < n) { if(!mark[j] && ((island[j].left >= LEFT && island[j].left <= RIGHT) || (island[j].right >= LEFT && island[j].right <= RIGHT) || (island[j].left <= LEFT && island[j].right >= RIGHT))) { mark[j] = 1; LEFT = island[j].left > LEFT ? island[j].left : LEFT; RIGHT = island[j].right < RIGHT ? island[j].right : RIGHT; } j++; } } int main() { int flag, k = 1, i, j; int tx, ty; while(scanf("%d%d", &n, &m), n != 0 || m != 0) { memset(mark, 0, sizeof(mark)); flag = 0; ppp = 0; for(i = 0, j = 0; i < n; i++) { scanf("%d%d", &tx, &ty); if(ty > m) flag = 1; if(ty >= 0 && !flag) { island[j].x = tx; island[j].y = ty; island[j].left = 1.0 * tx - sqrt(m * m - ty * ty); island[j].right = 1.0 * tx + sqrt(m * m - ty * ty); j++; } } n = j; printf("Case %d: ", k++); if(flag || m <= 0) printf("-1\n"); else { sort(island, island + n); for(i = 0; i < n; i++) { if(!mark[i]) func(i); } printf("%d\n", ppp); } } return 0; }
看到代码才想起,就是我们判定下一个海岛是否在这个范围内,有三种情况,一种是,下一个海岛的左交点坐标在范围内,一种是右交点在范围内,还有一种!!!就是下一个海岛的左交点小于范围的左交点,右交点大于范围的右交点!相当于下一个海岛的左右范围比原本的范围还要大!这也是有交集的!!!脑子不够用我当时就是少了这种情况啊!!!好像就是因为这个wa了一发。
一年多了,重写了一次,发现还是不会。。。不过代码风格变得更清晰了
#include<iostream>
#include<algorithm>
#include<stdio.h>
#include<cmath>
#include<utility>
#include<vector>
using namespace std;
#define eps 1e-7
typedef pair<double, double> pii;
vector <pii> v;
int n, d;
int main() {
int Case = 1, x, y;
while(cin >> n >> d) {
bool flag = 1;
v.clear();
if(n == 0 && d == 0)
break;
if(d < 0)
flag = 0;
for(int i = 0; i < n; i++) {
cin >> x >> y;
if(y <= 0)
continue;
if(y > d)
flag = 0;
if(!flag)
continue;
double tmp = sqrt((double)d * d - (double)y * y);
v.push_back(pii(x - tmp, x + tmp));
}
if(flag) {
int ans = 0;
sort(v.begin(), v.end());
for(int i = 0; i < v.size(); i++) {
double l = v[i].first;
double r = v[i].second;
int k = i + 1;
while(k < v.size() && v[k].second >= l && v[k].first <= r) {
l = max(v[k].first, l);
r = min(v[k].second, r);
k++;
}
i = k - 1;
ans++;
}
printf("Case %d: %d\n", Case++, ans);
} else {
printf("Case %d: -1\n", Case++);
}
}
return 0;
}
相关文章推荐
- poj1328贪心
- POJ 1328 Radar Installation(贪心)
- POJ 题目1328 Radar Installation(贪心)
- poj 1328 【Radar Installation】【贪心】
- Radar Installation(POJ 1328 贪心)
- poj1328(贪心)
- POJ1328 Radar Installation (贪心)
- 【POJ】1328 - Radar Installation(贪心)
- [ACM] POJ 1328 Radar Installation (贪心,区间选点问题)
- POJ 1328 Radar Installation 贪心题解
- 【贪心专题】POJ 1328 G - Radar Installation (区间覆盖)
- POJ 1328 Radar Installation 贪心
- poj 贪心相关之1328 Radar Installation
- poj 1328 Radar Installation(nyoj 287 Radar):贪心
- poj 1328 贪心
- poj1328贪心
- POJ1328 Radar Installation 【贪心·区间选点】
- poj1328 区间贪心 雷达覆盖岛屿
- poj 1328 Radar Installation (贪心)
- POJ--1328:Radar Installation (贪心)