POJ 1328 Radar Installation【贪心】
题意:
将一条海岸线看成X轴,X轴上面是大海,海上有若干岛屿,给出雷达的覆盖半径和岛屿的位置,要求在海岸线上建雷达,在雷达能够覆盖全部岛屿情况下,求雷达的最少使用量。
分析:
贪心法,先研究一下每个岛屿,设岛屿到海岸线的垂直距离为d,雷达的覆盖半径为k,若d>k,直接输出-1,若d<=k,则雷达的建造有一个活动区间[x1,x2](用平面几何可以求得出来)。因此,在可以覆盖的情况下每个岛屿都有一个相应的活动区间。该问题也就转变成了最少区间选择问题即:
在n个区间中选择一个区间集合,集合中的各个区间都不相交,集合中元素的个数就是答案了。
#include <iostream>
#include <algorithm>
#include <stdlib.h>
#include <math.h>
#include <stdio.h>
using namespace std; struct point
{
double left, right;
}p[2010], temp; bool operator < (point a, point b)
{
return a.left < b.left;
} int main()
{
int n;
double r;
int kase = 0;
while(true)
{
scanf("%d%lf",&n,&r);
if(n==0&&r==0)break;
bool flag = false;
for (int i = 0; i < n; i++)
{
double a, b;
scanf("%lf%lf",&a,&b);
if (fabs(b) > r)
{
flag = true;
}
else
{
p[i].left = a * 1.0 - sqrt(r * r - b * b);
p[i].right = a * 1.0 + sqrt(r * r - b * b);
}
}
cout << "Case " << ++kase << ": ";
if (flag)
{
cout << -1 << endl;
}
else
{
int count = 1;
sort(p, p + n);
temp = p[0]; for (int i = 1; i < n; i++)
{
if (p[i].left > temp.right)//不重叠
{
count++;
temp = p[i];
}
else if (p[i].right < temp.right)//重叠取里面的端点
{
temp = p[i];
}
}
cout << count << endl;
}
}
return 0;
}
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 贪心算法
Description Assume the coasting is an infinite straight line. Land is in one side of coasting, sea i ...
- 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 ...
随机推荐
- ThinkPHP 3.2公共类库、应用类库ThinkPHP/Library讲解
一.ThinkPHP的类库主要包括公共类库和应用类库,都是基于命名空间进行定义和扩展的.只要按照规范定义,都可以实现自动加载. 公共类库 公共类库通常是指ThinkPHP/Library ...
- python -- 题目不看别人的自己写然后比较
题目一 ''' 编写Python脚本,分析xx.log文件,按域名统计访问次数倒序输出 xx.log文件内容如下: https://www.sogo.com/ale.html https://www. ...
- Python发邮件的小脚本
# -*- coding: UTF-8 -*- import smtplib from email.mime.text import MIMEText mailto_list = ['hitwh_Gy ...
- .NET面试题系列(十)委托与事件
委托 有了委托的存在,使得方法可以作为参数传递给另一个方法. int Max(int x,int y) { return x>y?x:y; } int Min(int x,int y) { re ...
- Bulma - 基于 Flexbox 的现代化的 CSS 框架
Bulma 是一个基于 Flexbox 的现代化的 CSS 框架,设计的初衷就是移动优先(Mobile First),模块化设计,可以轻松用来实现各种简单或者复制的内容布局,浏览器支持:浏览器支持:C ...
- SQL语句——重复记录
1.查找重复记录: (按id查找) select * from user_info where id in ( select id from user_info group by id ) 即:sel ...
- 20155319 2016-2017-2 《Java程序设计》第八周学习总结
20155319 2016-2017-2 <Java程序设计>第八周学习总结 教材学习内容总结 NIO与NIO2 - NIO使用频道(channel)来衔接数据节点 - read()将Re ...
- 第15月第22天 libz.dylib
1. 3.在弹出的对话框中输入"cmd"+"shift"+"g" 4 4.输入/usr/lib https://jingyan.baidu. ...
- Python中os.system和os.popen区别
Python调用Shell,有两种方法:os.system(cmd)或os.popen(cmd)脚本执行过程中的输出内容.实际使用时视需求情况而选择. 两者的区别是: os.system(cmd)的返 ...
- js_原生js实现上拉加载更多的功能。
1.人生啊,是我莽撞了啊.这是我公司一个喜欢读书的女孩子的个性签名,喜欢哪些句子,不悲伤,却切切实实的令人喜好. 2.写程序是一件很直接明了的事情,明白了就是明白了,不懂就是不懂,不懂装懂的会让你走很 ...