Radar Installation(POJ 1328 区间贪心)
2016-01-28 11:06
337 查看
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
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 68578 | Accepted: 15368 |
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 需要判断d<0,a[i].y>d情况。 首先,按照x坐标排序,对于每个岛屿求出雷达所能放置的区间,然后对这些进行处理,x1,x2; 设当前雷达放置位置为nowx,对于下一个区间,如果写x1>nowx,显然多需要一个雷达,反之如果nowx>x1,nowx=min(nowx,x2);
#include <iostream> #include <cstdio> #include <algorithm> #include <cstring> #include <cmath> using namespace std; struct node { int x,y; }a[1000+5]; bool cmp(node q,node p) { if(q.x==p.x) return q.y>=p.y; return q.x<p.x; } int main() { int n,d; int i,j; int k=1; freopen("in.txt","r",stdin); while(scanf("%d%d",&n,&d)) { int coun=1; if(n==0&&d==0) break; bool flag=0; for(i=0;i<n;i++) { scanf("%d%d",&a[i].x,&a[i].y); if(a[i].y>d) flag=1; } if(flag||d<=0) { printf("Case %d: -1\n",k++); continue; } sort(a,a+n,cmp); double nowx=sqrt(double(d*d-a[0].y*a[0].y))+a[0].x; double x1,x2,temp; for(i=1;i<n;i++) { temp=sqrt(double(d*d-a[i].y*a[i].y)); x1=a[i].x-temp; x2=a[i].x+temp; if(x1>nowx) { nowx=x2; coun++; } else if(nowx>x2) nowx=x2; } printf("Case %d: %d\n",k++,coun); } }
相关文章推荐
- 摩斯代码转换
- 【python】 身份认证小程序
- iOS webp支持
- 中间件系列(一)——浅谈中间件原理及优势
- Android 第三天(上午)
- 瑞星发布2015年安全报告:“互联网+”企业需做好准备
- 安装VC6问题----字体font空
- AFN使用
- 大数据时代,计算模式从客户机/服务器到节点的转变
- android checkbox设置自定义的样式
- Service (在Service中注册BroadcastReceiver)
- 分析自定义view的实现过程-实现雪花飞舞效果
- JavaScript中的各种小坑汇总
- 大数据时代,计算模式从客户机/服务器到节点的转变
- When to Use Em vs. Rem
- maven项目依赖小试牛刀
- c++如何批量修改文件后缀名
- 建造者模式
- oracle中nvl()函数有什么用啊?
- HDU 2018:母牛的故事