Aggressive cows

Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 23866   Accepted: 11141

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.

 

尝试了剪枝 结果运行时间并没有什么区别(注释掉的地方是剪枝)

#include <stdio.h>
#include <iostream>
#include <cstring>
#include <vector>
#include <algorithm>
const int si = 100010, inf = 1000000000 + 10;
using namespace std;
int N, COW;
int ar[si]; bool c(int dis) {
int pre = 0, finished = 1;
for (int i = 1; i < N && finished < COW; i++) {
//if (ar[N - 1] - ar[pre] < dis * (COW - finished)) return false;
//剪枝 剩下COW - finished头牛 每头牛需要dis的距离
if (ar[i] - ar[pre] >= dis) {
finished++;
pre = i;
}
}
return finished >= COW;
}
int main() {
cin >> N >> COW;
for (int i = 0; i < N; i++) scanf("%d", &ar[i]);;
sort(ar, ar + N);
int l = 1, m, r = inf;
while (l < r - 1) {
m = l + r >> 1;
if (c(m)) l = m;
else r = m;
}
cout << l << endl;
return 0;
}

2456 Aggressive cows的更多相关文章

  1. poj 2456 Aggressive cows && nyoj 疯牛 最大化最小值 二分

    poj 2456 Aggressive cows && nyoj 疯牛 最大化最小值 二分 题目链接: nyoj : http://acm.nyist.net/JudgeOnline/ ...

  2. 二分搜索 POJ 2456 Aggressive cows

    题目传送门 /* 二分搜索:搜索安排最近牛的距离不小于d */ #include <cstdio> #include <algorithm> #include <cmat ...

  3. POJ 2456 Aggressive cows (二分 基础)

    Aggressive cows Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7924   Accepted: 3959 D ...

  4. [ACM] poj 2456 Aggressive cows (二分查找)

    Aggressive cows Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 5436   Accepted: 2720 D ...

  5. POJ 2456 Aggressive cows

    Aggressive cows Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 11192   Accepted: 5492 ...

  6. POJ 2456 Aggressive cows(二分答案)

    Aggressive cows Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 22674 Accepted: 10636 Des ...

  7. POJ - 2456 Aggressive cows 二分 最大化最小值

    Aggressive cows Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 18099   Accepted: 8619 ...

  8. poj 2456 Aggressive cows 贪心+二分

    Aggressive cows Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 25944   Accepted: 11982 ...

  9. POJ 2456 Aggressive cows(贪心 + 二分)

    原题链接:Aggressive cows 题目大意:农夫 建造了一座很长的畜栏,它包括  个隔间,这些小隔间依次编号为. 但是, 的  头牛们并不喜欢这种布局,而且几头牛放在一个隔间里,他们就要发生争 ...

随机推荐

  1. springboot使用hibernate validator校验

    一.参数校验 在开发中经常需要写一些字段校验的代码,比如字段非空,字段长度限制,邮箱格式验证等等,写这些与业务逻辑关系不大的代码个人感觉有两个麻烦: 验证代码繁琐,重复劳动 方法内代码显得冗长 每次要 ...

  2. JavaScript Dom 事件

    JavaScript  Dom 事件 对于事件需要注意的要点: // this标签当前正在操作的标签. this // event封装了当前事件的内容. even 常用事件 // 鼠标单击.触发事件 ...

  3. Linux 查看系统状态

    查看系统状态 命令:vmstat 命令:vmsta 1 10               #每1秒钟1次显示10次. r:几个进程在占用cpu b:等待IO值 Swpd:多少交换内存 free:剩余内 ...

  4. java限制map大小,并FIFO淘汰

    有时候需要往一个MAP中写入一些记录,但又怕无限制地写入会导致内存爆掉,所以得限制这个MAP的大小. 实现:LinkedHashMap提供了简单的方法. 首先,定义一个最大数,比如1000,然后new ...

  5. [CodeForces 1141A] Game 23

    题目链接:http://codeforces.com/problemset/problem/1141/A 首先,nn一定要是mm的倍数.否则就无法转换,输出-1: 然后令k=.\frac{m}{n} ...

  6. jQuery validator plugin之Plugin Method

    原文 .validate() validate( [options ] ) options Type: Object debug (default: false) Type: Boolean Enab ...

  7. win10 处理程序“ExtensionlessUrlHandler-Integrated-4.0”在其模块列表中有一个错误模块“ManagedPipelineHandler”

    更新Win10,原来的IIS站点访问不了,原因是因为IIS 没有.net 4.5,使用网上的aspnet_regiis.exe -i命令,一点都不靠谱,直接提示: C:\WINDOWS\system3 ...

  8. CSS实现输入框宽度随内容自适应效果

    有时候我们会遇到如下需求:输入框的宽度随内容长度自适应,当输入框宽度增大到一定值时,里边的内容自动隐藏. 面对这种需求,我们首先想到的是使用input元素标签,但是发现input标签的宽度默认设定的是 ...

  9. .gitlab-ci.yml简介

    关键字   script 由Runner执行的Shell脚本. image 使用docker镜像,  image:name service 使用docker  services镜像, services ...

  10. SpringBoot MyBatis 配置多数据源 (静态多个)

    转载地址:https://www.jianshu.com/p/118ca1d5ecf9?utm_campaign=haruki&utm_content=note&utm_medium= ...