POJ 1328 Radar Installation(经典贪婪)
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 54143 | Accepted: 12178 |
Description
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
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
Sample Input
3 2
1 2
-3 1
2 1 1 2
0 2 0 0
Sample Output
Case 1: 2
Case 2: 1
算法分析:
刚開始不知道从哪分析,后经指点发现圆心位置是个突破口。首先得出每个点所相应的圆心位置,注意若想覆盖最多。每个圆都尽量做到使点刚好位于圆边界。比方我们左右两边各有一个水果。我们不确定是否能拿得到两个,为了使得尽量拿到两个,我们会使左手刚好触碰到一个。伸右手去抓还有一个,而不是直接以某一个为中心而忽略增大自身所能更加接近还有一个的机会,于是问题就变成了求解圆心。依照圆心排序,不断更新雷达圆心,终于使数量最小。
此外,注意代码凝视部分。。
。
圆心竖轴为0,横轴坐标计算公式:
r=x+sqrt(d*d-y*y)//圆心从左向右移动
#include<iostream>
#include<algorithm>
#include<cmath>
using namespace std; typedef struct island{
int x;
int y;
double z;
}island;
island land[1000];
int Comp(island a,island b)
{
return a.z<b.z;
}
int main()
{
int n,d,i,count,num=1,sign;
while(cin>>n>>d)
{
sign=0;
if(!(n||d))
break;
count=1,i=0;
while(i<n)
{
cin>>land[i].x>>land[i].y;
land[i].z=(double)land[i].x+sqrt(double(d*d-land[i].y*land[i].y)); //圆心必须浮点数啊有木有
if(abs(land[i].y)>d||d<=0) //注意。当雷达无法笼罩时的情况输出-1
sign=1;
i++;
}
if(sign)
{cout<<"Case "<<num<<": -1"<<endl;num++;continue;}
sort(land,land+n,Comp); //对圆心位置从左到右进行排序
i=1;
double a=land[0].z;
while(i<n)
{
if(!((land[i].x-a)*(land[i].x-a)+land[i].y*land[i].y<=d*d))
{
a=land[i].z;
count++;
}
i++;
}
cout<<"Case "<<num<<": "<<count<<endl;
num++;
}
return 0;
}
版权声明:本文博主原创文章,博客,未经同意不得转载。
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 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【贪心】
POJ 1328 题意: 将一条海岸线看成X轴,X轴上面是大海,海上有若干岛屿,给出雷达的覆盖半径和岛屿的位置,要求在海岸线上建雷达,在雷达能够覆盖全部岛屿情况下,求雷达的最少使用量. 分析: 贪心法 ...
- 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: 56702 Accepted: 12 ...
随机推荐
- 一、Github博客搭建之jekyll安装
注意:以下步骤是FQ后操作的,需要了解FQ的可以移步 -> 枫叶主机 一.安装jekyll需要Ruby-2.1.0以上版本,本人是mac pro系统版本10.12.5(macOS Sierra) ...
- WPF遍历当前容器中某种控件的方法
原文:WPF遍历当前容器中某种控件的方法 版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/m0_37591671/article/details/79 ...
- 【t086】防护伞
Time Limit: 1 second Memory Limit: 128 MB [问题描述] 据说2012的灾难和太阳黑子的爆发有关.于是地球防卫小队决定制造一个特殊防护伞,挡住太阳黑子爆发的区域 ...
- [AngualrJS NG-redux] Map State and Dispatchers to Redux
In this lesson, we are going to learn how to map our Angular component directly to our application s ...
- php课程 3-12 回调参数怎么用
php课程 3-12 回调参数怎么用 一.总结 一句话总结:有时候需要在一个函数中使用另外一个函数,使用回调的话,那么那个函数的几十行代码就不用写了,其实很基础,就是函数名的字符串的替换,超级基础的. ...
- WEB应用图片的格式,以及各自的特点和优化(一) by FungLeo
WEB应用图片的格式,以及各自的特点和优化(一) by FungLeo 前言 12年前我入行三天.用table布局做了一个非常粗糙的网页.我说了一句话,"网页就是表格加文字加图片,图片分两种 ...
- 【漏洞公告】Tomcat信息泄漏和远程代码执行漏洞:CVE-2017-12615/CVE-2017-12616
2017年9月19日,Apache Tomcat官方确认并修复了两个高危漏洞,漏洞CVE编号:CVE-2017-12615和CVE-2017-12616,该漏洞受影响版本为7.0-7.80之间,在一定 ...
- Http请求工具类(Java原生Form+Json)
package com.tzx.cc.common.constant.util; import java.io.IOException; import java.io.InputStream; imp ...
- POJ - 2236Wireless Network-并查集
id=11125" target="_blank" style="color:blue; text-decoration:none">POJ - ...
- 【codeforces 546D】Soldier and Number Game
time limit per test3 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...