【OpenJ_Bailian - 1328】Radar Installation (贪心)
Radar Installation
原文是English,直接上中文
Descriptions:
假定海岸线是无限长的直线。陆地位于海岸线的一侧,海洋位于另一侧。每个小岛是位于海洋中的一个点。对于任何一个雷达的安装 (均位于海岸线上),只能覆盖 d 距离,因此海洋中的小岛被雷达安装所覆盖的条件是两者间的距离不超过 d 。
我们使用卡笛尔坐标系,将海岸线定义为 x 轴。海洋的一侧位于 x 轴上方,陆地的一侧位于下方。给定海洋中每个小岛的位置,并给定雷达安装的覆盖距离,您的任务是写一个程序,找出雷达安装的最少数量,使得所有的小岛都被覆盖。注意:小岛的位置以它的 x-y 坐标表示。

图 A: 雷达安装的示例输入
Input
输入由多个测试用例组成。每个测试用例的第一行,包含了两个整数 n (1<=n<=1000) 和 d,其中 n 是海洋中小岛的数目,d 是雷达安装的覆盖距离。接下来是 n 行,每行包含了两个整数,表示每个小岛的坐标。每组测试用例之间,以一个空行间隔。
Output
对于每个测试用例,输出一行,包括测试用例的编号,以及雷达安装所需的最小数量。"-1" 个安装,表示该测试用例无解决方案。
Sample Input
3 2
1 2
-3 1
2 1 1 2
0 2 0 0
Sample Output
Case 1: 2
Case 2: 1
题目链接:
https://vjudge.net/problem/OpenJ_Bailian-1328
雷达必须在x轴的[s[i].left,s[i].right]之间,每个小岛都有对应在x轴的点是s[i].left和s[i].right,问题转换成用最少的点,使所有的线段都被覆盖到
AC代码:
#include <iostream>
#include <cstdio>
#include <fstream>
#include <algorithm>
#include <cmath>
#include <deque>
#include <vector>
#include <queue>
#include <string>
#include <cstring>
#include <map>
#include <stack>
#include <set>
#include <sstream>
#define mod 1000000007
#define eps 1e-6
#define ll long long
#define INF 0x3f3f3f3f
#define ME0(x) memset(x,0,sizeof(x))
using namespace std;
struct point
{
double left,right;//线段两端
bool operator<(const point &c)const//按做端点排序
{
return left<c.left;
}
};
point points[];
int n,d;
int sum=;
int f=;
//求每个Case
int solve()
{
int ans=;//雷达个数
double nowpos;
sort(points,points+n);
// for(int i=0; i<n; i++)
// cout<<points[i].left<<" "<<points[i].right<<endl;
nowpos=points[].right;
for(int i=; i<n; ++i)
{
// cout<<"*"<<endl;
if(points[i].left<=nowpos)
nowpos=min(nowpos,points[i].right);
else//若当前线段与目前线段没有交集,则加入新雷达
{
++ans;
nowpos=points[i].right;
}
}
return ans;
}
int main()
{
while(cin>>n>>d,n+d)
{
f=;
sum++;
int x,y;
for(int i=; i<n; ++i)
{
cin>>x>>y;
if(y>d)
f=;
else
{
double t=d*d-y*y;
points[i].left=x-sqrt(t);
points[i].right=x+sqrt(t);
}
}
if(f)
printf("Case %d: %d\n",sum,solve());
else
printf("Case %d: -1\n",sum);
}
}
【OpenJ_Bailian - 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 ...
随机推荐
- SAM4E单片机之旅——6、LED闪烁之按钮控制
现在试试用按钮控制LED灯……让LED在一个按钮按下时亮起:弹起时灭掉. 主要目的是学习GPIO的输入及中断. 一. 电路 图中的J39-n是几个跳线插座,位置在开发板LCD附近,往下进行前要先确保跳 ...
- 在DuiLib中使用MFC类
比如,想在DuiLib里使用MFC中的CInternetSession 首先,将Project Properties->General->Use of MFC设置为Use MFC in a ...
- Parallel Tests
Parallel Tests Parallel Android Tests Appium provides a way for users to automate multiple Android s ...
- indexDB操作(部分方法不太会使用)
<script type="text/javascript"> //打开数据库 function openDB(name,version){ var version = ...
- js正则表达式,密码长度要大于6位,由数字和字母组成
var pwd = $("#pwd").val(); var reg = /^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{6,}$/; if(!reg ...
- ffmpeg full help
Hyper fast Audio and Video encoder usage: ffmpeg [options] [[infile options] -i infile]... {[outfile ...
- 【转载】Myeclipse中实现js的提示
近期需要大量使用JS来开发,但是MyEclipse2014自带的JS编辑器没有代码提示的功能,开发效率有点低,所以安装了一个Spket的插件,过程非常简单,SVN插件的安装比这个更简单. Spket插 ...
- Session移除
Session.Clear()就是把Session对象中的所有项目都删除了,Session对象里面啥都没有.但是Session对象还保留. Session.Abandon()就是把当前Session对 ...
- OJ提交题目中的语言选项里G++与C++的区别
一.OJ提交题目中的语言选项里G++与C++的区别 http://www.th7.cn/Program/cp/201405/199001.shtml 首先更正一个概念,C++是一门计算机编程语言,G+ ...
- 细谈HTML解析模块
细谈HTML解析模块 Html在网页中所占的位置,用一个简单直观的图给展示一下: