POJ 2456 Aggressive cows
Aggressive cows Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 24838 Accepted: 11537 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
USACO 2005 February Gold
二分,看代码能懂,没什么坑~
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int INF = 1e9 + 5;
const int maxn = 1e5 + 5;
int n, k, x[maxn];
int check(int d)
{
int last = 0;
for(int i = 1; i < k; i++)
{
int cur = last + 1;
while(cur < n && x[cur] - x[last] < d)
cur++;
if(cur == n) return 0;
last = cur;
}
return 1;
}
int main()
{
while(~scanf("%d%d", &n, &k))
{
for(int i = 0; i < n; i++)
scanf("%d", &x[i]);
sort(x, x+n);
int l = 0, r = INF;
while(r - l > 1)
{
int mid = (l + r) / 2;
if(check(mid)) l = mid;
else r = mid;
}
printf("%dn", l);
}
return 0;
}
- JavaScript 教程
- JavaScript 编辑工具
- JavaScript 与HTML
- JavaScript 与Java
- JavaScript 数据结构
- JavaScript 基本数据类型
- JavaScript 特殊数据类型
- JavaScript 运算符
- JavaScript typeof 运算符
- JavaScript 表达式
- JavaScript 类型转换
- JavaScript 基本语法
- JavaScript 注释
- Javascript 基本处理流程
- Javascript 选择结构
- Javascript if 语句
- Javascript if 语句的嵌套
- Javascript switch 语句
- Javascript 循环结构
- Javascript 循环结构实例
- Javascript 跳转语句
- Javascript 控制语句总结
- Javascript 函数介绍
- Javascript 函数的定义
- Javascript 函数调用
- Javascript 几种特殊的函数
- JavaScript 内置函数简介
- Javascript eval() 函数
- Javascript isFinite() 函数
- Javascript isNaN() 函数
- parseInt() 与 parseFloat()
- escape() 与 unescape()
- Javascript 字符串介绍
- Javascript length属性
- javascript 字符串函数
- Javascript 日期对象简介
- Javascript 日期对象用途
- Date 对象属性和方法
- Javascript 数组是什么
- Javascript 创建数组
- Javascript 数组赋值与取值
- Javascript 数组属性和方法
- Docker_000
- 如何应对面试官的JVM调优问题
- Docker_001
- Docker_002
- [PyQt Tutorial]2.一个Hello World程序
- Oracle设置开机自启
- Go_学习之Docke容器
- zabbix 监控项
- [PyQt Tutorial]4.使用Qt Designer
- [PyQt Tutorial]5.Signals & Slots(信号与槽)
- Docker数据共享与持久化
- [PyQt Tutorial]6.Layout Management(布局管理)
- Kubernetes入门
- [PyQt Tutorial]7.QDialog 类
- kubeadm快速部署kubernetes集群