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

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

  1. 5 3
  2. 1
  3. 2
  4. 8
  5. 4
  6. 9

Sample Output

  1. 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.

Source

 
 
 
解析:最大化最小值(二分)
 
 
 
  1. #include <cstdio>
  2. #include <algorithm>
  3. using namespace std;
  4.  
  5. const int MAXN = 100000+5;
  6. int a[MAXN];
  7. int n, c;
  8.  
  9. bool ok(int d)
  10. {
  11. int pre = 0;
  12. for(int i = 1; i < c; ++i){
  13. int cur = pre+1;
  14. while(cur < n && a[cur]-a[pre] < d)
  15. ++cur;
  16. if(cur == n)
  17. return false;
  18. pre = cur;
  19. }
  20. return true;
  21. }
  22.  
  23. int main()
  24. {
  25. scanf("%d%d", &n, &c);
  26. for(int i = 0; i < n; ++i)
  27. scanf("%d", &a[i]);
  28. sort(a, a+n);
  29. int l = 0, r = 0x7fffffff;
  30. while(r-l > 1){
  31. int mid = (l+r)/2;
  32. if(ok(mid)) l = mid;
  33. else r = mid;
  34. }
  35. printf("%d\n", l);
  36. return 0;
  37. }

  

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: 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. HDU4612 Warm up 边双连通分量&&桥&&树直径

    题目的意思很简单,给你一个已经连通的无向图,我们知道,图上不同的边连通分量之间有一定数量的桥,题目要求的就是要你再在这个图上加一条边,使得图的桥数目减到最少. 首先要做的就是找出桥,以及每个点所各自代 ...

  2. hdu 3790 最短路径问题(最短路,Dijsktra)

    题目 Dijsktra基础题,只是多了一个花费,稍稍变动处理就好 #define _CRT_SECURE_NO_WARNINGS #include<string.h> #include&l ...

  3. POJ2251Dungeon Master

    http://poj.org/problem?id=2251 题意 : 就是迷宫升级版,从以前的一个矩阵也就是一层,变为现在的L层," . "是可以走,但是“#”不可以走,从S走到 ...

  4. 用Eclipse编写运行Java程序

    1.选择一个空的文件夹,作为workspace工作空间,用来存放你以后用eclipse写的Java程序.(一个workspace可以放很多很多project项目) 2.新建java项目:File-&g ...

  5. mysql中bigint在php中表示

    http://bbs.csdn.net/topics/340266753 http://www.percona.com/blog/2008/01/10/php-vs-bigint-vs-float-c ...

  6. eclipse的安装

    eclipse的安装只要记住一条就好,那就是eclipse安装的时候版本要一致(就是,安装eclipse 32位的,同时要配套jdk 32位,tomcat 32位的),同理64位的也是一样的. 本人安 ...

  7. mysql备份恢复数据库据/表

    备份单个数据库,只备份表,如要恢复,必须先创建一个数据库[root@s]# mysqldump -u root -p dbname1 > dbname1.sql[root@s]# mysql - ...

  8. linux下对符合条件的文件大小做汇总统计的简单命令

    (1)统计当前目录下的 *txt 文件du -c -h *txt   (2)统计当前目录下的 *txt 文件, 并求出总大小du  *txt |awk 'BEGIN{count=0;size=0;} ...

  9. 一位ACM过来人的心得(转)

    励志下! 刻苦的训练我打算最后稍微提一下.主要说后者:什么是有效地训练? 我想说下我的理解.很多ACMer入门的时候,都被告知:要多做题,做个500多道就变牛了.其实,这既不是充分条件.也不会是必要条 ...

  10. 宏HASH_SEARCH

    /********************************************************************//** Looks for a struct in a ha ...