链接: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;
}

C、Guard the empire(贪心)的更多相关文章

  1. bzoj2811[Apio2012]Guard 贪心

    2811: [Apio2012]Guard Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 905  Solved: 387[Submit][Statu ...

  2. bzoj 2811: [Apio2012]Guard【线段树+贪心】

    关于没有忍者的区间用线段树判就好啦 然后把剩下的区间改一改:l/r数组表示最左/最右没被删的点,然后删掉修改后的左边大于右边的:l升r降排个序,把包含完整区间的区间删掉: 然后设f/g数组表示i前/后 ...

  3. 【二分答案+贪心】UVa 1335 - Beijing Guards

    Beijing was once surrounded by four rings of city walls: the Forbidden City Wall, the Imperial City ...

  4. 贪心(qwq)习题题解

    贪心(qwq)习题题解 SCOI 题解 [ SCOI2016 美味 ] 假设已经确定了前i位,那么答案ans一定属于一个区间. 从高位往低位贪心,每次区间查找是否存在使此位答案为1的值. 比如6位数确 ...

  5. bzoj2811 [Apio2012]Guard

    传送门:http://www.lydsy.com/JudgeOnline/problem.php?id=2811 [题解] 首先我们先把没看到忍者的段去掉,可以用线段树做. 如果剩下的就是K,那么特判 ...

  6. LUOGU P3112 [USACO14DEC]后卫马克Guard Mark

    题目描述 Farmer John and his herd are playing frisbee. Bessie throws the frisbee down the field, but it' ...

  7. [差分][二分][贪心]luogu P3634 [APIO2012]守卫

    题面 https://www.luogu.com.cn/problem/P3634 给m个限制,可以是一段区间中必须有或者必须无忍者 最多有k个忍者,问有多少个位点一定有忍者 分析 首先用差分标记一下 ...

  8. BZOJ 1692: [Usaco2007 Dec]队列变换 [后缀数组 贪心]

    1692: [Usaco2007 Dec]队列变换 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 1383  Solved: 582[Submit][St ...

  9. HDOJ 1051. Wooden Sticks 贪心 结构体排序

    Wooden Sticks Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) To ...

随机推荐

  1. linux 安装Mosquitto

    这篇博客讲的很好:https://www.cnblogs.com/chen1-kerr/p/7258487.html 此处简单摘抄部分内容 1.下载mosquitto安装包 源码地址:http://m ...

  2. 三维GIS引擎地图可视化渲染方案设计

    1.GIS地图可视化流程 GIS地图可视化就是将空间数据转化为地图数据再进行交互处理的方法,下图一展示了地图可视化的可编程渲染的典型管道,原始空间数据必须处理为图形API支持基础图元用以地图渲染.下图 ...

  3. 高可用Keepalived+LVS搭建流程

    本流程搭建1个master,1个backup节点的Keepalived,使用lvs轮询2个节点的服务. 一.使用版本 CentOS 7.7 Keepalived 1.3.5 ipvsadm 1.27( ...

  4. Redis(7)——持久化【一文了解】

    一.持久化简介 Redis 的数据 全部存储 在 内存 中,如果 突然宕机,数据就会全部丢失,因此必须有一套机制来保证 Redis 的数据不会因为故障而丢失,这种机制就是 Redis 的 持久化机制, ...

  5. 聊聊order by的工作机制

    总结写在前面: 1. 介绍了orderBy的两种算法流程:全字段排序 和 rowid排序. 2. rowid排序 相比 全字段排序,参与排序字段较少,耗内存较少,多一步回表,如果内存够的情况下MySQ ...

  6. C语言 变量初始化二进制、八进制、十六进制

    int a1 = 10;   //十进制 int a2 = 0b10;  //二进制 int a3 = 010;    //八进制 int a4 = 0x10;  //十六进制 打印的结果:

  7. 【转】分布式架构的演进(JavaWeb)

    作者:李小翀 链接:https://www.zhihu.com/question/22764869/answer/31277656 来源:知乎 1.初始 初始阶段 的小型系统 应用程序.数据库.文件等 ...

  8. oracle--触发器(转)

    转载自http://blog.csdn.net/indexman/article/details/8023740/ 触发器是许多关系数据库系统都提供的一项技术.在oracle系统里,触发器类似过程和函 ...

  9. JavaScript每日学习日记(1)

    8.11.2019 1. lastIndexOf() 方法从尾到头进行检索. 2. 有三种提取部分字符串的方法: 2.1 slice(start, end)  如果某个参数为负,则从字符串的结尾开始计 ...

  10. 普通人学习rust——从零到放弃 简单输入输出

    普通人学习rust--从零到放弃 简单输入输出 环境 本文章内容基于如下环境,如若出入请参考当前环境. rustc 1.42.0 (b8cedc004 2020-03-09) cargo 1.42.0 ...