POJ2456 Aggressive cows(二分+贪心)
如果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(二分+贪心)的更多相关文章
- POJ2456 Aggressive cows 二分
Description Farmer John has built a new long barn, with N (2 <= N <= 100,000) stalls. The stal ...
- POJ 2456 Aggressive cows ( 二分 && 贪心 )
题意 : 农夫 John 建造了一座很长的畜栏,它包括N (2 <= N <= 100,000)个隔间,这些小隔间依次编号为x1,...,xN (0 <= xi <= 1e9) ...
- 二分法的应用:最大化最小值 POJ2456 Aggressive cows
/* 二分法的应用:最大化最小值 POJ2456 Aggressive cows Time Limit: 1000MS Memory Limit: 65536K Total Submissions: ...
- 二分算法的应用——最大化最小值 POJ2456 Aggressive cows
Aggressive cows Time Limit: 1000MS Memory Limit: 65536K Total Submissions: Accepted: Description Far ...
- POJ 2456 Aggressive cows(贪心 + 二分)
原题链接:Aggressive cows 题目大意:农夫 建造了一座很长的畜栏,它包括 个隔间,这些小隔间依次编号为. 但是, 的 头牛们并不喜欢这种布局,而且几头牛放在一个隔间里,他们就要发生争 ...
- POJ 2456 Aggressive cows(二分答案)
Aggressive cows Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 22674 Accepted: 10636 Des ...
- POJ2456 Aggressive cows
Aggressive cows 二分,关键是转化为二分! #include <cstdio> #include <algorithm> ; ; int N, C; int a[ ...
- POJ2456 Aggressive cows 2017-05-11 17:54 38人阅读 评论(0) 收藏
Aggressive cows Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 13993 Accepted: 6775 ...
- POJ - 2456 Aggressive cows 二分 最大化最小值
Aggressive cows Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 18099 Accepted: 8619 ...
随机推荐
- Python-多线程学习总结
我们在做软件开发的时候很多要用到多线程技术.例如如果做一个下载软件象flashget就要用到.象在线视频工具realplayer也要用到因为要同时下载media stream还要播放.其实例子是很多的 ...
- 查看Linux磁盘空间大小命令
发表于:2012-09-17 10:25 查看Linux磁盘空间大小 一.df 命令: df 是来自于coreutils 软件包,系统安装时,就自带的:我们通过这个命令可以查看磁盘的使用情况以及文 ...
- 【LeetCode】69. Sqrt(x) (2 solutions)
Sqrt(x) Implement int sqrt(int x). Compute and return the square root of x. 解法一:牛顿迭代法 求n的平方根,即求f(x)= ...
- Android--全局变量 很好很强大
As you know, each Activity is also a Context, which is information about its execution environment i ...
- 深入理解Android的密度独立性
前言 安卓是一个只对硬件设备限制有很少限制的移动操作系统.生产商们几乎可以创造任何形状的.尺寸的和密度的屏幕的设备.设备可以有物理键盘和按钮或者只有虚 拟键盘和按钮.由于它的设备客制化的自由性给软件开 ...
- CSC时无法找到C:\Program Files\Microsoft SDKs\Windows\v6.0A\lib
偶然用到CSC编译c#程序,出现如下问题: warning CS1668: “LIB 环境变量”中指定的搜索路径“C:\Program Files\Microsoft SDKs\Wind ...
- 机器学习基石第三讲:types of learning
博客已经迁移至Marcovaldo's blog (http://marcovaldong.github.io/) 刚刚完毕机器学习基石的第三讲.这一讲主要介绍了机器学习的分类.对何种问题应该使用何种 ...
- layui单文件上传
function imguload(cls) { var taskId = $("#model-taskId").val(); var processInstanceId = $( ...
- jvm默认垃圾收集器
jdk1.7 默认垃圾收集器Parallel Scavenge(新生代)+Parallel Old(老年代) jdk1.8 默认垃圾收集器Parallel Scavenge(新生代)+Parallel ...
- spring boot热部署pom.xml配置
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/20 ...