POJ_2456
来源:互联网 发布:王菲歌词 知乎 编辑:程序博客网 时间:2024/05/24 05:42
Aggressive cows
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 11623 Accepted: 5693
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?
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
* 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 312849
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.
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.
#include <stdio.h>#include <iostream>#include <algorithm>#include <math.h>using namespace std;const int maxN = 100005;int N;int M;int cable[maxN];int sucess(int x){ int pre = 0; for (int i = 1; i < M; i++) { int current = pre + 1; while (current < N && ((cable[current] - cable[pre]) < x)) { current++; } if (current == N) { return 0; } pre = current; } return 1;}void slove(){ int low = 0; int high = cable[N-1]+1; while (high - low > 1) { int mid = (low + high) / 2; if (sucess(mid)) { low = mid; } else { high = mid; } } printf("%d\n", low);}int main(){ scanf("%d%d", &N, &M); for (int i = 0; i < N; i++) { cin >> cable[i]; } sort(cable, cable+N); /* for (int i = 0; i < N; i++) { cout << cable[i] << endl; } */ slove(); return 0;}
0 0
- POJ_2456
- PHP操作PDF文档输出
- 编译器中一些内置宏(方便调试)
- java查看字符串中有多少相同的字母,并且按顺序输出
- EasyOpenJTAG驱动无法正确安装解决方法(Win10)
- 【UNITY3D 游戏开发之七】C# 中的委托、事件、匿名函数、Lambda 表达式
- POJ_2456
- Android 自定义View实现圆形切图的效果
- [已解决]错误: 找不到或无法加载主类 Demo.class
- javascript实现html网页版的2048游戏源代码
- 【UNITY3D 游戏开发之八】Unity编译到iPhone运行 Collider 无法正常触发事件解决方案
- 个人记录-LeetCode 11. Container With Most Water
- 跳出循环与结束循环
- LeetCode 1. Two Sum (C++)
- Java 静态模块和构造方法