Radar Installation(贪心)

时间:2022-04-28 03:16:52
Radar Installation
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 56826   Accepted: 12814

Description

Assume the coasting is an infinite straight line. Land is in one side of coasting, sea in the other. Each small island is a point locating in the sea side. And any radar installation, locating on the coasting, can only cover d distance, so an island in the 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. Radar Installation(贪心)  Figure A Sample Input of Radar Installations

Input

The input consists of several test cases. The first line of each case contains two integers n (1<=n<=1000) and d, where n is the number of islands in the sea and d is the distance of coverage of the radar installation. This is followed by n lines each containing 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

For each test case output one line consisting of the test case number followed by the minimal number of radar installations needed. "-1" installation means no solution for that case.

Sample Input

3 2
1 2
-3 1
2 1 1 2
0 2 0 0

Sample Output

Case 1: 2
Case 2: 1
 #include<stdio.h>
#include<iostream>
#include<algorithm>
#include<math.h>
using namespace std;
struct island
{
double x , y ;
double left , right ;
}a[];
int n ;
int d ;
bool cmp (island a , island b)
{
return a.x < b.x ;
} int main ()
{
// freopen ("a.txt" , "r" , stdin) ;
int cnt ;
int ans = ;
bool flag ; while (~ scanf ("%d%d" , &n , &d)) {
if (n == && d == )
break ;
flag = ; for (int i = ; i < n ; i++) {
scanf ("%lf%lf" , &a[i].x , &a[i].y) ;
if (a[i].y > 1.0 * d || a[i].y < || d <= ) {
flag = ;
}
if (!flag) {
a[i].left = (double) a[i].x - sqrt (1.0 * d * d - a[i].y * a[i].y) ;
a[i].right = (double) a[i].x + sqrt (1.0 * d * d - a[i].y * a[i].y) ;
}
}
if (flag) {
printf ("Case %d: -1\n" , ans++) ;
continue ;
}
sort (a , a + n , cmp) ;
cnt = ;
double l = a[].left , r = a[].right ;
for (int i = ; i < n ; i++) {
if (a[i].left > r) {
cnt++ ;
l = a[i].left ;
r = a[i].right ;
}
else {
l = a[i].left ;
r = a[i].right < r ? a[i].right : r ;
}
}
printf ("Case %d: %d\n" , ans++ , cnt) ;
}
return ;
}

别忘记每次都要跟新放radar的区间 ,orz

Radar Installation(贪心)的更多相关文章

  1. POJ 1328 Radar Installation 贪心 A

    POJ 1328 Radar Installation https://vjudge.net/problem/POJ-1328 题目: Assume the coasting is an infini ...

  2. Radar Installation 贪心

    Language: Default Radar Installation Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 42 ...

  3. Radar Installation&lpar;贪心,可以转化为今年暑假不ac类型&rpar;

    Radar Installation Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 20000/10000K (Java/Other) ...

  4. poj 1328 Radar Installation&lpar;贪心&plus;快排&rpar;

    Description Assume the coasting is an infinite straight line. Land is in one side of coasting, sea i ...

  5. POJ - 1328 Radar Installation&lpar;贪心区间选点&plus;小学平面几何&rpar;

    Input The input consists of several test cases. The first line of each case contains two integers n ...

  6. POJ 1328 Radar Installation 贪心算法

    Description Assume the coasting is an infinite straight line. Land is in one side of coasting, sea i ...

  7. POJ1328 Radar Installation&lpar;贪心&rpar;

    题目链接. 题意: 给定一坐标系,要求将所有 x轴 上面的所有点,用圆心在 x轴, 半径为 d 的圆盖住.求最少使用圆的数量. 分析: 贪心. 首先把所有点 x 坐标排序, 对于每一个点,求出能够满足 ...

  8. poj1328 Radar Installation —— 贪心

    题目链接:http://poj.org/problem?id=1328 题解:区间选点类的题目,求用最少的点以使得每个范围都有点存在.以每个点为圆心,r0为半径,作圆.在x轴上的弦即为雷达可放置的范围 ...

  9. POJ 1328 Radar Installation 贪心题解

    本题是贪心法题解.只是须要自己观察出规律.这就不easy了,非常easy出错. 一般网上做法是找区间的方法. 这里给出一个独特的方法: 1 依照x轴大小排序 2 从最左边的点循环.首先找到最小x轴的圆 ...

随机推荐

  1. jdk动态代理实现

    1.jdk动态代理的简单实现类 package com.proxy; import java.lang.reflect.InvocationHandler; import java.lang.refl ...

  2. 【转】jQuery中&period;bind&lpar;&rpar; &period;live&lpar;&rpar; &period;delegate&lpar;&rpar; &period;on&lpar;&rpar;的区别

    bind(type,[data],fn) 为每个匹配元素的特定事件绑定事件处理函数 $("a").bind("click",function(){alert(& ...

  3. 任务中使用wget&comma;不保存文件

    */20 * * * * wget --output-document=/dev/null http://www.domain.com 使用wget每过20分钟访问一次,不保存访问文件内容

  4. Owin中间件搭建OAuth2&period;0认证授权服务体会

    继两篇转载的Owin搭建OAuth 2.0的文章,使用Owin中间件搭建OAuth2.0认证授权服务器和理解OAuth 2.0之后,我想把最近整理的资料做一下总结. 前两篇主要是介绍概念和一个基本的D ...

  5. elasticsearh 中每个节点中需要有相同的插件

    elasticsearh 中每个节点中需要有相同的插件 [2016-09-13 19:25:24,049][INFO ][discovery.zen ] [node02] failed to send ...

  6. docker 指定容器名字

    docker:/root# docker run -itd --name linux123 ubuntu /bin/bash Unable to find image 'ubuntu:latest' ...

  7. android 布局常用混淆属性

    1.如何控制某一控件在父控件中的相对位置呢? 在Android系统中提供了layout_margin,用来控制某一控件边缘相对于父控件的边距. 其中, android:layout_marginTop ...

  8. hdu4414&lpar;DFS 找十字架数量&rpar;

    Problem Description The Nazca Lines are a series of ancient geoglyphs located in the Nazca Desert in ...

  9. WebService基础学习&lpar;三&rpar;&mdash&semi;CXF

    一.什么是CXF?      Apache CXF = Celtix + Xfire,开始叫 Apache CeltiXfire,后来更名为 Apache CXF 了,以下简称为 CXF.Apache ...

  10. android学习笔记--AlarmManager

    AlarmManager称呼为全局定时器,有的称呼为闹钟.其实它的作用和Timer有点相似. 都有两种相似的用法: (1)在指定时长后执行某项操作(2)周期性的执行某项操作 AlarmManager ...