POJ 1328 Radar Installation(贪心)
2015-03-16 19:13
344 查看
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
Case 1: 2
Case 2: 1
x轴代表海岸线,上边代表海洋,下边代表陆地,在海中有岛屿,给出坐标(x,y),陆地有雷达,给出其覆盖半径d,问至少需要几个雷达才能覆盖所有的岛。如果没有解决方案就输出-1
思路:
我们找一个岛屿能被侦测到的极限范围, 当岛屿到雷达的距离等于d时, 雷达可以位于岛屿的左侧也可以位于雷达的右侧。而这就可以分别确定雷达相对于岛屿的最左坐标和最右坐标。
最左为:x - sqrt(d*d-y*y); 最右为:x+ sqrt(d*d-y*y); 每个岛屿都有这样的最左和最右可被侦测坐标。
根据贪心的思想,每次都应该将最右坐标作为衡量标准。
假定当前的岛屿为cur,当前的下一个为next。
1.如果next的最左坐标比cur的最右坐标都大的话,只能再设一个雷达来侦测next了,然后将next的最右坐标作为比较量。
2.如果next的最左坐标比cur的最右坐标小,这时会有两种情况。
A.next最右 < cur最右
B.next最右 >= cur最右
对于A情况,也就等价于next包含于cur, 这样就应该把next的右作为衡量标准了
对于B情况,我们可以直接侦测到next了, 可以找next的next了.
在第二种情况中为什么我们每次都取两个点右坐标的最小值呢,因为半径R一样,那图就有高矮胖瘦之形,若取右坐标的最大值的话,那右坐标小的那个 点就有可能够不到
#include <iostream>
#include <string.h>
#include <math.h>
#include <stdlib.h>
using namespace std;
struct node
{
double xL,xR;
}p[1010];
int cmp(const void*a,const void*b)
{
return ((node*)a)->xL>((node*)b)->xL?1:-1;
}
int main()
{
int n,d,cnt=1;
while(cin>>n>>d&&(n||d))
{
int flag=1;
if(d<=0)
flag=0;
for(int i=0;i<n;i++)
{
int x,y;
cin>>x>>y;
if(y>d)
flag=0;
double t=sqrt(1.0*(d*d-y*y));
p[i].xL=x-t;
p[i].xR=x+t;
}
if(flag)
{
qsort(p,n,sizeof(p[0]),cmp);
int sum=1;
double high=p[0].xR;
for(int i=1;i<n;i++)
{
if(p[i].xL>high)
{
sum++;
high=p[i].xR;
}
else
high=high<p[i].xR?high:p[i].xR;
}
cout<<"Case "<<cnt++<<": "<<sum<<endl;
}
else
cout<<"Case "<<cnt++<<": -1"<<endl;
}
return 0;
}
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
3 2 1 2 -3 1 2 1 1 2 0 2 0 0
Sample Output
Case 1: 2
Case 2: 1
x轴代表海岸线,上边代表海洋,下边代表陆地,在海中有岛屿,给出坐标(x,y),陆地有雷达,给出其覆盖半径d,问至少需要几个雷达才能覆盖所有的岛。如果没有解决方案就输出-1
思路:
我们找一个岛屿能被侦测到的极限范围, 当岛屿到雷达的距离等于d时, 雷达可以位于岛屿的左侧也可以位于雷达的右侧。而这就可以分别确定雷达相对于岛屿的最左坐标和最右坐标。
最左为:x - sqrt(d*d-y*y); 最右为:x+ sqrt(d*d-y*y); 每个岛屿都有这样的最左和最右可被侦测坐标。
根据贪心的思想,每次都应该将最右坐标作为衡量标准。
假定当前的岛屿为cur,当前的下一个为next。
1.如果next的最左坐标比cur的最右坐标都大的话,只能再设一个雷达来侦测next了,然后将next的最右坐标作为比较量。
2.如果next的最左坐标比cur的最右坐标小,这时会有两种情况。
A.next最右 < cur最右
B.next最右 >= cur最右
对于A情况,也就等价于next包含于cur, 这样就应该把next的右作为衡量标准了
对于B情况,我们可以直接侦测到next了, 可以找next的next了.
在第二种情况中为什么我们每次都取两个点右坐标的最小值呢,因为半径R一样,那图就有高矮胖瘦之形,若取右坐标的最大值的话,那右坐标小的那个 点就有可能够不到
#include <iostream>
#include <string.h>
#include <math.h>
#include <stdlib.h>
using namespace std;
struct node
{
double xL,xR;
}p[1010];
int cmp(const void*a,const void*b)
{
return ((node*)a)->xL>((node*)b)->xL?1:-1;
}
int main()
{
int n,d,cnt=1;
while(cin>>n>>d&&(n||d))
{
int flag=1;
if(d<=0)
flag=0;
for(int i=0;i<n;i++)
{
int x,y;
cin>>x>>y;
if(y>d)
flag=0;
double t=sqrt(1.0*(d*d-y*y));
p[i].xL=x-t;
p[i].xR=x+t;
}
if(flag)
{
qsort(p,n,sizeof(p[0]),cmp);
int sum=1;
double high=p[0].xR;
for(int i=1;i<n;i++)
{
if(p[i].xL>high)
{
sum++;
high=p[i].xR;
}
else
high=high<p[i].xR?high:p[i].xR;
}
cout<<"Case "<<cnt++<<": "<<sum<<endl;
}
else
cout<<"Case "<<cnt++<<": -1"<<endl;
}
return 0;
}
相关文章推荐
- POJ 1328 Radar Installation 雷达安装 贪心问题求解
- Radar Installation(poj1328,贪心,区间交集问题)
- poj 1328 Radar Installation 贪心
- POJ 1328 Radar Installation 贪心
- poj 1328 贪心(建立雷达覆盖小岛)
- Radar Installation(POJ1328, 贪心)
- POJ 1328 - Radar Installation(贪心)
- poj-1328 Radar Installation(贪心+区间选点)
- poj 1328 Radar Installation (贪心选择性质,需要抽象出来才可以)
- POJ 1328 Radar Installation【贪心 区间问题】
- 2018.1.21【POJ - 1328】小岛与雷达解题报告(二维转一维,贪心)
- (贪心+排序)poj1328 Radar Installation
- POJ 1328 Radar Installation(贪心)
- POJ_1328_Radar Installation_贪心
- POJ-1328(贪心)
- poj 1328 Radar Installation (贪心)@
- poj-1328 Radar Installation 贪心
- POJ 1328-Radar Installation(贪心)
- poj 1328 Radar Installation 【贪心】
- POJ 1328 Radar Installation(贪心)