POJ 2456 Agressive cows(二分)
农夫 John 建造了一座很长的畜栏,它包括N (2≤N≤100,000)个隔间,这 些小隔间的位置为x0,...,xN-1 (0≤xi≤1,000,000,000,均为整数,各不相同).
John的C (2≤C≤N)头牛每头分到一个隔间。牛都希望互相离得远点省得 互相打扰。怎样才能使任意两头牛之间的最小距离尽可能的大,这个最 大的最小距离是多少呢
思想:二分,首先把输入的数据进行从小到大排序,再由最短距离为1,最长距离为(a[n-1] - a[0] + 1 - c) / (c - 1) + 1进行二分测试即可
代码:
#include<iostream>
#include<algorithm>
using namespace std;
#define N 100000+5
int a[N];
int main() {
int n, c;
scanf("%d%d", &n, &c);
for(int i = ; i < n; i++) scanf("%d", &a[i]);
sort(a, a+n);
int L = , R = (a[n-] - a[] + - c)/(c-) + ;
int mid, ans;
while(L <= R) {
mid = L + (R - L)/;
int i = , count = , pre = ;
while(i < n && count < c) {
while(i < n && a[i] - a[pre] < mid) i++;
if(i < n) count++;
pre = i;
i++;
}
if(count < c) {
R = mid - ;
} else {
ans = mid;
L = mid + ;
}
}
printf("%d\n", ans);
return ;
}
POJ 2456 Agressive cows(二分)的更多相关文章
- poj 2456 Aggressive cows 二分 题解《挑战程序设计竞赛》
地址 http://poj.org/problem?id=2456 解法 使用二分逐个尝试间隔距离 能否满足要求 检验是否满足要求的函数 使用的思想是贪心 第一个点放一头牛 后面大于等于尝试的距离才放 ...
- [poj 2456] Aggressive cows 二分
Description Farmer John has built a new long barn, with N (2 <= N <= 100,000) stalls. The stal ...
- 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 二分 最大化最小值
Aggressive cows Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 18099 Accepted: 8619 ...
- POJ 2456 Aggressive cows ( 二分 && 贪心 )
题意 : 农夫 John 建造了一座很长的畜栏,它包括N (2 <= N <= 100,000)个隔间,这些小隔间依次编号为x1,...,xN (0 <= xi <= 1e9) ...
- 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 ...
随机推荐
- Python 模块(module)
模块(module)也是为了同样的目的.在Python中,一个.py文件就构成一个模块.通过模块,你可以调用其它文件中的程序. first.py def laugh(): print "Ha ...
- MVC数据列表展示【三】
一.控制器向视图传递参数的两种形式:使用到的技术有EF,linq表达式,StringBuilder,相关技术都可以在我的博客园中找到详细的技术介绍. 1. 第一种是通过字符通过foreach循环拼接将 ...
- [原][osg][QT]osg与QT界面结合的简单例子二
//main.cpp #include "VREObliqueEditorQTWindow.h" #include <QtWidgets/QApplication> # ...
- 《剑指offer》第四十三题(从1到n整数中1出现的次数)
// 面试题43:从1到n整数中1出现的次数 // 题目:输入一个整数n,求从1到n这n个整数的十进制表示中1出现的次数.例如 // 输入12,从1到12这些整数中包含1 的数字有1,10,11和12 ...
- Django - models.py 应用
Django - models.py 应用 编写 models.py 文件 from django.db import models # Create your models here. class ...
- Python redis 简单介绍
Python redis 简单介绍 1.安装 终端输入: pip(or)pip3.6 install redis 安装成功 2.哈哈,发现我并没有redis服务可以访问,所以到这里,在本机安装了red ...
- composer修改中文镜像
composer config -g repo.packagist composer https://packagist.phpcomposer.com
- FreeRTOS
一.内核配置 1.configUSE_PREEMPTION 设置为1,使用抢先式内核:设置为0,为合作轮转内核. 2.configCPU_CLOCK_HZ 内部处理器执行的频率.这个值需要正确配置外围 ...
- LeetCode--005--最长回文子串(java)
给定一个字符串 s,找到 s 中最长的回文子串.你可以假设 s 的最大长度为 1000. 示例 1: 输入: "babad" 输出: "bab" 注意: &qu ...
- 详解 Solidity 事件Event - 完全搞懂事件的使用
很多同学对Solidity 中的Event有疑问,这篇文章就来详细的看看Solidity 中Event到底有什么用? 写在前面 Solidity 是以太坊智能合约编程语言,阅读本文前,你应该对以太坊. ...