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. MVC开发模式的数据运行流程

    对于java中经典的开发模式MVC,有一些感触!现说一下Java中数据的运行流程,由于我技术有限,有错的话欢迎提出,不喜勿喷! 我们知道在MVC开发模式,包括三部分视图层V(view).控制层C(Co ...

  2. c#高级编程第七版 学习笔记 第二章 核心c#

    第二章 核心C# 本章内容: 声明变量 变量的初始化和作用域 C#的预定义数据类型 在c#程序中使用条件语句.循环和跳转语句执行流 枚举 名称空间 Main()方法 基本的命令行c#编译器选项 使用S ...

  3. shell实现自动部署两台tomcat项目Ⅱ

    本次分为3个脚本, scp.sh放进第一台机器(负责传输文件), schenglee.sh放进第一台机器(自动部署), schenglee2.sh放进第二台机器(自动部署) 环境 tomcat1: 1 ...

  4. luoguP1281 书的复制 DP,贪心

    luoguP1281 书的复制 链接 https://www.luogu.org/problemnew/show/P1281 思路 简单dp,输出方案. 很明显dp记录路径对不对? 恭喜你死了. 求出 ...

  5. Spring-AOP环绕监听出错

    Exception in thread "main" org.springframework.aop.AopInvocationException: Null return val ...

  6. WebApi 后台获取token值

    前台传递一个token,后台不知道怎么获取那么不是很悲剧吗. $(function () { $.ajax({ url: "/api/TokensTest/FirstCode", ...

  7. eclipse下的mybatis插件:MyBatipse

    MyBatipse:Eclipse 下的 Mybatis插件 MyBatipse是Eclipse的一个插件,提供了内容提示和Mybatis的配置文件验证功能: 特点 XMLEditor 的增强 代码自 ...

  8. JAVA中的责任链模式(CH02)

    对责任链CH01做出优化,解决耦合度太高问题 记得上一篇我们使用的是抽象类,然后用子类去继承的方法实现等级的桥接,从而发现了耦合度太高. 为了解决这个问题. 我们本次使用接口进行抽象,然后使用到一个” ...

  9. express+mockjs实现模拟后台数据发送

    前言: 大多数时候,前端会和后端同时进行开发,即在我们开发完页面的时候,很可能还不能立马进入联调阶段,这个时候,为了保证我们接口的有效性和代码的功能完整,我们可能需要模拟数据. 模拟数据方法 1.通过 ...

  10. 『PyTorch × TensorFlow』第十七弹_ResNet快速实现

    『TensorFlow』读书笔记_ResNet_V2 对比之前的复杂版本,这次的torch实现其实简单了不少,不过这和上面的代码实现逻辑过于复杂也有关系. 一.PyTorch实现 # Author : ...