暑期集训2:ACM基础算法 例2:POJ-2456

news/2024/7/7 19:35:26

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;
}

 


http://lihuaxi.xjx100.cn/news/247407.html

相关文章

Linux启动流程(二)

//...根据grub内核映像所在路径,读取内核映像&#xff0c;并进行解压缩操作。并调用start_kernel()函数来启动一系列的初始化函数并初始化各种设备&#xff0c;完成Linux核心环境的建立1.start_kernel(init/main.c)中调用一系列初始化函数:(1) 在屏幕上打印出当前的内核版本信息…

TL-WDN3321 Ubuntu 下安装

为什么80%的码农都做不了架构师&#xff1f;>>> The WiFi USB dongles based on the newest RT5572 chip set do not work out of the box on Ubuntu. Unex DNUR-V72, D-Link DWA-160 Rev B and TP-Link TL-WDN3200 dongles are based on this chipset. You will …

C++数值极限numeric_limits

一般来说&#xff0c;数值类型的极值是一个与平台相关的特性。C标准程序库通过template numeric_limits提供这些极值&#xff0c;取代传统C语言所采用的预处理常数。你仍然可以使用后者&#xff0c;其中整数常数定义于<climits>和<limits.h>,浮点常数定义于<cfl…

[异常笔记] spring boot 启动-2018040201

异常 1、编码引发异常 00:59:49.311 [main] DEBUG org.springframework.boot.devtools.settings.DevToolsSettings - Included patterns for restart : [] 00:59:49.318 [main] DEBUG org.springframework.boot.devtools.settings.DevToolsSettings - Excluded patterns for re…

2018牛客暑期ACM多校训练营第二场 - A题

A题是道水题……结果没注意到编译器不支持I64d卡了快俩小时…… 还要注意的是k1时不是特例&#xff0c;因为这时候走跟跑还是不一样的…… A run 输入描述&#xff1a; The first line of input contains 2 integers Q and k.Q is the number of queries.(Q<100000,1<k…

最近做手机端,GPS,微信QQ分享总结的问题

Android端 百度地图&#xff1a; 1.libs包中armeabi下liblocSDK4d.so文件丢失&#xff0c;导致百度定位失效。 微信分享&#xff1a; 1.分享App,app的内容&#xff08;图片加描述&#xff09;不能超过32kb &#xff0c;不然无法分享。&#xff08;直接跳至Oncancel事件中&#…

基于Sql Server 2008的分布式数据库的实践(一)

原文 基于Sql Server 2008的分布式数据库的实践&#xff08;一&#xff09; 配置Sql Server 2008&#xff08;Win7&#xff09; 1.打开SQL server2012&#xff0c;使用windows身份登录 2.登录后&#xff0c;右键选择“属性”。左侧选择“安全性”&#xff0c;选中右侧的“SQL S…

利用gevent实现异步执行任务

import gevent def task(pid):gevent.sleep(2)print ("task %s done"%pid)def asynchronous():threads [gevent.spawn(task,i) for i in xrange(5)]gevent.joinall(threads)if __name__ __main__:print ("asynchronous")asynchronous()执行结果&#xff…