POJ:1328-Radar Installation
Radar Installation
Time Limit: 1000MS Memory Limit: 10000K
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.
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轴上半径为r的圆将这些点覆盖,问最少要用多少个圆。这是一个很好的题。
一开始知道这是一个贪心,但是贪心的方法很久都没想到。其实就是先将这些点按照x坐标排序,然后确定第一个点的圆心,然后看第二个点圆能够建立圆心区域的最左边是否在第一个圆心的左边,是则将第二个圆心替换第一个圆的圆心,如果不是就看第二个点是否在第一个圆内,如果在圆内则不用管,不在就需要建一个新的圆来包含这个点,然后依次类推就可以得到最佳答案。输出-1就是y轴上的坐标比给出的r还更大。
还有就是要注意一下浮点数精度的问题。
#include<stdio.h>
#include<queue>
#include<algorithm>
#include<math.h>
using namespace std;
const int maxn = 1010;
struct node
{
int x,y;
} p[maxn];
bool cmp(node a,node b)
{
if(a.x != b.x)
return a.x < b.x;
return a.y > b.y;
}
bool init(int n,int r)
{
bool flag = false;
for(int i=0; i<n; i++)
{
scanf("%d%d",&p[i].x,&p[i].y);
if(p[i].y > r)
flag = true;
}
sort(p,p+n,cmp);
return flag;
}
int solve(int n,int r)
{
double pre_pos = -100000;
int ans = 1;
node now;
queue<node> qu;
for(int i=0; i<n; i++)
qu.push(p[i]);
pre_pos = 0x7f7f7f7f;
while(!qu.empty())
{
now = qu.front();
qu.pop();
double x = now.x + sqrt(r*r - now.y*now.y);
if(x < pre_pos)//圆心替换
{
pre_pos = x;
continue;
}
if((now.x-pre_pos)*(now.x-pre_pos) + now.y*now.y > r*r)//不包含在上一个点的圆内,需要新的圆
{
ans++;
pre_pos = x;
}
}
return ans;
}
int main()
{
int n,r,t = 1;
while(scanf("%d%d",&n,&r))
{
if(n+r == 0)
return 0;
bool flag = init(n,r);
if(flag)
{
printf("Case %d: -1\n",t++);
continue;
}
int ans = solve(n,r);
printf("Case %d: %d\n",t++,ans);
}
}
POJ:1328-Radar Installation的更多相关文章
- 贪心 POJ 1328 Radar Installation
题目地址:http://poj.org/problem?id=1328 /* 贪心 (转载)题意:有一条海岸线,在海岸线上方是大海,海中有一些岛屿, 这些岛的位置已知,海岸线上有雷达,雷达的覆盖半径知 ...
- POJ 1328 Radar Installation 贪心 A
POJ 1328 Radar Installation https://vjudge.net/problem/POJ-1328 题目: Assume the coasting is an infini ...
- Poj 1328 / OpenJudge 1328 Radar Installation
1.Link: http://poj.org/problem?id=1328 http://bailian.openjudge.cn/practice/1328/ 2.Content: Radar I ...
- poj 1328 Radar Installation (简单的贪心)
Radar Installation Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 42925 Accepted: 94 ...
- poj 1328 Radar Installation(nyoj 287 Radar):贪心
点击打开链接 Radar Installation Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 43490 Accep ...
- poj 1328 Radar Installation【贪心区间选点】
Radar Installation Time Limit : 2000/1000ms (Java/Other) Memory Limit : 20000/10000K (Java/Other) ...
- poj 1328 Radar Installation(贪心)
Description Assume the coasting is an infinite straight line. Land is in one side of coasting, sea i ...
- poj 1328 Radar Installation(贪心+快排)
Description Assume the coasting is an infinite straight line. Land is in one side of coasting, sea i ...
- POJ 1328 Radar Installation(很新颖的贪心,区间贪心)
Radar Installation Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 106491 Accepted: 2 ...
- POJ - 1328 Radar Installation(贪心区间选点+小学平面几何)
Input The input consists of several test cases. The first line of each case contains two integers n ...
随机推荐
- SpringBoot---Web开发
一.概述 1.SpringBoot提供了spring-boot-starter-web为 web开发 予以支持: 2.spring-boot-starter-web提供了 内嵌的Tomcat 以及 S ...
- HDU 5875 H - Function 用单调栈水过了
http://acm.hdu.edu.cn/showproblem.php?pid=5875 单调栈,预处理to[i]表示第一个比a[i]小的数字,一直跳就可以. 这题是数据水而已. 这里学习下单调栈 ...
- 洛谷-P3927 SAC E#1 - 一道中档题 Factorial
原址 题目背景 数据已修改 SOL君(炉石主播)和SOL菌(完美信息教室讲师)是好朋友. 题目描述 SOL君很喜欢阶乘.而SOL菌很喜欢研究进制. 这一天,SOL君跟SOL菌炫技,随口算出了n的阶乘. ...
- I/O流操做总结(三)
说实话,其实我并不是很喜欢Java这门语言,尽管它很强大,有很多现成的API可以调用 但我总感觉它把简单的事情弄得太过复杂,甚至有时候会让人迷失 弄不清到底是为了写出东西,还是为了语言本身 我学习的第 ...
- Generator 和 函数异步应用 笔记
Generator > ES6 提供的一种异步编程解决方案 > Generator 函数是一个状态机,封装了多个内部状态.还是一个遍历器对象生成函数.返回<label>遍历器对 ...
- mui对话框、表单
1.mui.alert() 普通提醒参数 1.message Type: String 提示对话框上显示的内容 2.title Type: String 提示对话框上显示的标题 3.btnValue ...
- ajax在购物车中的应用
代码如下: 购物车页面: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http: ...
- 编写SQL语句操作数据库(慕课SQLite笔记)
安卓常用数据存储方式之一SQLite学习及操作笔记 0.视频地址:http://www.imooc.com/video/3382 1.每个程序都有自己的数据库 默认情况下是各自互不干扰 1)创建一个数 ...
- 前端APP打包管理规范
1.包命名规范1)说明打包人:姓名拼音首字母小写 dev:开发环境 test:测试环境 pre:预发布环境 prod:正式环境 例如:版本号:1.2.3,说明:第一个数字大版本:变更框架.调整结构时变 ...
- LR中变量、参数的使用介绍
Action(){ char * url = "www.baidu.com"; char arr_url[1024]; //将url变量的值复制给p_url1参数 lr_save_ ...