链接:http://poj.org/problem?id=2456

题意:一个数轴上n个点,每个点一个整数值,有c个奶牛,要放在这些点的某几个上,求怎么放可以使任意两个奶牛间距离的最小值最大,求这个最大值。

思路:仍然是最大化最小值,套路一样,二分最小距离,每次check即可

AC代码:

 #include<iostream>
#include<vector>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<cstring>
#include<queue>
#include<map>
using namespace std;
const int maxn = 1e5+;
typedef long long ll;
ll sta[maxn];
int N,C;
bool check(ll dis){
int sum = ;
int cur = ;
for(int i = ;i<N;i++){
if(sta[i] - sta[cur]>=dis){
sum++;
cur = i;
}
}
return sum>=C;
}
int main(){
while(cin>>N>>C){
for(int i = ;i<N;i++){
cin>>sta[i];
}
sort(sta,sta+N);
ll MAX = -;
for(int i = ;i<N;i++){
MAX = max(MAX,sta[i]-sta[i-]);
}
ll l = ,r = MAX *;
ll mid;
while(l<r){
mid = (+l+r)>>;
if(check(mid)){//sum>=C;
l = mid ;
}
else{
r = mid - ;
}
}
// cout<<l<<" "<<r<<endl;
cout<<l<<endl;
}
return ;
}

POJ2456 Aggressive cows(二分)的更多相关文章

  1. POJ2456 Aggressive cows 二分

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

  2. POJ2456 Aggressive cows(二分+贪心)

    如果C(d)为满足全部牛之间的距离都不小于d. 先对牛舍的位置排序,然后二分枚举d,寻找满足条件的d. #include<iostream> #include<cstdio> ...

  3. 二分法的应用:最大化最小值 POJ2456 Aggressive cows

    /* 二分法的应用:最大化最小值 POJ2456 Aggressive cows Time Limit: 1000MS Memory Limit: 65536K Total Submissions: ...

  4. 二分算法的应用——最大化最小值 POJ2456 Aggressive cows

    Aggressive cows Time Limit: 1000MS Memory Limit: 65536K Total Submissions: Accepted: Description Far ...

  5. POJ2456 Aggressive cows

    Aggressive cows 二分,关键是转化为二分! #include <cstdio> #include <algorithm> ; ; int N, C; int a[ ...

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

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

  7. POJ2456 Aggressive cows 2017-05-11 17:54 38人阅读 评论(0) 收藏

    Aggressive cows Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 13993   Accepted: 6775 ...

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

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

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

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

  10. poj2456 Aggressive cows(二分查找)

    https://vjudge.net/problem/POJ-2456 二分,从最大长度开始,不断折半试,如果牛全放下了,就是可行,修改下界,否则改上届. #include<iostream&g ...

随机推荐

  1. Python_2_daythree2_CyclicStructure

    """Function_1: for-in循环用for循环实现1~100之间的奇数求和 Time: 2020.1.27Author: YaoXie"" ...

  2. Windows电脑最最最常用快捷键

                            快捷键组合                    作用 Ctrl+A 全选 Ctrl+S 保存 Ctrl+Z 撤销 Ctrl+X 剪切 Ctrl+C 复 ...

  3. Linux_Centos7安装VNC实现远程桌面

    一.首先安装GNOME桌面 yum groupinstall -y "GNOME Desktop"# 安装完成后,修改默认启动方式为图形化界面systemctl set-defau ...

  4. urllib模块提供的urlretrieve()函数使用

    urllib模块提供的urlretrieve()函数,urlretrieve()方法直接将远程的数据下载到本地 注意:若是网站有反爬虫的话这个函数会返回 403 Forbidden 参数url:传入的 ...

  5. MST Unification CodeForces - 1108F

    #include<iostream> #include<cstring> #include<algorithm> using namespace std; ; in ...

  6. 题解 AT4556 【12/22】

    题目传送门. ___________ 主要思路 我们可以使用\(C++\)语言中的\(string\)字符串来解. 先定义一个字符串\(s\),并输入\(s\). string s; cin>& ...

  7. 微信小程序 --- 日历效果

    wxml部分: <view class='box1' style='width: {{ sysW * 7 }}px'> <view class='dateBox'>{{ yea ...

  8. ORACLE10G非归档模式下异机迁库(文件迁移)

    环境信息: 源库 目标库 操作系统 WIN7 WIN SVR 2012 R2 系统盘符 C,D,E,F C,D IP x.x.x.216 x.x.x.112 数据库版本 10.2.0.4.0 - 64 ...

  9. C 库函数 - sprintf()

    C 库函数 - sprintf() C 标准库 - <stdio.h> 描述 C 库函数 int sprintf(char *str, const char *format, ...) 发 ...

  10. Bootstrap 警告框(Alert)插件

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...