如果C(d)为满足全部牛之间的距离都不小于d。

先对牛舍的位置排序,然后二分枚举d,寻找满足条件的d。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<queue>
#include<set>
#include<map>
#include<vector>
#include<cmath>
#define ll __int64
#define INF 0x3fffffff
using namespace std; int n,m;
int x[100005]; bool C(int d)
{
int num=1;
int a=x[0];
int i=1;
while(num<m)
{
if(a+d<=x[i])
{
num++;
a=x[i];
i++;
}
else i++;
if(i==n) break;
}
if(num==m) return true;
else return false;
} void solve()
{
int l=0,r=x[n-1]+1;
while(r-l>1)
{
int mid=(r+l)/2;
if(C(mid)) l=mid;
else r=mid;
}
cout<<l<<endl;
} int main()
{
//freopen("d:\\Test.txt","r",stdin);
cin>>n>>m;
for(int i=0;i<n;i++)
{
scanf("%d",&x[i]);
}
sort(x,x+n);
solve();
return 0;
}

POJ2456 Aggressive cows(二分+贪心)的更多相关文章

  1. POJ2456 Aggressive cows 二分

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

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

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

  3. 二分法的应用:最大化最小值 POJ2456 Aggressive cows

    /* 二分法的应用:最大化最小值 POJ2456 Aggressive cows Time Limit: 1000MS Memory Limit: 65536K Total Submissions: ...

  4. 二分算法的应用——最大化最小值 POJ2456 Aggressive cows

    Aggressive cows Time Limit: 1000MS Memory Limit: 65536K Total Submissions: Accepted: Description Far ...

  5. POJ 2456 Aggressive cows(贪心 + 二分)

    原题链接:Aggressive cows 题目大意:农夫 建造了一座很长的畜栏,它包括  个隔间,这些小隔间依次编号为. 但是, 的  头牛们并不喜欢这种布局,而且几头牛放在一个隔间里,他们就要发生争 ...

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

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

  7. POJ2456 Aggressive cows

    Aggressive cows 二分,关键是转化为二分! #include <cstdio> #include <algorithm> ; ; int N, C; int a[ ...

  8. POJ2456 Aggressive cows 2017-05-11 17:54 38人阅读 评论(0) 收藏

    Aggressive cows Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 13993   Accepted: 6775 ...

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

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

随机推荐

  1. 关于RDS备份文件使用wget下载提示403 Forbidden的情况

    关于RDS备份文件使用wget下载提示403 Forbidden的情况 使用wget下载提示403错误当我们RDS物理备份文件时,例如: 原因: URL中包含有特殊字符比如&,从而造成URL被 ...

  2. 使用vs调试.net源代码

    使用.NET Framework库参考源进行调试 您可能会想知道使用.NET Framework参考源的调试方式.在下面的示例中,您将看到一个我调用公用Console.WriteLine方法的工具.从 ...

  3. php 检查该数组有重复值

    if (count($array) != count(array_unique($array))) { echo '该数组有重复值'; }

  4. jquery获取tr并更改tr内容

    jquery获取tr并更改tr内容示例代码. 例子: $(document).ready(function() { $("#Email tr").each(function(){ ...

  5. mysql远程登录出错的解决方法

    mysql远程登录出错的情况,先比很多朋友都有遇到过吧,下面有个不错的解决方法,大家可以参考下. 错误:ERROR 2003 (HY000): Can't connect to MySQL serve ...

  6. Linux 用C语言判断文件和文件夹

    Linux 用C语言判断文件和文件夹 #include <stdio.h> #include <stdlib.h> #include <unistd.h> #inc ...

  7. Atitit. 构造ast 语法树的总结attilax oao

    Atitit. 构造ast 语法树的总结attilax oao 1. 能那更加有意义的名字来命名ast节点... 1 2. 如何命名表达式名称..使用实际对象名称,而不是操作符号表达式更好 1 2.1 ...

  8. MySQL Fabric部署

    架构描写叙述: 一台主机上安装4个MySQL 服务,当中一个MySQL服务用于存储MySQL Fabric后台数据:另外3个MySQL服务用于主从架构測试.一个主+两个从. 第一部分:二进制方式安装M ...

  9. 102. Linked List Cycle【medium】

    Given a linked list, determine if it has a cycle in it.   Example Given -21->10->4->5, tail ...

  10. Hystrix的用法

    package com.example.demo; import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand; imp ...