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. Golang : flag 包简介

    在 Golang 程序中有很多种方法来处理命令行参数.简单的情况下可以不使用任何库,直接处理 os.Args:其实 Golang 的标准库提供了 flag 包来处理命令行参数:还有第三方提供的处理命令 ...

  2. Weekly Contest 111-------->942. DI String Match

    Given a string S that only contains "I" (increase) or "D" (decrease), let N = S. ...

  3. 修改jq weui自定义对话框点击确定按钮不关闭对话框

    如果我们在对话框给用户输入值时,当用户输入空值点击确定按钮时,应该给个提示然后让用户继续输入. 如果在方法里使用 return false;,结果用户输入空值时对话框还是会关闭.正确做法如下: 先设置 ...

  4. Python学习 Part2:深入Python函数定义

    在Python中,可以定义包含若干参数的函数,这里有几种可用的形式,也可以混合使用: 1. 默认参数 最常用的一种形式是为一个或多个参数指定默认值. >>> def ask_ok(p ...

  5. 密码破解工具John the Ripper使用说明

    John the Ripper John 包描述 John the Ripper 既功能丰富又运行快速. 它在一个程序中结合了几种破解模式,并且可以根据您的特定需求进行全面地配置(你甚至可以使用支持C ...

  6. LeetCode.897-递增搜索树(Increasing Order Search Tree)

    这是悦乐书的第346次更新,第370篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第211题(顺位题号是897).给定一棵树,按中序遍历顺序重新排列树,以便树中最左边的节 ...

  7. BZOJ3224普通平衡树

    洛谷题面链接 很早就过了,太久没打了,原本是在noip前用来练emacs的手感的. noip炸了,就滚回来更博客了(安排的计数任务刷不动,学不会容斥,打发时间...) 众所周知,splay是个好算法, ...

  8. SOA思想

    参考:https://www.cnblogs.com/renzhitian/p/6853289.html 是什么 SOA service-oriented architecture 面向服务的体系结构 ...

  9. exec 和 eval

    exec exec语句用来执行储存在字符串或文件中的Python语句, 我们可以运行一个包含Python语句的字符串 >>> exec "print 'Hello Pyth ...

  10. redis开发小结

    随着缓存在web服务中用的越来越广泛,redis可以说成为了目前最流行的NoSQL数据库!redis与memcached最大的不同在于redis支持更多的数据类型,包括string.hash.list ...