poj2456
2024-09-01 09:59:04
Aggressive cows
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 24979 | Accepted: 11594 |
Description
Farmer John has built a new long barn, with N (2 <= N <= 100,000) stalls. The stalls are located along a straight line at positions x1,...,xN (0 <= xi <= 1,000,000,000).
His C (2 <= C <= N) cows don't like this barn layout and become aggressive towards each other once put into a stall. To prevent the cows from hurting each other, FJ want to assign the cows to the stalls, such that the minimum distance between any two of them is as large as possible. What is the largest minimum distance?
Input
* Line 1: Two space-separated integers: N and C
* Lines 2..N+1: Line i+1 contains an integer stall location, xi
Output
* Line 1: One integer: the largest minimum distance
Sample Input
5 3
1
2
8
4
9
Sample Output
3
Hint
OUTPUT DETAILS:
FJ can put his 3 cows in the stalls at positions 1, 4 and 8, resulting in a minimum distance of 3.
Huge input data,scanf is recommended.
Source
一句话题意:
有n个牛栏,k头牛。将这k头牛放入牛栏中,如何保证牛与牛之间最小的距离最大。
解析:
明显的是二分算法。
L=0;R= 最大值
#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
const int maxn=;
int n,k;
int a[maxn];
int check(int x){
int cnt=;
int y=a[];
for(int i=;i<=n;i++)
if(a[i]-y>=x){
cnt++;y=a[i];
if(cnt>=k) return ;
}
return ;
}
int main(){
cin>>n>>k;
for(int i=;i<=n;i++){
cin>>a[i];
}
sort(a+,a++n);
int l=,r=a[n],mid;
while (l<r){
mid=(l+r+)/;
if(check(mid)) l=mid;
else r=mid-;
}
cout<<l<<endl;
return ;
}
最新文章
- SQLAlchemy(一)
- 更改pip安装源的镜像解决安装总是timeout的情况
- 【转】Web Service单元测试工具实例介绍之SoapUI
- 增加Activity Monitor中的作业保存数量
- c#sqlhelper之用法
- iLinuxBot: Designing Botnets to Manage Linux Clients
- Codevs 1063 合并果子
- Centos7安装Xmind
- 百度云管家-V4.6.1-单文件版绿色版
- uva 11997 K Smallest Sums 优先队列处理多路归并问题
- shell参数传递
- Spring Security(三十六):12. Spring MVC Test Integration
- 基于OpenCV做“三维重建”(2)--封装标定过程
- 如何用ABP框架快速完成项目(13) - 用ABP遇到难题项目受阻时如何避免项目延迟
- cf1114D 区间dp基础
- Python特色数据类型(列表)(上)
- XPath简介及节点
- 微信小程序https发起请求失败的解决方法
- 利用 Azure Devops 创建和发布 Nuget 包
- flask中邮件发送方法