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

题解

知识点:二分。

显然二分半径,由于覆盖范围是 [xR,x+R]xR 开始到另一点 x+R 一共距离 2R ,所以从之前一个点 apre 开始,到某个点 apos 的距离满足 aposapre>2R 时,则让 apos 成为下一个 apre ,然后区域数 cnt 加一。如果最后 cntk ,则说明可行;cnt>k,则说明不可行。

时间复杂度 O(nloga[n1]a[0]2)

空间复杂度 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;
}
posted @   空白菌  阅读(64)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
点击右上角即可分享
微信分享提示