poj 2456 Aggressive cows 二分 题解《挑战程序设计竞赛》
地址 http://poj.org/problem?id=2456

解法
使用二分逐个尝试间隔距离 能否满足要求
检验是否满足要求的函数 使用的思想是贪心 第一个点放一头牛 后面大于等于尝试的距离才放置一头牛 如果能放置完所有的牛 那么就继续增加尝试的距离 否则就减少尝试的距离
代码
#include <iostream>
#include <vector>
#include <math.h>
#include <algorithm> using namespace std; /*
Sample Input
5 3
1
2
8
4
9
Sample Output
3
*/ vector<int> v; int n, m; bool check(int d)
{
int mm=m-; int lastselect = v[]; for (int i = ; i < n; i++) {
if (v[i] - lastselect >= d) {
mm--;
lastselect = v[i];
if (mm == )
return false;
}
} return true;
} int main()
{
cin >> n >> m; for (int i = ; i < n; i++) {
int t; cin >> t;
v.push_back(t);
}
if (m == ) {
printf("0\n");
return ;
} sort(v.begin(), v.end()); //距离的极端取值k
int l = ; int r = v.back(); while (l<r) {
int mid = l + r >> ;
if (check(mid)) r = mid;
else {
l = mid + ;
}
} cout << r- << endl; return ;
}
poj 2456 Aggressive cows 二分 题解《挑战程序设计竞赛》的更多相关文章
- POJ 2456 Aggressive cows (二分 基础)
Aggressive cows Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 7924 Accepted: 3959 D ...
- 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 二分
Description Farmer John has built a new long barn, with N (2 <= N <= 100,000) stalls. The stal ...
- [POJ] 2456 Aggressive cows (二分查找)
题目地址:http://poj.org/problem?id=2456 最大化最小值问题.二分牛之间的间距,然后验证. #include<cstdio> #include<iostr ...
- 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 ...
- POJ 2456 Agressive cows(二分)
POJ 2456 Agressive cows 农夫 John 建造了一座很长的畜栏,它包括N (2≤N≤100,000)个隔间,这 些小隔间的位置为x0,...,xN-1 (0≤xi≤1,000,0 ...
随机推荐
- 在IIS上部署 .Net Core 3.0 项目踩坑实录
在IIS上部署 .Net Core 3.0 项目的主要流程有: 安装并启用IIS 安装AspNetCoreModuleV2 添加.配置网站 设置应用程序池 通过VS发布 一.安装并启用IIS: 安装了 ...
- Java内存区域与内存溢出异常,对象的创建
一.运行时数据区域 Java程序的执行流程:首先 .java源代码文件会被Java编译器编译为字节码文件(.class后缀),然后由JVM中的类加载器加载各个类的字节码文件,加载完毕之后,交由JVM执 ...
- 文本切换器(TextSwitcher)的功能与用法
TextSwitcher继承了ViewSwitcher,因此它具有与ViewSwitcher相同的特征:可以在切换View组件时使用动画效果.与ImageSwitcher相似的是,使用TextSwit ...
- Visual Studio安装工具和安装组件下载速度慢的问题
下载安装Visual Studio时在下面这个界面下载时速度只有几十KB每秒 解决办法:修改Host文件,在Host文件中添加下面代码,然后保存即可 110.53.72.104 download.vi ...
- asp.net实现SQL2005的通知数据缓存
首先第一步是确保您的 Service Broker 已经激活,激活 Service Broker (Transact-SQL)如下: USE master ; GO ALTER DATABASE Yo ...
- 22(8).模型融合---RegionBoost
在adaboost当中,样本的权重alpha是固定的,蓝色五角星所在的圈中3个○分错了,红色五角星所在的圈中4个×和1个○都分对了,很容易让人想到,这个模型,对于红色位置的判断更加可信. 动态权重,每 ...
- Yii2 中常用的增删改查操作总结
一.新增 1.使用save() $model = new User(); $model->name = 'test'; $model->phone = '13000000000'; $mo ...
- laravel5+ElasticSearch+go-mysql-elasticsearch MySQL数据实时导入(mac)
1. ElasticSearch安装 直接使用brew install elasticsearch 安装最新版本的es,基本没有障碍. 2.Laravel5 框架添加elasticsearch支持 在 ...
- Docker-Nginx,发布前端服务
1.安装环境: yum install -y yum-utils \ device-mapper-persistent-data \ lvm2 yum-config-manager \ --add-r ...
- Java日期时间API系列6-----Jdk8中java.time包中的新的日期时间API类
因为Jdk7及以前的日期时间类的不方便使用问题和线程安全问题等问题,2005年,Stephen Colebourne创建了Joda-Time库,作为替代的日期和时间API.Stephen向JCP提交了 ...