当前位置:网站首页>POJ - 2456 aggressive cows
POJ - 2456 aggressive cows
2022-04-22 04:42:00 【GHOSTANDBREAD】
Title Description
Farmer John Built a long corral , It includes N (2 ≤ N ≤ 100,000) Compartments , The compartments are numbered in turn x1,…,xN(0 ≤ xi ≤ 1,000,000,000). however ,John Of C(2 ≤ C ≤ N) The cows don't like this layout , And a few cows in a cubicle , They're going to fight . In order not to let cattle hurt each other .John Decide to allocate compartments to the cattle , Make the minimum distance between any two cows as large as possible , that , What is the maximum value of this minimum distance ?
Input format
first line : Two integers separated by spaces N and C ;
The second line --- The first N+1 That's ok :i+1 The line points out xi The location of .
Output format
An integer , Maximum value of minimum distance .
sample input
5 3
1 2 8 4 9
sample output
3
Sample explanation
Put the cow in 1,4,8 So the minimum distance is 3.
The code is as follows :
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
int n, m;
const int N = 1e5 + 10;
int a[N];
bool c(int x) {
int last = 0; // Initialize to the subscript of the position of the first cowshed
for(int i = 1; i < m; i ++) {
// i from 1 Start judging , Not from 0 Start , Because I compared the first cowshed with the second cowshed at the beginning
int crt = last + 1;
while(crt < n && a[crt] - a[last] < x) {
crt ++;
}
if(crt == n) return false;// At this time, the cowshed has been used up , But the cow hasn't finished yet , So failure
last = crt; // When a[crt] - a[last] >= x, When it shows that you can put down a cow , take last Updated to crt
}
return true; // When all the cattle can be released , Show success , return true
}
void solve() {
sort(a, a + n);
int l = 0, r = a[n - 1] - a[0];
int mid;
while(l < r) { // Standard bisection template
mid = (l + r) >> 1;
if(c(mid)) l = mid + 1; // Analyze, judge and choose according to the specific situation
else r = mid;
}
cout << l - 1;
/*
Suppose the last answer is true mid, here l = mid + 1 , Then the next one must not be established and l = r sign out ,
The answer is l - 1
*/
}
int main()
{
scanf("%d %d", &n, &m);
for(int i = 0; i < n; i ++) scanf("%d", &a[i]);
solve();
return 0;
}
版权声明
本文为[GHOSTANDBREAD]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204220437309963.html
边栏推荐
- Objectmapper, stop being new like a second product
- Convert a matrix into a sparse matrix, and then convert a sparse matrix into a matrix (Part I)
- Will the expired data of redis be deleted immediately?
- LeetCode 剑指 Offer 43. 1~n 整数中 1 出现的次数***
- Statistics of authoritative institutions: the best data center network company in 2021, China Huawei and H3C, were listed
- Random number of unity
- 10. Libevent receives and processes server messages
- H7-tool releases firmware v2 15. For offline recording, the full series SPI flash of Renesas, Hetai and is25wp are added (2022-04-14)
- DNS domain name system - directory service of the Internet
- How expensive is the "salary" of software testing industry?
猜你喜欢

Solution to Chinese translation of GoLand (in case of failure to download plug-ins)

Mapbox creates multiple draggable marker points

2022 a special equipment related management (elevator) retraining question bank and answers

Paper reading (48): a library of optimization algorithms for organizational design

DS18B20 temperature sensor based on 51 single chip microcomputer

JVM shorthand

Jupiter notebook modifying the default opening path

vue项目 npm run build 打包项目时为css、js文件加时间戳版本号,防止浏览器缓存
![[twelfth database operation - stored procedure]](/img/8f/5ca161db5ffb465dddb9d423418fad.png)
[twelfth database operation - stored procedure]

6_ Data analysis modeling
随机推荐
STL的unique函数返回值
Financial retail map - transaction flow warning map
rpc error: code = Unavailable desc = error reading from server: EOF
10人小团队创业,如今收入过亿,不融资的简道云是怎么做到的?
Linked list Chapter 4
2022T电梯修理考试试题及在线模拟考试
ES next相关
Linked list Part III
权威机构统计:2021 年最佳数据中心网络公司,中国华为和H3C上榜
使用指定显卡
How much do you know about the testing methods of software testing?
pycharm+anaconda安装包
树莓派4B编译paddlelite(详细步骤2022年)
Shell variables $, $@, $0, $1, $2, ${},%% use explanation and easy-to-use shell formatting tools
目标检测--轻量级网络(截至2022-04-21)
-bash: /home/lylg/bin/kf. sh: /bin/bash^M: bad interpreter: No such file or directory
13. Bufferevent receives and sends data
Queue II
Will the expired data of redis be deleted immediately?
5_ Data analysis - Data Visualization