Language:
Default
Radar Installation
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 42461   Accepted: 9409

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. 
 
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<string.h>
#include<math.h>
#include<algorithm>
#include<iostream>
#include<stack>
#include<math.h>
using namespace std; struct node
{
double start;
double end;
}coor[];//记录每个区间的端点
int cmp(const struct node a,const struct node b)
{
return a.start < b.start;
}
int t,r;
stack <node> st;//用栈存每个区间, int cal(int ans)
{
while(!st.empty())
st.pop();
for(int i = t-; i >= ; i--)
st.push(coor[i]);
while(st.size() >= )//当栈中至少存在两个区间时
{
struct node tmp1 = st.top();
st.pop();
struct node tmp2 = st.top();
if(tmp1.end >= tmp2.start)//当取出的两个区间有公共部分时
{
st.pop();//tmp2出栈
struct node tmp;
tmp.start = max(tmp1.start, tmp2.start);//注意取公共部分时,起始点取较大者
tmp.end = min(tmp1.end, tmp2.end);//终点取较小者
st.push(tmp);//将公共部分入栈
ans--;//每两个区间交一次,雷达个数减一次
}
}
return ans;
}
int main()
{ int cor_x[],cor_y[];
double add;
int cnt = ;
while(~scanf("%d %d",&t,&r))
{
int ok = ;//判断小岛的坐标是否合法,
if(t == && r == ) break;
for(int i = ; i < t; i++)
{
scanf("%d %d",&cor_x[i],&cor_y[i]);
if(cor_y[i] > r)//若小岛纵坐标大于半径则不合法
ok = ;
}
if(ok == )
{
printf("Case %d: -1\n", cnt++);
continue;
}
for(int i = ; i < t; i++)
{
//以每个小岛为圆心,r为半径画圆,coor[]存该圆与x轴相交的区间
add = sqrt(r*r-cor_y[i]*cor_y[i]);
coor[i].start = cor_x[i] - add;
coor[i].end = cor_x[i] + add;
}
sort(coor,coor+t,cmp);//对这些区间按起始点从小到大排序
int ans = t;
ans = cal(ans);
printf("Case %d: %d\n",cnt++,ans);
}
return ;
}

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(贪心)

    Radar Installation Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 56826   Accepted: 12 ...

  3. Radar Installation(贪心,可以转化为今年暑假不ac类型)

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

  4. poj 1328 Radar Installation(贪心+快排)

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

  5. POJ - 1328 Radar Installation(贪心区间选点+小学平面几何)

    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(贪心)

    题目链接. 题意: 给定一坐标系,要求将所有 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. 分布式系统状态下redis存储asp.net session使用第三方Providers驱动

    https://github.com/ServiceStack/ServiceStack.Redis (redis客户端组件) 注:redis服务端在windows不太稳定,一般部署在Linux下. ...

  2. [Javascrip] Logging Timing Data to the Console

    Learn to use console.time with console.timeEnd to get accurate timings of operations in javascript. ...

  3. [Angular 2] Exposing component properties to the template

    Showing you how you can expose properties on your Controller to access them using #refs inside of yo ...

  4. TCP/IP之分层

    网络协议通常分不同层次进行开发,每一层分别负责不同的通信功能.一个协议族,比方T C P / I P,是一组不同层次上的多个协议的组合.T C P / I P通常被觉得是一个四层协议系统. 1.每层的 ...

  5. Pyhton开发【第五篇】:Python基础之杂货铺

    Python开发[第五篇]:Python基础之杂货铺   字符串格式化 Python的字符串格式化有两种方式: 百分号方式.format方式 百分号的方式相对来说比较老,而format方式则是比较先进 ...

  6. (转)H264(NAL简介与I帧判断)

    1.NAL全称Network Abstract Layer, 即网络抽象层.         在H.264/AVC视频编码标准中,整个系统框架被分为了两个层面:视频编码层面(VCL)和网络抽象层面(N ...

  7. 2015-09-21CSS:引入方式、选择器、注释、文字样式

    1.HTML中引入CSS的方式 HTML中引入CSS的样式有4种:行内式.内嵌式.导入式和链接式. ⑴行内式 行内式是在标记的style属性中设定CSS样式.这种方式没有体现出CSS的优势,不推荐使用 ...

  8. JavaScript设计模式之策略模式(学习笔记)

    在网上搜索“为什么MVC不是一种设计模式呢?”其中有解答:MVC其实是三个经典设计模式的演变:观察者模式(Observer).策略模式(Strategy).组合模式(Composite).所以我今天选 ...

  9. 初识html、css时随笔记录

    css部分样式 圆框效果:border-radius:5px; 固定样式:position:fixed;悬浮窗在IE6中position:absolute其余浏览器可以用fixed: 使table中的 ...

  10. 5事件DOM零级事件跟DOM二级事件

    事件的行为传播,行为本身跟事件绑定没有关系:1.全新认识事件(某一个具体的行为)->行为本身:浏览器天生自带的一些行为操作->click,mouseover(mouseenter),mou ...