POJ_2456 Aggressive cows 【二分求最大化最小值】
题目:
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>
#include <cstdio>
using namespace std;
const int MAXN = 1e5+5;
int N, C;
int Pt[MAXN]; bool fun(int x)
{
int pre = 0, next = 1, cnt = 1;
while(next < N)
{
if(Pt[next] - Pt[pre] < x)
{
next++;
}
else
{
pre = next;
next++;
cnt++;
}
}
return cnt >= C;
} void solve()
{
sort(Pt, Pt+N);
int left = Pt[0], right = Pt[N-1], mid;
while(right - left > 1)
{
mid = (left+right)>>1;
if(fun(mid)) left = mid;
else right = mid;
}
printf("%d\n", left);
} int main()
{
while(~scanf("%d%d", &N, &C))
{
for(int i = 0; i < N; i++)
{
scanf("%d", &Pt[i]);
}
solve();
}
return 0;
}
POJ_2456 Aggressive cows 【二分求最大化最小值】的更多相关文章
- poj 2456 Aggressive cows && nyoj 疯牛 最大化最小值 二分
poj 2456 Aggressive cows && nyoj 疯牛 最大化最小值 二分 题目链接: nyoj : http://acm.nyist.net/JudgeOnline/ ...
- codeforce 1070 E Getting Deals Done(二分求最大化最小值)
Polycarp has a lot of work to do. Recently he has learned a new time management rule: "if a tas ...
- poj 2456 Aggressive cows(二分搜索之最大化最小值)
Description Farmer John has built a <= N <= ,) stalls. The stalls are located along a straight ...
- POJ 3258(二分求最大化最小值)
题目链接:http://poj.org/problem?id=3258 题目大意是求删除哪M块石头之后似的石头之间的最短距离最大. 这道题目感觉大致代码写起来不算困难,难点在于边界处理上.我思考边界思 ...
- 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 (二分查找)
题目地址:http://poj.org/problem?id=2456 最大化最小值问题.二分牛之间的间距,然后验证. #include<cstdio> #include<iostr ...
- POJ 2456 Aggressive cows(二分答案)
Aggressive cows Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 22674 Accepted: 10636 Des ...
- [poj 2456] Aggressive cows 二分
Description Farmer John has built a new long barn, with N (2 <= N <= 100,000) stalls. The stal ...
随机推荐
- 189. Rotate Array 从右边开始翻转数组
[抄题]: Rotate an array of n elements to the right by k steps. For example, with n = 7 and k = 3, the ...
- Linux 的文件系统
Linux 文件属性 文件属性示意图 第一栏代表这个文件的类型与权限(permission): FHS Filesystem Hierarchy Standard(文件系统层次化标准) 1. / (r ...
- 【Java】java中的compareTo和compare的区别
compare 从这里可以看出,compare是Comparator接口中的一个类,再看一下源代码中的解释 Compares its two arguments for order. Returns ...
- APUE(3)---文件I/O (1)
一.引言 UNIX系统中的大多数文件对I/O只需用到5个函数:open/read/write/lseek和close,这些函数都是不带缓冲I/O(Unbuffered I/O).只要涉及到多个进程间共 ...
- POJ3041 Asteroids(二分图最小点覆盖)
Description Bessie wants to navigate her spaceship through a dangerous asteroid field in the shape o ...
- JAVA自动装箱拆箱与常量池
java 自动装箱与拆箱 这个是jdk1.5以后才引入的新的内容,作为秉承发表是最好的记忆,毅然决定还是用一篇博客来代替我的记忆: java语言规范中说道:在许多情况下包装与解包装是由编译器自行完成的 ...
- android开关控件Switch和ToggleButton
序:今天项目中用到了开关按钮控件,查阅了一些资料特地写了这篇博客记录下. 1.Switch <Switch android:id="@+id/bt" android:layo ...
- Newtonsoft.Json.Linq
var json = "{\"name\":\"ok1\",\"sex\":\"man\"}"; / ...
- 适配器(Adapter)模式
一. 适配器(Adapter)模式 适配器模式把一个类的接口变换成客户端所期待的另一种接口,从而使原本接口不匹配而无法在一起工作的两个类能够在一起工作 二. 类的Adapter模式的结构: 目标(Ta ...
- 使用CodeMaid自动程序排版[转]
前言 「使用StyleCop验证命名规则」这篇文章,指引开发人员透过StyleCop这个工具,来自动检验项目中产出的程序代码是否合乎命名规则. [Tool] 使用StyleCop验证命名规则 但是在项 ...