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 ...
随机推荐
- centos7下安装客户端rabbitmq9.0
下载目前最新rabbitmq客户端版本: wget https://github.com/alanxz/rabbitmq-c/archive/v0.9.0.tar.gz php扩展 : wget ht ...
- ubutu tornado python3.7.5 nginx supervisor 部署web api
环境: 1.Ubuntu 服务器 2.python3.7.5 安装 1.python3.7.5 安装的话还是比较简单,流程大致是./configure ->make && mak ...
- SQL server 安装成功到使用Sa SQL server验证登录等一系列问题
使用 Windows 身份验证方式登录 出现错误 无法连接到 本地服务器 解决问题: SQL server配置管理器:服务远程过程调用失败 https://blog.csdn.net/gfjjggg/ ...
- python 基础学习笔记(7)--迭送器
**函数名的运用** - [ ] 函数名是一个变量, 但它是一个特殊的变量, 与括号配合可以执行函数的变量 **函数名的内存地址** ``` def func(): print('666') p ...
- supervisor 工具使用
最近项目要使用supervisor 来管理程序,简单查了查,发现比较容易使用: 中文博客查了查,发现很多人都写出了教程,我这边就懒得写了,找了几个能看懂的记录如下: https://www.cnblo ...
- 关于Git和GitHub的一些知识
git是分布式的版本控制工具,可离线,svn是集中式的,要联网操作.集中式的所有数据都放在服务器端,如果服务器宕机,则历史记录也可能就丢失了,这叫做单点故障.分布式的数据可直接保存在客户端. 为何要版 ...
- MyBatis核心对象之StatementHandler
MyBatis核心对象之StatementHandler StatementHandler ResultHandler ParameterHandler Executor org.apache.iba ...
- volatile可见性案例-黑马
volatile可见性案例-黑马 package com.mozq.demo.demo; class Task implements Runnable{ //public boolean flag = ...
- CTPN网络理解
本文主要对常用的文本检测模型算法进行总结及分析,有的模型笔者切实run过,有的是通过论文及相关代码的分析,如有错误,请不吝指正. 一下进行各个模型的详细解析 CTPN 详解 代码链接:https:// ...
- Gluserfs 架构详解【译】官网
Gluserfs详解 排版看着不舒服的,可以查看[我的简书](https://www.jianshu.com/p/0340e429431b) doc home:https://docs.gluster ...