ACM题目————Aggressive cows
Description
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
* Lines 2..N+1: Line i+1 contains an integer stall location, xi
Output
Sample Input
5 3
1
2
8
4
9
Sample Output
3
Hint
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.
#include <iostream>
#include <algorithm> using namespace std;
const long long INF = 1000000002;
int a[100002], C, n; bool OK(int m)
{
int last = 0;
for(int i=1; i<C; i++)
{
int cur = last + 1 ;
while(cur<n && a[cur]-a[last]<m) cur ++ ;
if(cur==n) return false ;
last = cur ;
}
return true;
} int main()
{
cin >> n >> C ;
for(int i=0; i<n; i++) cin >> a[i] ;
sort(a,a+n);//排序
int low = 0, high = a[n-1] + 1 ;
while(high - low > 1)
{
int mid = (low + high) >> 1 ;
if(OK(mid)) low = mid ;
else high = mid ;
}
cout << low << endl ; return 0;
}
ACM题目————Aggressive cows的更多相关文章
- [ACM] poj 2456 Aggressive cows (二分查找)
Aggressive cows Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 5436 Accepted: 2720 D ...
- poj 2456 Aggressive cows && nyoj 疯牛 最大化最小值 二分
poj 2456 Aggressive cows && nyoj 疯牛 最大化最小值 二分 题目链接: nyoj : http://acm.nyist.net/JudgeOnline/ ...
- POJ2456 Aggressive cows 2017-05-11 17:54 38人阅读 评论(0) 收藏
Aggressive cows Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 13993 Accepted: 6775 ...
- 二分算法的应用——最大化最小值 POJ2456 Aggressive cows
Aggressive cows Time Limit: 1000MS Memory Limit: 65536K Total Submissions: Accepted: Description Far ...
- POJ - 2456 Aggressive cows 二分 最大化最小值
Aggressive cows Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 18099 Accepted: 8619 ...
- POJ 2456 Aggressive cows (二分 基础)
Aggressive cows Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 7924 Accepted: 3959 D ...
- 二分搜索 POJ 2456 Aggressive cows
题目传送门 /* 二分搜索:搜索安排最近牛的距离不小于d */ #include <cstdio> #include <algorithm> #include <cmat ...
- 【POJ - 2456】Aggressive cows(二分)
Aggressive cows 直接上中文了 Descriptions 农夫 John 建造了一座很长的畜栏,它包括N (2 <= N <= 100,000)个隔间,这些小隔间依次编号为x ...
- 二分-F - Aggressive cows
F - Aggressive cows Farmer John has built a new long barn, with N (2 <= N <= 100,000) stalls. ...
随机推荐
- python之django 资料
里边有不少比较好的文章. http://www.cnblogs.com/luxiaojun/p/5795070.html
- Lintcode: Maximum Subarray Difference
Given an array with integers. Find two non-overlapping subarrays A and B, which |SUM(A) - SUM(B)| is ...
- CSS自定义弹出框
<script type="text/javascript" language="javascript"> function sAlert(str) ...
- JavaScript: basis
ref: http://www.imooc.com/code/387 1. html里直接嵌入js: <!DOCTYPE HTML> <html> <head> & ...
- 当android studio一直显示gradle compile dependency
出现这种情况,是被墙的问题,我的解决办法是这样的: 打开file---->setting,然后搜索gradle,把offline勾上,然后点击apply以及ok,就可以了. 有时候它会关闭,只需 ...
- Java编程思想(一):大杂烩
在java中一切都被视为对象.尽管一切都是对象,但是操纵的标识符实际上是对象的一个引用,可以将引用想象成是遥控器(引用)来操纵电视机(对象).没有电视机,遥控器也可以单独存在,即引用可以独立存在,并不 ...
- SparkSQL JSON数据操作(1.3->1.4)
1.用户自定义schema data json串格式如下: { "partner_code": "demo", "app_name": &q ...
- 关于.Net Remoting 和 Web Servcie的比较
参照文献 http://www.cnblogs.com/shinehouse/articles/3001955.html http://www.cnblogs.com/paper/archive/20 ...
- Microsoft.Jet.OLEDB.4.0和Microsoft.ACE.OLEDB.12.0的区别
Microsoft.Jet.OLEDB.4.0和Microsoft.ACE.OLEDB.12.0的区别 时间 2012-12-19 20:30:12 CSDN博客原文 http://blog.cs ...
- 夺命雷公狗—angularjs—6—单条数据的遍历
我们在实际的工作中常常会处理到一些数据的遍历,这些数据都是后端传到前端的,有些公司会让前端帮忙处理一点遍历的工作,废话不多说,直接上代: <!doctype html> <html ...