题意:把c个牛分进n个摊位,摊位位置已知,所有摊位分布在0 <= xi <= 1,000,000,000,问两头牛间最小距离的最大值。

分析:找所有最小距离取个最大的。所以二分找这个最小的距离,这个最大的最小距离是二分的分界线。贪心来判断当前的最小距离是否能安排下所有的牛。

#pragma comment(linker, "/STACK:102400000, 102400000")
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cctype>
#include<cmath>
#include<iostream>
#include<sstream>
#include<iterator>
#include<algorithm>
#include<string>
#include<vector>
#include<set>
#include<map>
#include<stack>
#include<deque>
#include<queue>
#include<list>
#define Min(a, b) ((a < b) ? a : b)
#define Max(a, b) ((a < b) ? b : a)
typedef long long ll;
typedef unsigned long long llu;
const int INT_INF = 0x3f3f3f3f;
const int INT_M_INF = 0x7f7f7f7f;
const ll LL_INF = 0x3f3f3f3f3f3f3f3f;
const ll LL_M_INF = 0x7f7f7f7f7f7f7f7f;
const int dr[] = {, , -, , -, -, , };
const int dc[] = {-, , , , -, , -, };
const int MOD = 1e9 + ;
const double pi = acos(-1.0);
const double eps = 1e-;
const int MAXN = + ;
const int MAXT = + ;
using namespace std;
int a[MAXN];
int n, c;
bool judge(int x){
int cnt = ;
int st = a[];
for(int i = ; i < n; ++i){
if(a[i] - st >= x){
++cnt;
st = a[i];
}
if(cnt == c) return true;
}
return false;
}
int solve(){
int l = , r = 1e9;
while(l < r){
int mid = l + (r - l + ) / ;
if(judge(mid)) l = mid;
else r = mid - ;
}
return l;
}
int main(){
while(scanf("%d%d", &n, &c) == ){
memset(a, , sizeof a);
for(int i = ; i < n; ++i){
scanf("%d", &a[i]);
}
sort(a, a + n);
int ans = solve();
printf("%d\n", ans);
}
return ;
}

POJ - 2456 Aggressive cows(二分+贪心)的更多相关文章

  1. POJ 2456 Aggressive cows ( 二分 && 贪心 )

    题意 : 农夫 John 建造了一座很长的畜栏,它包括N (2 <= N <= 100,000)个隔间,这些小隔间依次编号为x1,...,xN (0 <= xi <= 1e9) ...

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

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

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

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

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

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

  5. poj 2456 Aggressive cows 二分 题解《挑战程序设计竞赛》

    地址 http://poj.org/problem?id=2456 解法 使用二分逐个尝试间隔距离 能否满足要求 检验是否满足要求的函数 使用的思想是贪心 第一个点放一头牛 后面大于等于尝试的距离才放 ...

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

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

  7. [poj 2456] Aggressive cows 二分

    Description Farmer John has built a new long barn, with N (2 <= N <= 100,000) stalls. The stal ...

  8. [POJ] 2456 Aggressive cows (二分查找)

    题目地址:http://poj.org/problem?id=2456 最大化最小值问题.二分牛之间的间距,然后验证. #include<cstdio> #include<iostr ...

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

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

  10. 二分搜索 POJ 2456 Aggressive cows

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

随机推荐

  1. ch8 让div居中--使用外边距

    假设有一个布局,希望让其中的容器div在屏幕上水平居中,则只需要定义div的宽度,然后将水平外边距设置为auto <body> <div class="wrapper&qu ...

  2. 使用 JvisualVM 监控 spark executor

    使用 JvisualVM,需要先配置 java 的启动参数 jmx 正常情况下,如下配置 -Dcom.sun.management.jmxremote -Dcom.sun.management.jmx ...

  3. Law of large numbers and Central limit theorem

    大数定律 Law of large numbers (LLN) 虽然名字是 Law,但其实是严格证明过的 Theorem weak law of large number (Khinchin's la ...

  4. 解决vue更新默认值时出现的闪屏问题

    在Vue项目中,对于一个展示用户个人信息的页面.有以下需求,需要判断用户个人信息是否填充过,如果填充过,需要在页面中展示已填充项(未填充项不展示):如果未填充过,需要在页面中显示另外一种元素(提示用“ ...

  5. jforum发表文章和回复文章显示中文乱码问题

    jforum安装时,在数据库创建jforum数据库的时候,设置了字符集(CREATE DATABASE jforum DEFAULT CHARACTER SET utf8 COLLATE utf8_g ...

  6. 报警视图 报警窗口 报警指示器 的组态 PLC变量 事故信息 MW16 报警确认变量 MW18 转速变量 MW20 温度变量 MW22 用 M17.0 来模拟事故信息的最低位。用PLCSIM 给定温度为800 度 报警视图与报警窗口显示故障

    组态报警并用PLCSIM进行仿真 步骤1 : 组态离散变量报警 在PLC 的默认变量表中 创建变量"事故信息" 数据类型是word 绝对地址是MW16 同时建立 报警确认变量 MW ...

  7. 每天一点点之vue框架 watch监听变量(深度监听)

    <div> <p>FullName: {{fullName}}</p> <p>FirstName: <input type="text& ...

  8. Keras入门——(4)长短期记忆网络LSTM(一)

    参考: https://blog.csdn.net/zwqjoy/article/details/80493341 https://blog.csdn.net/u012735708/article/d ...

  9. 第1节 kafka消息队列:10、flume与kafka的整合使用

    11.flume与kafka的整合 实现flume监控某个目录下面的所有文件,然后将文件收集发送到kafka消息系统中 第一步:flume下载地址 http://archive.cloudera.co ...

  10. swift4之String与NSString的区别与使用

    String是结构体,NSString是类,这是它们的根本区别. 在 Swift 中,结构体struct是值类型,String是结构体,所以也是值类型.值类型被赋予给一个变量.常量或者被传递给一个函数 ...