poj 1328 Radar Installation(贪心)
2014-08-15 00:21
381 查看
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
首先计算出每个海岛对应的能够在海岸线上修建雷达站的区间
再按区间的左值排序 题目就转化为了区间选点的贪心问题
先设定一个能够架设当前雷达的区间
当岛屿的区间左值大于雷达区间右值时 雷达数加一 并设定下一个雷达区间
当岛屿的区间右值小于雷达区间右值时 雷达区间右值向左缩至当前岛屿区间
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
首先计算出每个海岛对应的能够在海岸线上修建雷达站的区间
再按区间的左值排序 题目就转化为了区间选点的贪心问题
先设定一个能够架设当前雷达的区间
当岛屿的区间左值大于雷达区间右值时 雷达数加一 并设定下一个雷达区间
当岛屿的区间右值小于雷达区间右值时 雷达区间右值向左缩至当前岛屿区间
#include<stdio.h> #include<string.h> #include<math.h> #include<iostream> #include<algorithm> #include<queue> #include<stack> #define mem(a,b) memset(a,b,sizeof(a)) #define ll __int64 #define MAXN 1000 #define INF 0x7ffffff #define lson l,m,rt<<1 #define rson m+1,r,rt<<1|1 using namespace std; struct Island{ double l,r; }; Island s[MAXN+10]; int cmp(Island a,Island b) { return a.l<b.l; } int main() { int n,coun=1,ans; double d,x,y; int i,j,ok; while(cin>>n>>d) { if(n==0&&d==0) break; ok=1;ans=0; for(i=1;i<=n;i++) { scanf("%lf%lf",&x,&y); if(y>d) ok=0; if(ok) { double l=sqrt(d*d-y*y); s[i].l=x-l; s[i].r=x+l; } } if(!ok) {printf("Case %d: -1\n",coun++);continue;} sort(s+1,s+n+1,cmp); double left=-INF*1.0,right=-INF*1.0; for(i=1;i<=n;i++) { if(s[i].l>right) { ans++; left=s[i].l; right=s[i].r; } else if(s[i].r<right) right=s[i].r; } printf("Case %d: %d\n",coun++,ans); } return 0; }
相关文章推荐
- poj 1328 雷达覆盖 贪心
- TOJ 1115 POJ 1328 Radar Installation 贪心 C/C++
- POJ 1328 Radar Installation#贪心(坐标几何题)
- poj 1328 Radar Installation (贪心)
- POJ 1328 - Radar Installation [贪心]
- POJ 1328 Radar Installation(贪心)
- POJ 1328:Radar Installation:贪心法
- poj1328 Radar Installation(贪心)
- POJ 1328 Radar Installation 贪心
- poj1328_Radar Installation(贪心区间解决)
- POJ 1328 Radar Installation (贪心)
- poj 1328 Radar Installation (贪心)
- poj1328贪心
- POJ - 1328 Radar Installation(贪心/最大不相交覆盖)
- POJ 1328 Radar Installation(贪心+区间选点问题)
- POJ 1328 - Radar Installation [贪心]
- POJ1328--Radar Installation(贪心)
- POJ 1328 Radar Installation(贪心)
- poj 1328 Radar Installation(贪心)
- Radar Installation - POJ 1328 贪心