POJ:2456-Aggressive cows
Aggressive cows
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 18313 Accepted: 8716
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
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间小屋,n个小屋排在一列,每个小屋距离原点有一个距离,要将m头牛放在小屋中,要使两头牛之间的距离尽量的大,如果最大化牛直接的距离,那么最近两头牛之间的距离最大是多少。
- 看到这个最大化最小值那么首先想到的就是二分,最大之间求最小,最小之间求最大,平均之间求最大都是二分。每次二分最大距离,然后按照这个距离安排,看能否用n个小屋安排下,不能那么减少距离,如果可以那么增大距离。
#include <algorithm>
#include <cstring>
#include <stdio.h>
using namespace std;
const int maxn = 1e5+10;
int pos[maxn],n,c;
void init() {
for(int i=0;i<n;i++)
scanf("%d",&pos[i]);
sort(pos,pos+n);
}
bool checke(int len){
int cnt = 1,now = pos[0];
for(int i=1;i<n;i++) {
if (pos[i] - now >= len) {
cnt++;
now = pos[i];
}
}
return cnt >= c;
}
int binary_search() {
int l = 0,r = 1e9+10;
while(r-l > 1) {
int mid = (l+r)/2;
if(checke(mid))
l = mid;
else
r = mid;
}
return l;
}
int main() {
while(scanf("%d%d",&n,&c) != EOF) {
init();
int ans = binary_search();
printf("%d\n",ans);
}
return 0;
}
POJ:2456-Aggressive cows的更多相关文章
- POJ:2456 Aggressive cows(z最大化最小值)
描述 农夫 John 建造了一座很长的畜栏,它包括N (2 <= N <= 100,000)个隔间,这些小隔间依次编号为x1,...,xN (0 <= xi <= 1,000, ...
- poj 2456 Aggressive cows && nyoj 疯牛 最大化最小值 二分
poj 2456 Aggressive cows && nyoj 疯牛 最大化最小值 二分 题目链接: nyoj : http://acm.nyist.net/JudgeOnline/ ...
- 二分搜索 POJ 2456 Aggressive cows
题目传送门 /* 二分搜索:搜索安排最近牛的距离不小于d */ #include <cstdio> #include <algorithm> #include <cmat ...
- POJ 2456 Aggressive cows (二分 基础)
Aggressive cows Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 7924 Accepted: 3959 D ...
- [ACM] poj 2456 Aggressive cows (二分查找)
Aggressive cows Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 5436 Accepted: 2720 D ...
- POJ 2456 Aggressive cows
Aggressive cows Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 11192 Accepted: 5492 ...
- 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: 25944 Accepted: 11982 ...
- POJ 2456 Aggressive cows(贪心 + 二分)
原题链接:Aggressive cows 题目大意:农夫 建造了一座很长的畜栏,它包括 个隔间,这些小隔间依次编号为. 但是, 的 头牛们并不喜欢这种布局,而且几头牛放在一个隔间里,他们就要发生争 ...
- POJ 2456 Aggressive cows(二分答案)
Aggressive cows Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 22674 Accepted: 10636 Des ...
随机推荐
- 《Cron表达式详解》
Cron表达式是一个字符串,字符串以5或6个空格隔开,分为6或7个域,每一个域代表一个含义,Cron有如下两种语法格式: Seconds Minutes Hours DayofMonth Month ...
- view 状态动画
stateListAnimator 一.xml配置 方法 res/xml/animate_scale.xml <?xml version="1.0" encoding=&qu ...
- 使用 Notapad++ 进行 Java 开发
准备工具 1.安装 JDK 以及配置相关环境变量: 2.安装 64 位版的 Notepad++ : 2.一台 64 位 Windows 系统电脑: 一.下载&安装Notepad++ 官网下载地 ...
- Andoid Intent学习之在各个活动之间传递数据
Intent是一种运行时绑定(run-time binding)机制,它能在程序运行过程中连接两个不同的组件.通过Intent,你的程序可以向Android表达某种请求或者意愿,Android会根据意 ...
- 关于BaseServlet
BaseServlet 是项目中所有servlet的父类,作用是为了让一个servlet可以同时处理多个请求,因为我们之前比如说完成对于商品的增删改查的时候,每一个需求就要创建一个servlet,这样 ...
- 如何更换vim-airline的theme
仓库位置: 点我直达 (主题以前是和airline在同个仓库的,现在独立出来了) 这些内置的这些主题,可以直接使用,方法是在 “.vimrc”文件中写 let g:airline_theme=&quo ...
- CRUD全栈式编程架构之界面层的设计
Layout的设计 模板模式 mvc的模板特别类似设计模式中模板方法模式,结合Layout中RenderSection和RenderBody方法可以将部分html展现逻辑延迟到具体的视图页面去实现里面 ...
- 2017.11.20 基于JSP+Servlet+JavaBean实现复数运算(一)
(7)在Servlet中使用JavaBean Servlet和JavaBean都是类,在Servlet中使用JavaBean有两种方式: 1.在一个Servlet中单独使用JavaBean 一般完成的 ...
- Java连接mysql中遇到的一些问题及解决方法
1.Java使用mysql-jdbc连接MySQL出现如下警告: Establishing SSL connection without server's identityverification i ...
- Mybatis-generator自动生成
第一步:导入架包 <build> <plugins> <plugin> <groupId>org.mybatis.generator</group ...