http://acm.nyist.net/JudgeOnline/problem.php?pid=586

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

解题思路:最大化最小值二分答案即可

 1 #include <stdio.h>
 2 #include <string.h>
 3 #include <stdlib.h>
 4 
 5 int a[];
 6 int n, c, i;
 7 
 8 int cmp(const void *a, const void *b){
 9     return *(int *)a - *(int *)b;
 }
 
 bool check(int x){
     int cnt = , temp = a[];
     for(i = ; i < n; i++){
         if(a[i] - temp > x){
             cnt++;
             temp = a[i];
             if(cnt >= c){//如果能装下c头牛
                 return true;
             }
         }
     }
     return false;
 }
 
 void solve(){
     //二分搜索最大的距离
     int l = , r = a[n - ] - a[];
     while(l <= r){
         int mid = (l + r) >> ;
         if(check(mid)){
             l = mid + ;
         }
         else{
             r = mid - ;
         }
     }
     printf("%d\n", l);
 }
 int main(){
     while(scanf("%d %d", &n, &c) != EOF){
         for(i = ; i < n; i++){
             scanf("%d", &a[i]);
         }
         qsort(a, n, sizeof(a[]), cmp);
         solve();
     }
     return ;

49 }

nyoj-586-疯牛|poj-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. POJ 2456 Aggressive cows (二分)

    题目传送门 POJ 2456 Description Farmer John has built a new long barn, with N (2 <= N <= 100,000) s ...

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

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

  6. POJ 2456 Aggressive cows

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

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

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

  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: 25944   Accepted: 11982 ...

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

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

随机推荐

  1. CSS3 制作魔方 - 玩转魔方

    在上一篇<CSS3 制作魔方 - 形成魔方>中介绍了一个完整魔方的绘制实现,本文将介绍魔方的玩转,支持上下左右每一层独立地旋转.先来一睹玩转的风采. 1.一个问题 由于魔方格的位置与转动的 ...

  2. 无法打开编译器生成的文件:“../../build/vs71/release/v100/MD_MBCS\json_value.

    1>正在生成代码 1>e:\Source\VC\?\json\jsoncpp-src-0.6.0-rc2\src\lib_json\json_value.cpp : fatal error ...

  3. CF939D Love Rescue

    题意 给定两个长度为n的由小写字母组成的字符串 每次可以花费1的代价,指定两个字母,把其中一个全部变为另一个 求使两个字符串相同的最小花费 n <= 100000 因为谁变成谁没有关系反正相等就 ...

  4. 康少带你python项目从部署到上线云服务器

    首先,服务器要安装nginx和mysql,网站文件建议放在/usr/local/www,环境python3.6+mysql5.7,阿里云的服务器可以用公共镜像有一个配置好的,不然就自己装一下环境吧. ...

  5. java后台杂记

    1.在@Select使用动态SQL有 "<=" 时要转义:<![CDATA[ <= ]]> 2.assert函数即断言,也即做出一些假设,程序运行时如果假设 ...

  6. (转)关于MongoDB你需要知道的几件事

    本文列举了颇让作者困惑的一些MongoDB限制,如果你也打算使用MongoDB,那么至少要提前了解这些限制,以免遇到的时候措手不及. 消耗磁盘空间 这是我的第一个困惑:MongoDB会消耗太多的磁盘空 ...

  7. [模板]manacher

    這麼簡單的算法現在才學...... https://segmentfault.com/a/1190000008484167?utm_source=tag-newest#articleHeader3 h ...

  8. 《统计学习方法》笔记九 EM算法及其推广

    本系列笔记内容参考来源为李航<统计学习方法> EM算法是一种迭代算法,用于含有隐变量的概率模型参数的极大似然估计或极大后验概率估计.迭代由 (1)E步:求期望 (2)M步:求极大 组成,称 ...

  9. MFC中的模态对话框与非模态对话框

    模态对话框创建: MyDialog mydlg; mydlg.DoModal() 当前只能运行此模态对话框,且停止主窗口的运行,直到模态对话框退出,才允许主窗口运行. 模态对话框的关闭顺序: OnCl ...

  10. 通用全局CSS样式

    PC全局样式 *{padding:0;margin:0;} div,dl,dt,dd,form,h1,h2,h3,h4,h5,h6,img,ol,ul,li,table,th,td,p,span,a{ ...