poj 1328 Radar Installation(贪心,线段重叠)
2017-06-19 23:11
435 查看
poj 1328 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
3 2
1 2
-3 1
2 1
1 2
0 2
0 0
Sample Output
Case 1: 2
Case 2: 1
思路 :
1. 将每个小岛看做中心,雷达就变成了范围,就转成线段重叠问题,我们然尽量多的线段重叠,就可以安装最少的雷达了。
2.还有一些注意事项会在代码提醒。
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include <cmath>
using namespace std;
const double dat = 0.00000001;
typedef struct node{
double l,r;
}node;
node a[1010];
int cmp(node x,node y )
{
return x.l < y.l; // 按照左端点排序
}
int main()
{
int n;
int index = 0;//样例下标
double m;
while(scanf("%d%lf",&n,&m)&& m+n)
{
bool flag = 1;
for(int i = 0; i < n; i++)
{
double x,y;
cin >> x >> y;
if(y > m)
{
flag = 0;
continue;//不能直接跳出,得让样例输完。
}
a[i].l = x - sqrt(m*m - y*y);
a[i].r = x + sqrt(m*m - y*y);
}
if(flag == 0)
{
printf("Case %d: -1\n",++index);
continue;
}
sort (a,a+n,cmp);
int num = 1;
double tmpr = a[0].r;//记录最远合适的 r 为 tmpr
for(int i = 1; i < n; i++)
{
if(a[i].r < tmpr)//此时仍然重叠 改变tmpr 为重叠部分最小值
{
tmpr = a[i].r;
}
else if(tmpr < a[i].l)//不再重叠 必须加 雷达了
{
tmpr = a[i].r;
num ++;
}
//其他情况就不用变了。
4000
}
printf("Case %d: %d\n",++index,num);
}
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
思路 :
1. 将每个小岛看做中心,雷达就变成了范围,就转成线段重叠问题,我们然尽量多的线段重叠,就可以安装最少的雷达了。
2.还有一些注意事项会在代码提醒。
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include <cmath>
using namespace std;
const double dat = 0.00000001;
typedef struct node{
double l,r;
}node;
node a[1010];
int cmp(node x,node y )
{
return x.l < y.l; // 按照左端点排序
}
int main()
{
int n;
int index = 0;//样例下标
double m;
while(scanf("%d%lf",&n,&m)&& m+n)
{
bool flag = 1;
for(int i = 0; i < n; i++)
{
double x,y;
cin >> x >> y;
if(y > m)
{
flag = 0;
continue;//不能直接跳出,得让样例输完。
}
a[i].l = x - sqrt(m*m - y*y);
a[i].r = x + sqrt(m*m - y*y);
}
if(flag == 0)
{
printf("Case %d: -1\n",++index);
continue;
}
sort (a,a+n,cmp);
int num = 1;
double tmpr = a[0].r;//记录最远合适的 r 为 tmpr
for(int i = 1; i < n; i++)
{
if(a[i].r < tmpr)//此时仍然重叠 改变tmpr 为重叠部分最小值
{
tmpr = a[i].r;
}
else if(tmpr < a[i].l)//不再重叠 必须加 雷达了
{
tmpr = a[i].r;
num ++;
}
//其他情况就不用变了。
4000
}
printf("Case %d: %d\n",++index,num);
}
return 0;
}
相关文章推荐
- POJ 1328 Radar Installation (贪心+线段覆盖)
- poj1328解题报告(贪心、线段交集)
- 51nod 1091 线段的重叠(贪心思维)
- POJ1328-Radar Installation(贪心)
- POJ 1328 && NYOJ 891 - 贪心 区间选点问题
- POJ 1328、Radar Installation 贪心
- POJ 1328 Radar Installation (贪心)
- POJ 1328 安装雷达 - (贪心)
- poj 1328 贪心
- poj1328 快排+贪心
- POJ1328 Radar Installation(贪心)
- POJ 1328 Radar Installation 贪心题解
- Poj 1328 贪心之区间选点+勾股定理**
- POJ 1328 Radar Installation 【贪心 区间选点】
- A - Radar Installation POJ - 1328 (区间贪心)
- 【贪心】[POJ1328]Radar Installation
- TOJ 1115 POJ 1328 Radar Installation 贪心 C/C++
- 51Nod 1091 线段的重叠(贪心+区间相关
- POJ 1328:Radar Installation 经典贪心
- 【贪心专题】POJ 1328 G - Radar Installation (区间覆盖)