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的更多相关文章

  1. USACO 2016 January Contest, Gold解题报告

    1.Angry Cows http://www.usaco.org/index.php?page=viewproblem2&cpid=597 dp题+vector数组运用 将从左向右与从右向左 ...

  2. 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 ...

  3. USACO翻译:USACO 2012 JAN三题(2)

    USACO 2012 JAN(题目二) 一.题目概览 中文题目名称 叠干草 分干草 奶牛联盟 英文题目名称 stacking baleshare cowrun 可执行文件名 stacking bale ...

  4. USACO翻译:USACO 2012 JAN三题(1)

    USACO 2012 JAN(题目一) 一.题目概览 中文题目名称 礼物 配送路线 游戏组合技 英文题目名称 gifts delivery combos 可执行文件名 gifts delivery c ...

  5. USACO翻译:USACO 2013 JAN三题(1)

    USACO 2013 JAN 一.题目概览 中文题目名称 镜子 栅栏油漆 奶牛排队 英文题目名称 mirrors paint lineup 可执行文件名 mirrors paint lineup 输入 ...

  6. USACO翻译:USACO 2014 JAN三题(2)

    USACO 2014 JAN 一.题目概览 中文题目名称 队伍平衡 滑雪录像 滑雪场建设 英文题目名称 bteams recording skicourse 可执行文件名 bteams recordi ...

  7. USACO翻译:USACO 2014 JAN三题(1)

    USACO 2014 JAN 一.题目概览 中文题目名称 滑雪场设计 滑雪降速 滑雪场评级 英文题目名称 skidesign slowdown skilevel 可执行文件名 skidesign sl ...

  8. USACO2016 January Gold Angry Cows

    Angry Cows 题目描述:给出数轴上的\(n\)个整点\((a[i])\),现在要在数轴上选一个位置\(x\)(可以是实数),以及一个半径\(R\),在以\(x\)为中心,半径为\(R\)的范围 ...

  9. Usaco 2019 Jan Platinum

    Usaco 2019 Jan Platinum 要不是昨天老师给我们考了这套题,我都不知道usaco还有铂金这么一级. 插播一则新闻:杨神坚持认为铂金比黄金简单,原因竟是:铜 汞 银 铂 金(金属活动 ...

随机推荐

  1. Dom基础(一):attribute和properrty的区别

    properrty:修改对象属性不会体现到html结构中,针对DOM节点自带属性(id,className,style) attribute:修改html属性,会改变html结构,大多可以添加自定义属 ...

  2. 【第六课】SHELL和计划任务(学习笔记)

    4月15日学习笔记

  3. LintCode-282 · 解压字符串-题解

    题目链接:https://www.lintcode.com/problem/282/description?_from=collection&fromId=208描述:小Q想要给他的朋友发送一 ...

  4. 【CSAPP】Shell Lab 实验笔记

    shlab这节是要求写个支持任务(job)功能的简易shell,主要考察了linux信号机制的相关内容.难度上如果熟读了<CSAPP>的"异常控制流"一章,应该是可以不 ...

  5. 【PyHacker】编写WAF指纹探测与Sqlmap相结合

    使用Python编写探测WAF指纹脚本,再结合到Sqlmap中,这样以后再探测网站时,如果识别到此WAF指纹,就会显示出来.本文属于巡安似海PyHacker系列课程   编写探测识别WAF脚本 00x ...

  6. 『现学现忘』Git基础 — 26、给Git命令设置别名

    目录 1.什么是Git命令的别名 2.别名的全局配置 3.别名的局部配置 4.删除所有别名 5.小练习 1.什么是Git命令的别名 Git中命令很多,有些命令比较长,有些命令也不好记,也容易写错. 例 ...

  7. 1.还不会部署高可用的kubernetes集群?看我手把手教你使用二进制部署v1.23.6的K8S集群实践(上)

    公众号关注「WeiyiGeek」 设为「特别关注」,每天带你玩转网络安全运维.应用开发.物联网IOT学习! 本章目录: 0x00 前言简述 0x01 环境准备 主机规划 软件版本 网络规划 0x02 ...

  8. CentOS6安装PLEX

    CentOS6安装PLEX 一.安装使用PLEX 1.1 安装 1)下载 https://www.plex.tv/downloads/ plexmediaserver-1.11.3.4803-c40b ...

  9. 汇编语言中loop循环编程

    (1)向内存0:200~ 0:23f依次传送数据0~63(3FH) (2)同上简化后的代码,要求九行以内

  10. 第06组 Beta冲刺 (2/5)

    目录 1.1 基本情况 1.2 冲刺概况汇报 1.郝雷明 2. 方梓涵 3.杜筱 4.黄少丹 5. 董翔云 6.鲍凌函 7.詹鑫冰 8.曹兰英 9.曾丽莉 10.吴沅静 1.3 冲刺成果展示 1.1 ...