2456 Aggressive cows
Aggressive cows
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 23866 | Accepted: 11141 |
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 <stdio.h>
#include <iostream>
#include <cstring>
#include <vector>
#include <algorithm>
const int si = 100010, inf = 1000000000 + 10;
using namespace std;
int N, COW;
int ar[si]; bool c(int dis) {
int pre = 0, finished = 1;
for (int i = 1; i < N && finished < COW; i++) {
//if (ar[N - 1] - ar[pre] < dis * (COW - finished)) return false;
//剪枝 剩下COW - finished头牛 每头牛需要dis的距离
if (ar[i] - ar[pre] >= dis) {
finished++;
pre = i;
}
}
return finished >= COW;
}
int main() {
cin >> N >> COW;
for (int i = 0; i < N; i++) scanf("%d", &ar[i]);;
sort(ar, ar + N);
int l = 1, m, r = inf;
while (l < r - 1) {
m = l + r >> 1;
if (c(m)) l = m;
else r = m;
}
cout << l << endl;
return 0;
}
2456 Aggressive cows的更多相关文章
- 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: 22674 Accepted: 10636 Des ...
- 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 题目大意:农夫 建造了一座很长的畜栏,它包括 个隔间,这些小隔间依次编号为. 但是, 的 头牛们并不喜欢这种布局,而且几头牛放在一个隔间里,他们就要发生争 ...
随机推荐
- 运行maven install命令时出现错误(BUILD FAILURE)
运行run as—>maven install时出现以下错误: [ERROR] Failed to execute goal org.apache.maven.plugins:maven-com ...
- Python Gevent协程自动切换IO
Gevent Gevent 是一个第三方库,可以轻松通过gevent实现并发同步或异步编程,在gevent中用到的主要模式是Greenlet, 它是以C扩展模块形式接入Python的轻量级协程. Gr ...
- Shell 终端ANSI控制码
Shell 系统交互参数整理 输出颜色 格式: \033[字背景颜色;字体颜色m字符串\033[0m 背景颜色 字体颜色 40: 黑 30: 黑 41: 红 31: 红 42: 绿 32: 绿 43: ...
- opencv学习之路(25)、轮廓查找与绘制(四)——正外接矩形
一.简介 二.外接矩形的查找绘制 #include "opencv2/opencv.hpp" using namespace cv; void main() { //外接矩形的查找 ...
- this指向问题,只提供案例,不做任何分析
希望大家在测试的道路上找到答案,阔步前行 <script type="text/javascript"> /*this指向 console.log(this); fun ...
- Canonical Coin Systems【完全背包】
问题 C: Canonical Coin Systems 时间限制: 1 Sec 内存限制: 128 MB 提交: 200 解决: 31 [提交] [状态] [命题人:admin] 题目描述 A ...
- Learning-Python【11】:函数嵌套及作用域
一.函数对象 函数是第一类对象:函数的内存地址(即函数名)可以像一个变量值一样去使用 1.变量值可以被引用,例如 x = 1,y = x def foo(): print('from foo') f ...
- 使用SpotBugs/FindBugs进行代码检查
原po:https://blog.csdn.net/zhangb00/article/details/8407070 SpotBugs 介绍 SpotBugs是Findbugs的继任者(Findbug ...
- Perl调用外部命令(其他脚本、系统命令)的方法和区别
1. `command`; 使用反引号调用外部命令能够捕获其标准输出,并按行返回且每行结束处附带一个回车.反引号中的变量在编译时会被内插为其值. 2. open LIST "ls -l| ...
- Jade入门学习笔记
jade是超高性能的node JavaScript模板引擎,有着非常强大的API和大量杰出的特性.它主要针对node的服务端.由于商标的原因,改为Pug,哈巴狗.Pug有它本身的缺点--可移植性差,调 ...