地址 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 二分 题解《挑战程序设计竞赛》的更多相关文章

  1. POJ 2456 Aggressive cows (二分 基础)

    Aggressive cows Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7924   Accepted: 3959 D ...

  2. POJ 2456 Aggressive cows(二分答案)

    Aggressive cows Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 22674 Accepted: 10636 Des ...

  3. POJ - 2456 Aggressive cows 二分 最大化最小值

    Aggressive cows Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 18099   Accepted: 8619 ...

  4. [poj 2456] Aggressive cows 二分

    Description Farmer John has built a new long barn, with N (2 <= N <= 100,000) stalls. The stal ...

  5. [POJ] 2456 Aggressive cows (二分查找)

    题目地址:http://poj.org/problem?id=2456 最大化最小值问题.二分牛之间的间距,然后验证. #include<cstdio> #include<iostr ...

  6. POJ 2456 Aggressive cows ( 二分 && 贪心 )

    题意 : 农夫 John 建造了一座很长的畜栏,它包括N (2 <= N <= 100,000)个隔间,这些小隔间依次编号为x1,...,xN (0 <= xi <= 1e9) ...

  7. poj 2456 Aggressive cows && nyoj 疯牛 最大化最小值 二分

    poj 2456 Aggressive cows && nyoj 疯牛 最大化最小值 二分 题目链接: nyoj : http://acm.nyist.net/JudgeOnline/ ...

  8. 二分搜索 POJ 2456 Aggressive cows

    题目传送门 /* 二分搜索:搜索安排最近牛的距离不小于d */ #include <cstdio> #include <algorithm> #include <cmat ...

  9. POJ 2456 Agressive cows(二分)

    POJ 2456 Agressive cows 农夫 John 建造了一座很长的畜栏,它包括N (2≤N≤100,000)个隔间,这 些小隔间的位置为x0,...,xN-1 (0≤xi≤1,000,0 ...

随机推荐

  1. Linux - CentOS 7 安装 .Net Core 运行环境

    阿里云的CentOS 7.7 64位,所需要的环境:MySql 5.7,.Net Core 2.2 ,Nginx 我这里用的 Xshell 工具,首先用root进入系统 版本信息 打开终端输入命令: ...

  2. KnockoutJS-自定义属性绑定

    在knockoutjs中,已有的绑定功能已经十分强大,基本上可以不需要再去考虑扩展了,但是,也有例外的场景,面对这种场景,还是得去完成,knockoutJS提供了自定义绑定来扩展绑定功能. 一.新建绑 ...

  3. day 27-1 反射、内置方法

    反射 反射:通过字符串来映射到对象的属性 class People(): def __init__(self, name, age): self.name = name self.age = age ...

  4. Spring基础——AOP通知

    spring(AOP通知) 切面 切面是封装通用业务逻辑的组件,可以作用到其他组件上.是spring组件中的某个方法.无返回类型.参数类型与通知类型有关.一个切面 开启数据库 关闭数据库 开启事务 检 ...

  5. selectors模块的设计亮点

    事件类型标志的选择 在selectors模块中的开头直接定义了事件类型的标志数字,选用的是(1 << 0)就是1代替EVENT_READ读操作:使用(1 << 1)就是2代替E ...

  6. python的@propert装饰器

    首先,@propert的作用是把类中的方法『变成』了属性,方便通过实例访问.propert可以有两种用法:可以把一个方法变成只读属性:可以对一些属性进行过滤. 想象这样一个场景,在实例化一个类之后,需 ...

  7. [专题总结]初探插头dp

    彻彻底底写到自闭的一个专题. 就是大型分类讨论,压行+宏定义很有优势. 常用滚动数组+哈希表+位运算.当然还有轮廓线. Formula 1: 经过所有格子的哈密顿回路数. 每个非障碍点必须有且仅有2个 ...

  8. [译]Vulkan教程(04)基础代码

    [译]Vulkan教程(04)基础代码 General structure 通用结构 In the previous chapter you've created a Vulkan project w ...

  9. java之可变个数的形参

    //采用数组形参来定义方法 public static void test (int a, String[] books); //采用可变个数形参来定义方法 public static void te ...

  10. PHP目前常见的五大运行模式

    版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明.本文链接:https://blog.csdn.net/xujingzhong0077/artic ...