poj 1328 贪心
2017-09-11 22:22
393 查看
http://poj.org/problem?id=1328
Radar Installation
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 90991 | Accepted: 20407 |
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
Source
Beijing 2002 没记错得话也是某届HNACM省赛的原题- -当时以为是计算几何什么的 一个很容易猜到的贪心是从左至右遍历,遇见没覆盖的点时尽可能的在保证覆盖此点的情况下往右边建。但是这是一个错误的贪心方案,圆心向右移动的同时最高点也将向右移动,这有可能导致原本可以覆盖住的点漏了出来导致答案错误,很忧桑。正解是对于每一个点考虑圆心可能的位置,显然每个点对应一条在x轴上的线段,这就转化为了一个经典的贪心问题,用最小点覆盖所有区间,按r排序后贪心建在最右边即可。#include<iostream> #include<cstdio> #include<cmath> #include<cstring> #include<algorithm> using namespace std; #define eps 1e-8 struct node{double x,y;}P[1005]; bool cmp(node A,node B) { if(A.y==B.y) return A.x<B.x; return A.y<B.y; } int main() { int N,D,i,j,k=0; while(cin>>N>>D&&(N||D)){int ans=0,ok=0; double x,y; for(i=0;i<N;++i) { scanf("%lf%lf",&x,&y); double r=sqrt(D*D-y*y); P[i].x=x-r; P[i].y=x+r; if(abs(y)>D)ok=1; } sort(P,P+N,cmp); double en=-999999999; for(i=0;i<N;++i) { if(P[i].x>en){ ans++; en=P[i].y; } } if(ok) ans=-1; printf("Case %d: %d\n",++k,ans); } return 0; }
相关文章推荐
- POJ 1328 Radar Installation 贪心区间选点
- POJ 1328 - Radar Installation(贪心)
- poj_1328_Radar Installation(区间贪心)
- Radar Installation(POJ 1328 贪心)
- poj-1328 Radar Installation 贪心
- poj1328(贪心)
- poj1328 Radar Installation —— 贪心
- POJ1328 贪心
- POJ-1328(贪心)
- POJ 1328 Radar Installation 贪心最少区间问题
- poj 1328 Radar Installation(nyoj 287 Radar):贪心
- POJ 1328 Radar Installation(雷达安装)__贪心
- POJ_1328_Radar Installation_贪心
- poj1328 -贪心雷达问题
- POJ 1328 Radar Installation 贪心
- POJ 1328 Radar Installation 雷达安装 贪心问题求解
- poj1328 Radar Installation(贪心)
- POJ 1328 Radar Installation【贪心】
- POJ1328,Radar Installation,贪心,仰慕逆向思维
- 贪心 POJ 1328