Radar Installation 贪心
Default
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 42461 | Accepted: 9409 |
Description
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 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
#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 贪心的更多相关文章
- POJ 1328 Radar Installation 贪心 A
POJ 1328 Radar Installation https://vjudge.net/problem/POJ-1328 题目: Assume the coasting is an infini ...
- Radar Installation(贪心)
Radar Installation Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 56826 Accepted: 12 ...
- Radar Installation(贪心,可以转化为今年暑假不ac类型)
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(贪心区间选点+小学平面几何)
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 ...
- POJ1328 Radar Installation(贪心)
题目链接. 题意: 给定一坐标系,要求将所有 x轴 上面的所有点,用圆心在 x轴, 半径为 d 的圆盖住.求最少使用圆的数量. 分析: 贪心. 首先把所有点 x 坐标排序, 对于每一个点,求出能够满足 ...
- poj1328 Radar Installation —— 贪心
题目链接:http://poj.org/problem?id=1328 题解:区间选点类的题目,求用最少的点以使得每个范围都有点存在.以每个点为圆心,r0为半径,作圆.在x轴上的弦即为雷达可放置的范围 ...
- POJ 1328 Radar Installation 贪心题解
本题是贪心法题解.只是须要自己观察出规律.这就不easy了,非常easy出错. 一般网上做法是找区间的方法. 这里给出一个独特的方法: 1 依照x轴大小排序 2 从最左边的点循环.首先找到最小x轴的圆 ...
随机推荐
- 一、cocos2d-x 3.0 final使用httpclient编译到android,须要用到的android.mk
今天写一个网络框架,在vs上面非常欢快的执行车,心想,尼玛!cocos2d-x 3.0这么方便,预计不久的将来我就能回家种地了,由于不用程序猿了,直接cocos2dstudio拖界面了= =!!. 写 ...
- [转] C++中临时对象及返回值优化
http://www.cnblogs.com/xkfz007/articles/2506022.html 什么是临时对象? C++真正的临时对象是不可见的匿名对象,不会出现在你的源码中,但是程序在运行 ...
- UITextField控件处理键盘弹出时遮住输入框的问题
原文连接: http://www.devdiv.com/thread-70159-1-1.html 实现以下三个方法,如果弹出的键盘会遮住输入框 ,整体的界面会向上移动,这样就不会遮住输入框了.自己增 ...
- Android 环境下编译FFmpeg
Android 环境下编译FFmpeg 开发环境:Ubuntu 12.04.2 LTS , android-sdk-linux, android-ndk-r8e 一 .X264 编译 1. X2 ...
- java编程思想-并发思维导图
- 关于IO学习的几个函数
这是最近学到的几个关于IO文件操作的几个小算法,今天总结出来. 1. 删除一个给定的目录,这上目录不为空目录,使用递归来实现 public void test04(File file) { File[ ...
- 开源的Android开发框架-------PowerFramework使用心得(一)总体介绍
PowerFramework是一款几乎囊括了所有Android基础功能的框架应用,这个框架目前是开源的,开发者可以在这个框架的基础上进行二次开发.结合开发者自己的UI设计,可以很快就能开发出具备基础应 ...
- IIS7、IIS6 web.config注册HttpModule
IIS6注册HttpModule <system.web> <httpModules> <add name="..." type= ...
- 【USACO 3.1.6】邮票
[描述] 已知一个N枚邮票的面值集合(如,{1分,3分})和一个上限K ——表示信封上能够贴K张邮票.计算从1到M的最大连续可贴出的邮资. 例如,假设有1分和3分的邮票:你最多可以贴5张邮票.很容易贴 ...
- iOS开发,新手入门指导
在做了近两年wp,安卓开发之后,某一天突然决定投身iOS的开发之中. 因为一直用的mac,做wp开发的时候都用双系统,vs开久了,就会比较烫,这点让人不爽.后来更多地做安卓,直接mac下开发,很舒适的 ...