NC24017 [USACO 2016 Jan S]Angry Cows
NC24017 [USACO 2016 Jan S]Angry Cows
题目
题目描述
Bessie the cow has designed what she thinks will be the next big hit video game: "Angry Cows". The premise, which she believes is completely original, is that the player shoots cows with a slingshot into a one-dimensional scene consisting of a set of hay bales located at various points on a number line. Each cow lands with sufficient force to detonate the hay bales in close proximity to her landing site. The goal is to use a set of cows to detonate all the hay bales.
There are N hay bales located at distinct integer positions x1,x2,…,xN on the number line. If a cow is launched with power R landing at position x, this will causes a blast of "radius R", destroying all hay bales within the range x−R…x+R.
A total of K cows are available to shoot, each with the same power R. Please determine the minimum integer value of R such that it is possible to use the K cows to detonate every single hay bale in the scene.
输入描述
The first line of input contains N (1≤N≤50,000) and K (1≤K≤10). The remaining N lines all contain integers x1…xN (each in the range 0…1,000,000,000).
输出描述
Please output the minimum power R with which each cow must be launched in order to detonate all the hay bales.
示例1
输入
7 2
20
25
18
8
10
3
1
输出
5
题解
思路
知识点:二分。
显然二分半径,由于覆盖范围是 \([x-R,x+R]\) 从 \(x-R\) 开始到另一点 \(x+R\) 一共距离 \(2R\) ,所以从之前一个点 \(a_{pre}\) 开始,到某个点 \(a_{pos}\) 的距离满足 \(a_{pos} - a_{pre}>2R\) 时,则让 \(a_{pos}\) 成为下一个 \(a_{pre}\) ,然后区域数 \(cnt\) 加一。如果最后 \(cnt \leq k\) ,则说明可行;\(cnt>k\),则说明不可行。
时间复杂度 \(O(n \log \lceil \frac{a[n-1]-a[0]}{2} \rceil)\)
空间复杂度 \(O(n)\)
代码
#include <bits/stdc++.h>
using namespace std;
int a[50007];
int n, k;
bool check(int mid) {
int pre = a[0], cnt = 0;
for (int i = 1;i < n;i++) {
if (a[i] - pre > 2 * mid) {
cnt++;
pre = a[i];
}
}
cnt++;
return cnt <= k;
}
int main() {
std::ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
cin >> n >> k;
for (int i = 0;i < n;i++) cin >> a[i];
sort(a, a + n);
int l = 1, r = (a[n - 1] - a[0] + 1) / 2;
while (l <= r) {
int mid = l + r >> 1;
if (check(mid)) r = mid - 1;
else l = mid + 1;
}
cout << l << '\n';
return 0;
}
NC24017 [USACO 2016 Jan S]Angry Cows的更多相关文章
- USACO 2016 January Contest, Gold解题报告
1.Angry Cows http://www.usaco.org/index.php?page=viewproblem2&cpid=597 dp题+vector数组运用 将从左向右与从右向左 ...
- NC25043 [USACO 2007 Jan S]Protecting the Flowers
NC25043 [USACO 2007 Jan S]Protecting the Flowers 题目 题目描述 Farmer John went to cut some wood and left ...
- USACO翻译:USACO 2012 JAN三题(2)
USACO 2012 JAN(题目二) 一.题目概览 中文题目名称 叠干草 分干草 奶牛联盟 英文题目名称 stacking baleshare cowrun 可执行文件名 stacking bale ...
- USACO翻译:USACO 2012 JAN三题(1)
USACO 2012 JAN(题目一) 一.题目概览 中文题目名称 礼物 配送路线 游戏组合技 英文题目名称 gifts delivery combos 可执行文件名 gifts delivery c ...
- USACO翻译:USACO 2013 JAN三题(1)
USACO 2013 JAN 一.题目概览 中文题目名称 镜子 栅栏油漆 奶牛排队 英文题目名称 mirrors paint lineup 可执行文件名 mirrors paint lineup 输入 ...
- USACO翻译:USACO 2014 JAN三题(2)
USACO 2014 JAN 一.题目概览 中文题目名称 队伍平衡 滑雪录像 滑雪场建设 英文题目名称 bteams recording skicourse 可执行文件名 bteams recordi ...
- USACO翻译:USACO 2014 JAN三题(1)
USACO 2014 JAN 一.题目概览 中文题目名称 滑雪场设计 滑雪降速 滑雪场评级 英文题目名称 skidesign slowdown skilevel 可执行文件名 skidesign sl ...
- USACO2016 January Gold Angry Cows
Angry Cows 题目描述:给出数轴上的\(n\)个整点\((a[i])\),现在要在数轴上选一个位置\(x\)(可以是实数),以及一个半径\(R\),在以\(x\)为中心,半径为\(R\)的范围 ...
- Usaco 2019 Jan Platinum
Usaco 2019 Jan Platinum 要不是昨天老师给我们考了这套题,我都不知道usaco还有铂金这么一级. 插播一则新闻:杨神坚持认为铂金比黄金简单,原因竟是:铜 汞 银 铂 金(金属活动 ...
随机推荐
- go语言编译过程概述
go语言编译过程概述 总结自<go语言设计与实现> 名词解释: 中间代码 中间代码是编译器或者虚拟机使用的语言,它可以来帮助我们分析计算机程序.在编译过程中,编译器会在将源代码转换到机器码 ...
- Ubuntu 下 Mariadb 数据库的安装和目录迁移
Ubuntu 下 Mariadb 数据库的安装和目录迁移 1.简介 本文主要是 Ubuntu 下 Mariadb 数据库的安装和目录迁移,同样适用于 Debian 系统:Ubuntu 20.0.4 M ...
- CSS 字体超出 省略 ... 展示
效果 /* 标题 */ .title_t{ color: #000000; font-size: 130%; display: inline-block; line-height: 30px; wid ...
- Mysql数据库基础_复习思维导图
Mysql复习的一个小总结,用xmind写的.(字数没有都不给我发博客) 下面是一些备注 子查询 MySQL子查询称为内部查询,而包含子查询的查询称为外部查询. 子查询可以在使用表达式的任何地方使用, ...
- spring boot 统一接口异常返回值
创建业务 Exception 一般在实际项目中,推荐创建自己的 Exception 类型,这样在后期会更容易处理,也比较方便统一,否则,可能每个人都抛出自己喜欢的异常类型,而造成代码混乱 Servic ...
- Vagrant之CentOS
Vagrant之CentOS Vagrant官网 https://www.vagrantup.com https://app.vagrantup.com/boxes/search https://ap ...
- 好客租房1-React基础目标
学习目标 能够说出React是什么 掌握react的特点 掌握react的基本使用 能够使用react脚手架 学习目录 react概述 react基本使用 react脚手架
- L2M-GAN: Learning to Manipulate Latent Space Semantics for Facial Attribute Editing阅读笔记
L2M-GAN: Learning to Manipulate Latent Space Semantics for Facial Attribute Editing 2021 CVPR L2M-GA ...
- Android——RelativeLayout
代码:<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android= ...
- Kafka 万亿级消息实践之资源组流量掉零故障排查分析
作者:vivo 互联网服务器团队-Luo Mingbo 一.Kafka 集群部署架构 为了让读者能与小编在后续的问题分析中有更好的共鸣,小编先与各位读者朋友对齐一下我们 Kafka 集群的部署架构及服 ...