当前位置:网站首页>1433:【例题1】愤怒的牛
1433:【例题1】愤怒的牛
2022-06-09 05:46:00 【c_yy_】
- 一本通传送门
- 二分法典型例题,求最小值最大,用二分就行
- 二分+判断可行
#include<bits/stdc++.h>
using namespace std;
const int N=1e5+3;
int n,m,x[N];
bool check(int d){
//判断是否可行
int cow=1,rgt=x[1]+d;
for(int i=2;i<=n;i++){
if(x[i]<rgt) continue;//距离小,下一个
++cow;//可行,牛数加一
rgt=x[i]+d;//下一个的位置
}
return cow>=m;//返回值
}
int main()
{
cin>>n>>m;
for(int i=1;i<=n;i++)
cin>>x[i];
sort(x+1,x+n+1);//牛舍位置输入时是无序的,需要排序
int l=0,r=x[n]-x[1];
while(l<=r){
int mid=(l+r)>>1;
if(check(mid)) l=mid+1;//向右二分
else r=mid-1;//向左二分
}
cout<<r<<endl;
return 0;
}
边栏推荐
- Locating memory problems using mat
- Yolov5-6.0 series | yolov5 module design
- Yolov5-6.0 series | yolov5 model network construction
- Quelles sont les informations contenues dans le certificat SSL?
- Gstreamer应用开发实战指南(一)
- 线程thread 中join方法解读
- YOLOv5的Tricks | 【Trick6】学习率调整策略(One Cycle Policy、余弦退火等)
- Creating a basic duilib window process
- Topic24——3. Longest substring without duplicate characters
- Wechat applet wx Getlocation location error information summary
猜你喜欢

What information does the SSL certificate contain?

SSL证书安装后网站还是显示不安全

Synchronized detailed parsing

Swagger basic use quick start

Xtrabackup backup and recovery

Alibaba cloud AI training camp MySQL foundation 1:

Mysql5 available clusters

Thread interrupted detailed parsing

YOLOv5的Tricks | 【Trick7】指数移动平均(Exponential Moving Average,EMA)

Interpretation of join method in thread
随机推荐
TCP error control, flow control, congestion control
微信小程序wx.getLocation定位错误信息汇总
Mysql5.7 one master multi slave configuration
reids 缓存与数据库数据不一致、缓存过期删除问题
SET DECIMAL_ V2=false and UDF error: cannot divide decimal by zero and incompatible return types decimal
Leetcode 1037.有效的回旋镖
Yolov5-6.0系列 | yolov5的模型网络构建
【论文】CBAM: Convolutional Block Attention Module
[it] Foxit PDF retention tool selection
Time stamp verification code signing method for code signing certificate
MySql使用模糊查询中文时查询语句出现乱码的解决
MySQL one master multi slave configuration centos7 pro test
Leetcode 929.独特的电子邮件地址
数据血缘用例与扩展实践
使用MAT进行内存问题定位
lambda匿名函数
Quelles sont les informations contenues dans le certificat SSL?
Rsync synchronous backup
MySql中事务详细理解学习(事务管理、事务隔离级别、事务传播机制)
代码签名证书的时间戳验证码签名方法