链接:https://ac.nowcoder.com/acm/contest/3570/C

来源:牛客网

题目描述

Hbb is a general and respected by the entire people of the Empire.

The wizard will launch an attack in the near future in an attempt to

destroy the empire. Hbb had knew the way the Wizards will attack this

time: they will summon N bone dragons on the broad flat ground outside

the wall and let the bone dragons spray a flame (the flame can only be

emitted once). If the wall is sprayed by flames, the wall will be

completely destroyed in an instant. To withstand the wizard’s attack,

Hbb feels very anxious, although he already knew where all the bone

dragons would be summoned. Coincidentally, scientists at the

Capital Laboratory have developed a new type of weapon. The striking

range of the weapon is a circle with the weapon as the center and a

radius of D. In other words, if the weapons are properly placed, the

bone dragons within the strike range will be destroyed. Weapons can

only be placed on the wall, but Hbb is too anxious at this time to

know how to place the weapon, so he tells you the position of the bone

dragon . Since the cost of the weapon is very expensive, Hbb gives you

a requirement: tell him what the minimum number of weapons to use in

order to destroy all bone dragons. If there is no way to destroy all

bone dragons, output -1.

输入描述: The input consists of several test cases. The first line of each

case contains two integers and , where is the number of bone dragon

in the ground and is the distance of coverage of the weapon. Then is

followed by lines each containing two integers and , representing

the coordinate of the position of each bone dragon. Then a blank line

follows to separate the cases. The input is terminated by a line

containing pair of zeros 输出描述: For each test case output one line

consisting of the test case number followed by the minimal number of

weapon needed. “-1” installation means no solution for that case. 示例1

输入

3 2
1 2
-3 1
2 1 1 2
0 2 0 0
输出
Case 1: 2
Case 2: 1

备注:



Case1 is explained in the figure, the wall is regarded as the X axis, and the weapon can only be placed on the X axis. The red circle is the weapon strike range.


思路如下

题意:

给出n头骨龙的坐标(只会在第一、第二象限),在X轴上放置武器,每个武器都有相同的固定打击范围D,问最少需要多少武器可以将所有骨龙覆盖,如果不能覆盖所有骨龙则输出-1.

思路:

贪心:要想武器打击范围能够覆盖所有的龙骨,若可以覆盖,则以每头龙为圆心,以D为半径,则必定与x轴有两个焦点,在两个交点所在的区间内就是我们可以放置武器的地方,因此计算出所有骨龙与X轴相交的两个点,按照左端点排序(或者右端点),遍历所有骨龙,判断左右边的武器是否能够覆盖当前遍历到的骨龙的,如果不能覆盖,则添置一个新武器于当前骨龙 与X轴交点的最右端。


题解如下

#include<iostream>
#include<cmath>
#include<algorithm>
#define l first
#define r second
#define Pff pair<double,double> //这里的 pair 用的非常恰到好处,pair的两个值恰好存储区间的两个端点
using namespace std; const int Len = 1005;
Pff p[Len];
int n; double d; inline void cal(int &x,int &y,int &i) //计算某个骨龙所对应的武器放置的区间
{
p[i].l = (double)x - sqrt(d * d - y * y);
p[i].r = (double)x + sqrt(d * d - y * y);
} inline void work()
{
sort(p + 1, p + 1 + n); //对各个武器区间进行排序
int ans = 1;
double pos_r = p[1].r;
for(int i = 2; i <= n; ++i)
{
if(p[i].l > pos_r)
{
++ ans;
pos_r = p[i].r;
}
else if(p[i].r < pos_r) //⚠️这里的一步操作一定要理解
{
pos_r = p[i].r;
}
}
printf("%d\n",ans);
} int main()
{
int case_ = 1;
while(~scanf("%d %lf", &n, &d) && n + d)
{ printf("Case %d: ",case_ ++);
int flag = 0,x,y;
for(int i = 1;i <= n;++ i)
{
scanf("%d %d",&x,&y);
if(y > d || flag)
{
flag = 1; //在输入的时候不能够break ,否则就输不进去了
continue;
}
cal(x,y,i);
}
if(flag == 0)
work();
else
printf("-1\n");
}
return 0;
}

最新文章

  1. 在C语言中利用PCRE实现正则表达式
  2. Python之操作Redis、 RabbitMQ、SQLAlchemy、paramiko、mysql
  3. TableViewCell自适应高度
  4. Objective-C 引用计数:不讲用法,只说原理
  5. Bootstrap 使用清单组组件创建价格表
  6. hadoop2.7.3+spark2.1.0+scala2.12.1环境搭建(2)安装hadoop
  7. 深入浅出Java Dom4j读取XML
  8. June. 27th 2018, Week 26th. Wednesday
  9. DAY20、垃圾回收机制,正则模块
  10. Jquery EasyUI Combotree只能选择叶子节点且叶子节点有多选框
  11. react 中的绑定事件
  12. springboot 升级到2.0后 context-path 配置 不起作用,不生效 不管用 皆是因为版本改动导致的在这里记录一下
  13. MySQL--3约束和修改数据表总结
  14. Lazarus的二维码解决方案
  15. 基于TrueLicense实现产品License验证功能
  16. nginx 超时问题: upstream timed out (110: Connection timed out) while reading response header from upstream
  17. mysql使用笔记(网易Mysql实用手册)---1
  18. [面试]CVTE 2019提前批 Windows应用开发一面
  19. Scikit-learn 库的使用
  20. java复制文件夹及所有子目录和文件

热门文章

  1. VUE实现Studio管理后台(完结):标签式输入、名值对输入、对话框(modal dialog)
  2. 峰哥说技术:09-Spring Boot整合JSP视图
  3. vue 动态加载图片路径报错解决方法
  4. Java基础 - Date的相关使用(获取系统当前时间)
  5. 免ROOT卸载手机自带软件详细教程
  6. windows服务搭建与删除简单介绍
  7. CSS的SVG学习
  8. mybatis探究之延迟加载和缓存
  9. 【Weiss】【第03章】练习3.9:大整数运算包
  10. 在Linux环境安装redis步骤,且设置开机自动启动redis