POJ 1328 Radar Installation 贪心算法
Description
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 is terminated by a line containing pair of zeros
Output
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
#include<cstdio>
#include<set>
#include<map>
#include<cstring>
#include<algorithm>
#include<queue>
#include<iostream>
#include<string>
#include<cmath>
using namespace std;
typedef long long LL;
#define MAXN 1003 /*
对于每个点可以在坐标轴上 得出能覆盖到该点的圆心范围,从而转化为x轴上的很多线段
选取一定数目的点,让所有的线段都包含至少一个点
先按结尾端点排序,然后尽量将雷达分布在右端(这样能尽可能和多个线段重叠)
*/
struct node
{
double beg,end;
}a[MAXN];
int n,d;
void cal(double x,double y,double &beg,double &end)//要求y<=d
{
double r = (double)d;
beg = x - sqrt(r*r-y*y);
end = x + sqrt(r*r-y*y);
}
bool cmp(node a,node b)
{
return a.end<b.end;
}
int main()
{
int cas = ;
while(scanf("%d%d",&n,&d),n+d)
{
double x,y;
bool f = false;
for(int i=;i<n;i++)
{
scanf("%lf%lf",&x,&y);
if(!f&&y<=d)
cal(x,y,a[i].beg,a[i].end);
else
{
f = true;
}
}
if(f)
{
printf("Case %d: -1\n",cas++);
continue;
}
sort(a,a+n,cmp);
int cnt = ;
double tmp = a[].end;
for(int i=;i<n;i++)
{
if(a[i].beg<=tmp)//因为是按结尾排序的,
//所以a[i].end肯定大于等于tmp,这种情况说明无需添加新的雷达
continue;
else//新的端点 起点无法包含,那么重新设置一个雷达(设置在新的线段最右端)
{
cnt++;
tmp = a[i].end;
}
}
printf("Case %d: %d\n",cas++,cnt);
}
return ;
}
POJ 1328 Radar Installation 贪心算法的更多相关文章
- POJ 1328 Radar Installation 贪心 A
POJ 1328 Radar Installation https://vjudge.net/problem/POJ-1328 题目: Assume the coasting is an infini ...
- 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(贪心区间选点+小学平面几何)
Input The input consists of several test cases. The first line of each case contains two integers n ...
- POJ 1328 Radar Installation 贪心 难度:1
http://poj.org/problem?id=1328 思路: 1.肯定y大于d的情况下答案为-1,其他时候必定有非负整数解 2.x,y同时考虑是较为麻烦的,想办法消掉y,用d^2-y^2获得圆 ...
- poj 1328 Radar Installation(贪心)
题目:http://poj.org/problem?id=1328 题意:建立一个平面坐标,x轴上方是海洋,x轴下方是陆地.在海上有n个小岛,每个小岛看做一个点.然后在x轴上有雷达,雷达能覆盖的范 ...
- POJ 1328 Radar Installation 贪心题解
本题是贪心法题解.只是须要自己观察出规律.这就不easy了,非常easy出错. 一般网上做法是找区间的方法. 这里给出一个独特的方法: 1 依照x轴大小排序 2 从最左边的点循环.首先找到最小x轴的圆 ...
- POJ 1328 Radar Installation#贪心(坐标几何题)
(- ̄▽ ̄)-* #include<iostream> #include<cstdio> #include<algorithm> #include<cmath ...
- 贪心 POJ 1328 Radar Installation
题目地址:http://poj.org/problem?id=1328 /* 贪心 (转载)题意:有一条海岸线,在海岸线上方是大海,海中有一些岛屿, 这些岛的位置已知,海岸线上有雷达,雷达的覆盖半径知 ...
- poj 1328 Radar Installation (简单的贪心)
Radar Installation Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 42925 Accepted: 94 ...
随机推荐
- 例题 3-5 谜题 uva227 Puzzle
A children’s puzzle that was popular years ago consisted of a × frame which contained small squares ...
- [POI2015]Wycieczki
题目描述 给定一张n个点m条边的带权有向图,每条边的边权只可能是1,2,3中的一种.将所有可能的路径按路径长度排序,请输出第k小的路径的长度,注意路径不一定是简单路径,即可以重复走同一个点. 输入输出 ...
- ACM_递推题目系列之一涂色问题(递推dp)
递推题目系列之一涂色问题 Time Limit: 2000/1000ms (Java/Others) Problem Description: 有排成一行的n个方格,用红(Red).粉(Pink).绿 ...
- .net中RSA加密解密
1.产生密钥: private static void CreateKey() { using (RSACryptoServiceProvider rsa = new RSACryptoService ...
- RabbitMQ~广播消息
定义 广播消息是指生产者产生的消息将分发给所有订阅这个消息的消费者,而普通的模式是:一批消息可以被多个人共同消费,如consumer1可能消费1,3,5记录,而consumer2可能消费的是2,4,6 ...
- EditPlus里面自带有更改文件编码的功能
- UVM基础之---------uvm report 机制分析
uvm 中的信息报告机制相对来说比较简单,功能上来说主要分为两部分: 第一通过ID对component的信息报告冗余级别进行控制,针对每个冗余级别进行不同的行为控制.这部分工作主要由uvm_repor ...
- 华硕(ASUS)X554LP笔记本一开机就进入aptio setup utility 问题的解决
某次因大意一直未插电,华硕(ASUS)X554LP笔记本后来没电关机.后来每次一开机就进入aptio setup utility界面,按F9调入默认配置,F10保存后退出,重启仍然进入aptio se ...
- windows系统下的redis启动教程
下载解压后配置redis.conf文件配置端口号和密码,打开poweshell命令,进入redis解压目录,使用.\redis-server.exe redis.conf 命令启动redis服务,再打 ...
- intellij idea console 乱码
修改文件 位置:{用户目录}\{iedea对应版本}\{idea or idea64}.vmoptions 比如我要修改我的配置文件 C:\Users\kkblf\.IntelliJIdea2017. ...