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 题目大意:农夫 建造了一座很长的畜栏,它包括 个隔间,这些小隔间依次编号为. 但是, 的 头牛们并不喜欢这种布局,而且几头牛放在一个隔间里,他们就要发生争 ...
随机推荐
- 常用模块 time sys os json pickle
# import time # print(time.time()) #秒数 # print('开始下载') # time.sleep(2) # print('下载完成') # print(time. ...
- python爬虫——论抓包的正确姿势和学好Javascript的重要性(1)
没事想爬下数据,就入了scrapy坑,跟着https://zhuanlan.zhihu.com/data-factory这篇教程走,中间被小数量的网站坑过,不过还是写出了爬虫~~ 切糕王子:毫无防御, ...
- Gpload安装手册(Linux版本)
Gpload安装手册(Linux版本) 一.python 2.7版本 Linux系统默认是安装python2.7的,如果没有需要手动安装(python版本要求2.4.4以上): 通过命令:python ...
- Linux sar工具安装使用
使用sar Sar是后台进程sadc的前端显示工具,安装名为“sysstat”的包后,sadc就会自动从内核收集报告并保存. 安装sar [root@localhost ~]# yum insta ...
- maven maven-war-plugin 解决java war项目间的依赖(两个war都可独立部署运行,maven 3.2.x亲测)
最近整理基础框架,有些项目不想分布式,所以基础框架必须同时可独立部署,也可直接被作为依赖和业务工程打到一起,记录下解决war项目依赖的要点,一开始用warpath,结果报找不到,有些帖子还是17年的, ...
- python 排序算法
冒泡排序: 一. 冒泡排序的定义 冒泡排序(英语:Bubble Sort)是一种简单的排序算法.它重复地遍历要排序的数列,一次比较两个元素,如果他们的顺序错误就把他们交换过来.遍历数列的工作是重复地进 ...
- 算法(第四版)C# 习题题解——1.1
写在前面 整个项目都托管在了 Github 上:https://github.com/ikesnowy/Algorithms-4th-Edition-in-Csharp 善用 Ctrl + F 查找题 ...
- 【Alpha】Scrum Meeting 6
目录 前言 任务分配 燃尽图 会议照片 签入记录 困难 前言 第6次会议在4月10日22:00由PM在一公寓三楼召开. 交流确认了任务进度,对下一阶段任务进行分配.时长15min. 任务分配 姓名 当 ...
- 2018年12月25日 圣诞节快乐 生成器plus
import time#导入时间模块 time.sleep(5)#输入间隔时间5秒 def p_bz():#使用函数 生产包子 for i in range(100): yield "包子% ...
- Python3 tkinter基础 Menu 添加菜单栏
Python : 3.7.0 OS : Ubuntu 18.04.1 LTS IDE : PyCharm 2018.2.4 Conda ...