Aggressive cows 二分不仅仅是查找
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 7083 | Accepted: 3522 |
Description
His C (2 <= C <= N) cows don't like this barn layout and
become aggressive towards each other once put into a stall. To prevent
the cows from hurting each other, FJ want to assign the cows to the
stalls, such that the minimum distance between any two of them is as
large as possible. What is the largest minimum distance?
Input
* Lines 2..N+1: Line i+1 contains an integer stall location, xi
Output
Sample Input
5 3
1
2
8
4
9
Sample Output
3
Hint
FJ can put his 3 cows in the stalls at positions 1, 4 and 8, resulting in a minimum distance of 3.
Huge input data,scanf is recommended.
Source
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <string>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <sstream>
#include <iomanip>
using namespace std;
const int INF=0x4fffffff;
const int EXP=1e-;
const int MS=; int pos[MS];
int N,M; bool judge(int d)
{
int last=;
int now;
for(int i=;i<M;i++) //贪心法给第i头牛找宿舍
{
now=last+;
while(now<N&&pos[now]-pos[last]<d)
now++;
if(now>=N)
return false;
last=now;
}
return true;
} void solve()
{
sort(pos,pos+N);
int l=,r=INF;
while(r-l>) // 注意是区间,很多时候开区间更方便一些。
{
int mid=(l+r)/;
if(judge(mid))
l=mid;
else
r=mid;
}
printf("%d\n",l);
} int main()
{
while(scanf("%d%d",&N,&M)!=EOF)
{
for(int i=;i<N;i++)
scanf("%d",&pos[i]);
solve();
}
return ;
}
Aggressive cows 二分不仅仅是查找的更多相关文章
- POJ - 2456 Aggressive cows 二分 最大化最小值
Aggressive cows Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 18099 Accepted: 8619 ...
- 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: 7924 Accepted: 3959 D ...
- [POJ] 2456 Aggressive cows (二分查找)
题目地址:http://poj.org/problem?id=2456 最大化最小值问题.二分牛之间的间距,然后验证. #include<cstdio> #include<iostr ...
- 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 二分
Description Farmer John has built a new long barn, with N (2 <= N <= 100,000) stalls. The stal ...
- Aggressive Cows 二分
Farmer John has built a new long barn, with N (2 <= N <= 100,000) stalls. The stalls are locat ...
- POJ2456 Aggressive cows(二分+贪心)
如果C(d)为满足全部牛之间的距离都不小于d. 先对牛舍的位置排序,然后二分枚举d,寻找满足条件的d. #include<iostream> #include<cstdio> ...
- poj 2456 Aggressive cows 二分 题解《挑战程序设计竞赛》
地址 http://poj.org/problem?id=2456 解法 使用二分逐个尝试间隔距离 能否满足要求 检验是否满足要求的函数 使用的思想是贪心 第一个点放一头牛 后面大于等于尝试的距离才放 ...
随机推荐
- 统一入口的Ajax验证
此前一直没有写博客的习惯,只是将一些心得和体会大致的用笔写在一个本子上,今天刚刚开通博客,就随便写一点吧! 关于服务端验证,大致可以分为登陆验证与功能权限验证,而以前端请求方式来区分的话,又可分为 1 ...
- CSS 居中大全
<center> text-align:center 在父容器里水平居中 inline 文字,或 inline 元素 vertical-align:middle 垂直居中 inline 文 ...
- JQuery与GridView控件结合示例
JQuery是一种非常强大的客户端JS编程技术,这里不想过多阐述它的相关背景知识,只想简单演示一下如何与asp.net的控件结合开发. 比如,我们要做一个下面如图所示的功能,效果是状态.编号.数字1. ...
- android 开源 + 一些素材网站
ui 设计工具:http://www.sketchcn.com/ 分类汇总: https://github.com/Trinea/android-open-project 直接拿来用!最火的Andro ...
- 转载:as3.0下对象类型返回值与变量默认值的详细说明
转自:http://www.cuplayer.com/player/PlayerCodeAs/2012/0905367.html Null.NaN.undefined 及各自应用对象:变量的默认值: ...
- linq 动态排序,不使用反射
之前网上搜索的相关方法都是使用了反射的方法来动态获取字段,以实现动态linq排序,但是因为项目组觉得此方法效率低下,所以不予采纳. 所以有了以下代码 public interface IBase{ d ...
- UIWebView 获取当前的javascript上下文,并js,oc互调
OC调用UIWebView 中的js,网上例子很多,最常用的是UIWebView自带的一个方法: - (NSString *)stringByEvaluatingJavaScriptFromStrin ...
- cocos2dx Http网络编程
转自:http://blog.csdn.net/wangbin_jxust/article/details/9632771,http://blog.csdn.net/wangbin_jxust/art ...
- PostgreSQL中 AnyElement AnyArray AnynonArray的区别与联系
http://www.postgresql.org/docs/current/static/extend-type-system.html#EXTEND-TYPES-POLYMORPHIC 先看一个例 ...
- FluentData官方文档翻译
开始 要求 .NET 4.0. 支持的数据库 MS SQL Server using the native .NET driver. MS SQL Azure using the native .NE ...