【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 ...
随机推荐
- SPDY: An experimental protocol for a faster web HTTP/2
http://www.chromium.org/spdy/spdy-whitepaper https://en.wikipedia.org/wiki/SPDY Internet protocol su ...
- 如何解决Apache无法启动的问题
今天重装xampp后,一直无法启动Apache,提示以下错误: 22:36:54 [Apache] Attempting to start Apache app...22:36:54 [Apache] ...
- SE14 调整并激活数据库
一.使用SE14 激活并调整MARA 时的消息:(体验心惊肉跳,你就改改标准表试试.-增强也请小心谨慎) 使用检查: 调整并激活后的结果: 二.如果 运行时对象检查 出错误,参考 http://blo ...
- CoreData使用
1.如果想创建一个带有coreData的程序,要在项目初始化的时候勾选中 2.创建完成之后,会发现在AppDelegate里多出了几个属性,和2个方法 <span style="fon ...
- SpringMVC+Spring+MyBatis配置
今天配置项目时遇到一个问题,tomcat启动没有报错,但是访问页面的时总是报404,后台打印的日志是: 8080-exec-1] WARN springframework.web.servlet.Pa ...
- 数据结构之 线性表---单链表的操作B(先逆序+再删除重复元素)
数据结构上机测试2-2:单链表操作B Time Limit: 1000MS Memory limit: 65536K 题目描述 按照数据输入的相反顺序(逆位序)建立一个单链表,并将单链表中重复的元素删 ...
- Codeforces Round #401 (Div. 2) C Alyona and Spreadsheet —— 打表
题目链接:http://codeforces.com/contest/777/problem/C C. Alyona and Spreadsheet time limit per test 1 sec ...
- Codeforces Round #369 (Div. 2) D. Directed Roads —— DFS找环 + 快速幂
题目链接:http://codeforces.com/problemset/problem/711/D D. Directed Roads time limit per test 2 seconds ...
- 理解数学公式与numpy矩阵运算
1.矩阵的初始化 (1)创建一个 3*5的全0矩阵和全1矩阵 import numpy as np myzero = np.zeros([3,5]) print myzero myones = np. ...
- java random配置修改
不知道 报啥错的时候 ,改这个 vim /usr/java/latest/jre/lib/security/java.security 原值: securerandom.source=file:/de ...