2018学校暑期集训第二天——ACM基础算法

例二  ——   POJ - 2456

Aggressive cows

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

5 3
1
2
8
4
9

Sample Output

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.


题意:有n个牛栏,选m个放进牛,相当于一条线段上有 n 个点,选取 m 个点,使得相邻点之间的最小距离值最大。

思路:贪心+二分
    二分枚举相邻两牛的间距,判断大于等于此间距下能否放进所有的牛。


PPT中的代码如下:

#include<iostream>
#include<algorithm>
#include<string>
#include<vector>
#include<cstdio>
#include<cmath>
#include<set>
#include<map>
using namespace std;
const int N = 1000010;
int a[N], n, m;bool judge(int k)    //枚举间距k,看能否使任意两相邻牛
{int cnt = a[0], num = 1; //num为1表示已经第一头牛放在a[0]牛栏中for(int i = 1; i < n; i ++)   //枚举剩下的牛栏{if(a[i] - cnt >= k)   //a[i]这个牛栏和上一个牛栏间距大于等于k,表示可以再放进牛{cnt = a[i];num ++;   //又放进了一头牛}if(num >= m) return true; //所有牛都放完了}return false;
}void solve()
{int l = 1, r = a[n-1] - a[0];    //最小距离为1,最大距离为牛栏编号最大的减去编号最小的while(l < r){int mid = (l+r) >> 1;if(judge(mid)) l = mid + 1;else r = mid;}printf("%d\n",r-1);
}
int main()
{while(~scanf("%d%d",&n,&m)){for(int i = 0; i < n; i ++)scanf("%d",&a[i]);sort(a, a+n);  //对牛栏排序solve();}return 0;
}

暑期集训2:ACM基础算法 例2:POJ-2456相关推荐

  1. 暑期集训2:ACM基础算法 例1:POJ-1064

    2018学校暑期集训第二天--ACM基础算法 例一  --  POJ - 1064 Cable master Inhabitants of the Wonderland have decided to ...

  2. 暑期集训2:ACM基础算法 练习题G:POJ - 1298

    2018学校暑期集训第二天--ACM基础算法 练习题G  --  POJ - 1298 The Hardest Problem Ever Julius Caesar lived in a time o ...

  3. 暑期集训2:ACM基础算法 练习题C:CF-1008A

    2018学校暑期集训第二天--ACM基础算法 练习题A  --   CodeForces - 1008A Romaji Vitya has just started learning Berlanes ...

  4. 暑期集训2:ACM基础算法 练习题B:CF-1008B

    2018学校暑期集训第二天--ACM基础算法 练习题B  --   CodeForces - 1008B Turn the Rectangles There are nn rectangles in ...

  5. 暑期集训2:ACM基础算法 练习题A:CF-1008C

    2018学校暑期集训第二天--ACM基础算法 练习题A  --  CodeForces - 1008C Reorder the Array You are given an array of inte ...

  6. ACM基础算法入门及题目列表

    对于刚进入大学的计算机类同学来说,算法与程序设计竞赛算是不错的选择,因为我们每天都在解决问题,锻炼着解决问题的能力. 这里以TZOJ题目为例,如果为其他平台题目我会标注出来,同时我的主页也欢迎大家去访 ...

  7. 暑期集训4:栈,树,优先队列 例 :  UVA - 514 ​​​​​​​​​​​​​​

    2018学校暑期集训第四天--栈,树,优先队列 例题  --   UVA - 514 Rails There is a famous railway station in PopPush City. ...

  8. NOIP训练营集训笔记—信息学基础算法(倍增与分治算法

      本文摘自清北OI学堂内部笔记,作者潘恺璠,来自柳铁一中曾参加过清北训练营提高组精英班,主要记录的是信息学基础算法.笔记非常详细,特分享给大家! NOIP2019年夏令营正在报名中,6大校区10种班 ...

  9. CDU集训代码:基础算法和数据结构2

    做题地址: http://acm.hdu.edu.cn/diy/contest_login.php?cid=16636 Problem A: HDU1040 排序题,可以直接使用c++提供的排序,如果 ...

最新文章

  1. NetLogo入门(一)——熟悉操作界面
  2. Android中的定时器AlarmManager
  3. iPhone必崩溃bug曝光!这个WiFi水太深谁也把握不住
  4. python三引号注释_python3学习笔记(三):注释和字符串
  5. [java进阶]3.slf4j作用及其实现原理
  6. Vue第一部分(2): 数据的渲染
  7. QT Creator 使用 design 修改 ui界面编译后界面未更新代码提示
  8. mbstring未安装
  9. 携程初赛 携程全球数据中心建设 球面上两点的最短距离 + 最小生成树
  10. JDK、SDK、JRE、JVM概念详解
  11. 厉害了!JMeter 模拟超过 5 万的并发用户!
  12. Guava学习笔记(零):Google Guava 类库简介
  13. 分享一个鼠标宏软件(XMouseButtonControl)用于替换logitech option等软件
  14. 编译原理实验四 C-语言 语法分析程序
  15. Python 第二章 字典
  16. 漏损分析与控制技术——漏损分析技术
  17. Kotlin 1.4 和未来值得期待的地方
  18. 青岛方言发音对照表(内附英文释义)
  19. 【软件质量】软件复杂性
  20. 放大镜展示特效,仿华为手机商品~

热门文章

  1. 算法导论Java实现-构建MaxHeap
  2. 清瘦的记录者: 一个比dbutils更小巧、好用的的持久化工具
  3. 第五章ThinkingInJava
  4. VBScript中InStr函数的用法
  5. 面试题6:用两个栈实现队列
  6. 10个最常用 Windows Vista运行命令
  7. JavaScript创建对象–如何在JS中定义对象
  8. css flexbox模型_5分钟内学习CSS Flexbox-初学者教程
  9. android level list,Android Drawable (levle List selector layer List)
  10. Embarcadero Dev C++ 中文输出乱码