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还有铂金这么一级. 插播一则新闻:杨神坚持认为铂金比黄金简单,原因竟是:铜 汞 银 铂 金(金属活动 ...
随机推荐
- 2021.11.09 P3435 [POI2006]OKR-Periods of Words(KMP)
2021.11.09 P3435 [POI2006]OKR-Periods of Words(KMP) https://www.luogu.com.cn/problem/P3435 题意: 对于一个仅 ...
- CDN绕过
信息收集_CDN绕过 什么是CDN?为什么要绕过? CDN全称是内容分发网络(content delivery network).其目的是让用户能够更快速的得到请求的数据. 网上找了一张图片, ...
- 非关系型数据库Nosql的优缺点分析
Nosql的全称是Not Only Sql,Nosql指的是非关系型数据库,而我们常用的都是关系型数据库.就像我们常用的mysql,oralce.sqlserver等一样,这些数据库一般用来存储重要信 ...
- XCTF练习题---MISC---Janos-the-Ripper
XCTF练习题---MISC---János-the-Ripper flag:flag{ev3n::y0u::bru7us?!} 解题步骤: 1.观察题目,下载附件 2.发现是压缩包,进行解压,是一个 ...
- javascript生成一棵树
问题描述 输入一串父子节点对的数组,利用其构造一颗树 输入 const arr = [ {id:1,parentid:null}, {id:2,parentid:1}, {id:3,parentid: ...
- Hadoop(二)Hdfs基本操作
HDFS HDFS由大量服务器组成存储集群,将数据进行分片与副本,实现高容错. 而分片最小的单位就是块.默认块的大小是64M. HDFS Cli操作 官网https://hadoop.apache.o ...
- VS Code 真的会一统江湖吗?
关注「开源Linux」,选择"设为星标" 回复「学习」,有我为您特别筛选的学习资料~ 作者 | ROBEN KLEENE / 策划 | 万佳原文链接:https://blog.ro ...
- 服务器 CPU 100% 异常排查实践与总结
一个执着于技术的公众号 问题背景 昨天下午突然收到运维邮件报警,显示数据平台服务器cpu利用率达到了98.94%,而且最近一段时间一直持续在70%以上,看起来像是硬件资源到瓶颈需要扩容了,但仔细思考就 ...
- Git 后续——分支与协作
Git 后续--分支与协作 本文写于 2020 年 9 月 1 日 之前一篇文章写了 Git 的基础用法,但那其实只是「单机模式」,Git 之所以在今天被如此广泛的运用,是脱不开分支系统这一概念的. ...
- Spring是如何整合JUnit的?JUnit源码关联延伸阅读
上一篇我们回答了之前在梳理流程时遇到的一些问题,并思考了为什么要这么设计. 本篇是<如何高效阅读源码>专题的第十二篇,通过项目之间的联系来进行扩展阅读,通过项目与项目之间的联系更好的理解项 ...