百练2456 Aggressive cows&典型二分
来源:互联网 发布:中级程序员面试题 编辑:程序博客网 时间:2024/05/17 06:09
2456:Aggressive cows
总时间限制: 1000ms 内存限制: 65536kB
描述
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?
输入
* Line 1: Two space-separated integers: N and C
* Lines 2..N+1: Line i+1 contains an integer stall location, xi
输出
* Line 1: One integer: the largest minimum distance
样例输入
5 3
1
2
8
4
9
样例输出
3
总时间限制: 1000ms 内存限制: 65536kB
描述
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?
输入
* Line 1: Two space-separated integers: N and C
* Lines 2..N+1: Line i+1 contains an integer stall location, xi
输出
* Line 1: One integer: the largest minimum distance
样例输入
5 3
1
2
8
4
9
样例输出
3
#include<iostream>#include<algorithm>#include<cstdio>using namespace std;int n,c;int a[1000005];bool ok(int x) //判断函数{ int i,res,la; res=1; la=0; i=1; while(i<n) {while(a[i]-a[la]<x) i++;if(i<n) res++; //是否出界的判断!if(res>=c) return true;la=i;i++; } return false; }int main(){ int i,l,r,mid,ans=0; scanf("%d%d",&n,&c);for(i=0;i<n;i++) scanf("%d",&a[i]);sort(a,a+n); //排序l=0; r=1000000000; while(l<=r){mid=(l+r)/2;if(ok(mid)){ans=mid; l=mid+1;}else r=mid-1;}printf("%d\n",ans);return 0;}
阅读全文
0 0
- 百练2456 Aggressive cows&典型二分
- 百练-2456-Aggressive cows
- POJ 2456 Aggressive cows 二分
- POJ 2456 Aggressive cows 二分
- poj 2456 Aggressive cows (二分)
- poj 2456 Aggressive cows 二分
- POJ - 2456 Aggressive cows 二分
- POJ 2456 Aggressive cows 【二分】
- POJ 2456 Aggressive cows(二分)
- POJ 2456 Aggressive cows 二分
- poj 2456 二分 Aggressive cows
- [POJ 2456]Aggressive cows[二分]
- 【POJ 2456 Aggressive cows】+ 二分
- poj 2456 Aggressive cows (二分)
- POJ 2456 Aggressive cows 二分
- pku 2456 Aggressive cows(二分数值测试)
- POJ 2456 Aggressive cows(二分)
- POJ 2456 - Aggressive cows(二分)
- STM32
- 深入剖析 mybatis 原理(二)
- java中spring+springMVC+Mybatis主流框架maven的pom.xml配置参考
- git 查看远程分支、本地分支、创建分支、把分支推到远程repository、删除本地分支
- Android TextView富文本深入探索
- 百练2456 Aggressive cows&典型二分
- C++2.0--范围for语句
- 这是一封写给明年这个时候的我的信!
- Java从Hbase取值分别转化为String和Map
- Java中的Web设计
- Hadoop-mapreduce reducer端多表合并
- 什么是僵尸进程,如何找到并杀掉僵尸进程?
- linux git diff 有颜色
- Spark核心RDD:combineByKey函数详解