如果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. java的重写、重载、覆盖的差别

    多态性  通过继承,一个类能够用作多种类型:能够用作它自己的类型.不论什么基类型,或者在实现接口时用作不论什么接口类型.这称为多态性  重载  每一个类型成员都有一个唯一的签名.方法签名由方法名称和一 ...

  2. 【转帖】Sigma水平和缺陷率的对应关系:正态分布中心和1.5标准差偏移

    http://www.pinzhi.org/thread-5395-1-1.html Sigma水平和缺陷率的对应关系:正态分布中心和有1.5个标准差偏移 在过程稳定时,若给出了规范限,过程的平均与标 ...

  3. centos 配置 samba 与windows共享文件夹

    yum install samba /etc/samba/smb.conf directory mask = 0777 ← 指定新建目录的属性(以下4行) force directory mode = ...

  4. Php开发工具:PhpStorm=webstorm+php+db/SQL

    下载地址:https://www.jetbrains.com/zh/phpstorm/specials/phpstorm/phpstorm.html?utm_source=baidu&utm_ ...

  5. python --批量重命名文件名

    # -*- coding: utf-8 -*- import os path = "d:\\curl\data\\" for file in os.listdir(path): p ...

  6. C#:列表视图操作类

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.W ...

  7. some issues that you should be take care of when use the plupload module

    1. the config arguments 'browse_button' should not be a single element like button etc. because in i ...

  8. vue $http请求服务

    vue中的$http服务  需要引入一个叫vue-resource.js的文件,因为vue.js中没有$http服务.如果需要使用这个服务去百度下载vue-resource.js 然后引进项目即可. ...

  9. JVM性能调优监控工具jps、jstack、jmap、jhat、jstat、hprof

    JVM性能调优监控工具jps.jstack.jmap.jhat.jstat.hprof

  10. ui-router详解(二)ngRoute工具区别

    我们了解 angular.js 是一种富客户端单页面应用,所以要在一个页面呈现不同的视图,路由起到了至关重要的作用. angular.js 为我们封装好了一个路由工具 ngRoute ,它是一种靠ur ...