ACM题解——贪心——卫星安装转化类似节目安排问题

题目描述

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 11 2
0 20 0

Sample Output

Case 1: 2
Case 2: 1

题意

现在有n个点,给定n个位置(xi,yi)(均>=0),有半径为r的卫星安装在x轴上,想要所有卫星的辐射范围囊括所有位置,求最少的卫星个数。

题解

一开始我想的是将岛屿按照x轴大小排序,然后for循环遍历,y轴大于半径直接让标记值为1,跳出循环,输出-1;如果不是就让这个点压在一个半圆的最左边,求圆心,以致包括右边更多的点,这个圆心传递下去;遍历第二个坐标依次判断y轴是不是大于半径,然后查看该点坐标在不在以传递圆心为圆心的半圆内,若在传递圆心不变,要不在,就让这个点做新圆心的边界最左,然后求新的圆心值,并且卫星个数++;最后遍历完所有位置,输出卫星个数即可。

但是wa了,以上坐标我虽然都用了double来算,但是不排除有小数取了整造成的偏移错误。于是考虑换一个算法:

对每一个位置,确定可以满足囊括这个位置的圆心范围,x1,x2,       ,将每一个位置的圆心范围放入结构体,按照右范围(注意一定是按右范围进行排序)从小到大排序,记录当前右范围curr;循环遍历每一个位置的左右范围,若左范围<=curr,则不做处理,若>curr,则更新curr为当前右范围,且卫星计数器++;最终遍历完所有位置之后输出计数器的值即可。

代码

#include<iostream>
#include<algorithm>
#include<cmath>
using namespace std;
struct point
{long double x;long double y;long double l;   //记录岛屿 圆心左范围long double r;   //        圆心右范围
};
bool cmp(point a,point b)
{return a.r<b.r;
}
int main()
{int n=0;double k=0;cin>>n>>k;long long int num=0;while(n!=0 || k!=0){num++;point *p=new point[n];bool flag=0;for(int i=0;i<n;i++){cin>>p[i].x>>p[i].y;p[i].l=p[i].x-sqrt(k*k-p[i].y*p[i].y);p[i].r=p[i].x+sqrt(k*k-p[i].y*p[i].y);if(p[i].y>k)flag=1;}if(flag==1)cout<<"Case "<<num<<": "<<"-1"<<endl;  //先排除有y大于r的可能else{sort(p,p+n,cmp);    long long int coun=1;long double index=p[0].r;for(int i=1;i<n;i++){if(p[i].l>index){coun++;index=p[i].r;}}cout<<"Case "<<num<<": "<<coun<<endl;}cin>>n>>k;}return 0;
}

ACM题解——贪心——卫星安装相关推荐

  1. ACM题解——贪心专题——木头加工

    ACM题解--贪心之木头加工 题目描述 There is a pile of n wooden sticks. The length and weight of each stick are know ...

  2. 武汉ACM集训——贪心-7

    武汉ACM集训--贪心-7 洛谷 P6878 [JOI 2020 Final] JJOOII 2 题解 首先将字符串中各字母的下标按照字母种类分类存储. 接着依次遍历字母 J, O, I 当符合情况时 ...

  3. ACM题解——动态规划专题——G天上掉馅饼

    ACM题解--动态规划专题--G.天上掉馅饼 题目描述 都说天上不会掉馅饼,但有一天gameboy正走在回家的小径上,忽然天上掉下大把大把的馅饼.说来gameboy的人品实在是太好了,这馅饼别处都不掉 ...

  4. ACM题解——训练赛2_D - The Beatles

    ACM题解--训练赛2_D - The Beatles 题目描述 Examples Input 2 3 1 1 Output 1 6 Input 3 2 0 0 Output 1 3 Input 1 ...

  5. PAT甲级1125 Chain the Ropes:[C++题解]贪心、优先队列、合并果子

    文章目录 题目分析 题目来源 题目分析 来源:acwing 板子题:合并果子合并果子优先队列 分析:贪心策略是: 每次取最短的两条绳子a和b.该两条绳子合并为1条绳子,且长度变为a+b2\frac{a ...

  6. PAT甲级1038 Recover the Smallest Number (30 分):[C++题解]贪心、排列成最小的数、字符串

    文章目录 题目分析 题目来源 题目分析 来源:acwing 分析: 贪心: 对于字符串a和b,如果 a+b < b+a (这里+代表字符串中的连接)代表字典序更小.举例 a = 321 , b ...

  7. PAT甲级1037 Magic Coupon:[C++题解]贪心

    文章目录 题目分析 题目来源 题目分析 来源:acwing 分析: 贪心. 两个数列分别从大到小排列.从前往后遍历,如果a数组和b数组前k个数都是正数,就相乘累加到res中: 从后往前遍历,如果a数组 ...

  8. ACM题解系列之一:刘汝佳:《算法竞赛入门经典》(第2版)

    题是书中的题,解法参照了书中的解法,不少解法都做了简化和改进. 做程序,就要努力做到自己的程序是最好的! 第3章例题 POJ1488 UVA272 UVALive5381 TEX Quote[输入输出 ...

  9. leetcode题解——贪心

    文章目录 738. 单调递增的数字 435. 无重叠区间(活动调度问题) 134. 加油站 738. 单调递增的数字 当且仅当每个相邻位数上的数字 x 和 y 满足 x <= y 时,我们称这个 ...

  10. leetcode 贪心_leetcode题解(贪心算法)

    定义 贪心算法(又称贪婪算法)是指,在对问题求解时,总是做出在当前看来是最好的选择. 也就是说,不从整体最优上加以考虑,他所做出的是在某种意义上的局部最优解. 通常贪心算法的代码会非常短而且思路也非常 ...

最新文章

  1. 性能提升19倍,DGL重大更新支持亿级规模图神经网络训练
  2. ES5中新增的Array方法详细说明
  3. RestFul风格学习
  4. 蓝桥杯第八届省赛JAVA真题----方格分割
  5. JSON_dump和load
  6. 亚马逊因密码泄露重置部分用户密码
  7. 重定位----操作系统做的事情
  8. 从华为“流程与IT管理部”看IT部门定位
  9. Python 学习笔记【12】字典
  10. 【共读Primer】8.[2.1] 基本内置类型(2) Page32
  11. 钢结构节点输出软件_BIM技术助力桥梁钢结构设计施工一体化建设
  12. 步进电机正反转实验_电机正反转控制电路图原理图解
  13. java漫画pdf_Java并发编程学习宝典(漫画版)(PDF+HTML完结)
  14. autorunner
  15. b站的视频如何下载到手机上
  16. 双系统、多系统快速切换
  17. 算法高级(42)-大数定律-澳门皇家菠菜,为什么你逢赌必输?
  18. 语音情感识别中的音频检测算法学习
  19. oracle空的显示成减号,qdrzq
  20. Gtk-ERROR : GTK+ 2.x symbols detected. Using GTK+2.x and GTK+3 in the same process is not support

热门文章

  1. 蓝桥杯比赛时间在什么时候_什么时候立冬2020年农历具体时间
  2. 两台主机如何共享一套键鼠一台显示器?
  3. 二元函数matlab画图_matlab心形图大全,几个经典的函数图像,有趣的函数图像,matlab画图...
  4. PLC可编程控制器综合实训装置
  5. 什么是“个人商业模式”?就是一个人出售自己时间的方式
  6. 初识主成分分析 (PCA)
  7. DRM 驱动程序开发(VKMS)
  8. BZOJ_4199_[Noi2015]品酒大会_后缀自动机
  9. iOS 音频本地和在线播放器
  10. Kaggle共享单车需求项目详解