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 解法 使用二分逐个尝试间隔距离 能否满足要求 检验是否满足要求的函数 使用的思想是贪心 第一个点放一头牛 后面大于等于尝试的距离才放 ...
随机推荐
- KMP算法——Javascript实现
腾讯和阿里的笔试刚过去了,里面有很多题都很值得玩味的.之前Blog积累的很多东西,还要平时看的书,都有很大的帮助.这个深有体会啊! 例如,腾讯有一道算法题是吃香蕉(好邪恶的赶脚..),一次吃一根或者两 ...
- Type Encoding
[Type Encodings] The compiler encodes the return and argument types for each method in a character s ...
- Prefabs
[Prefabs] A Prefab is a type of asset -- a reusable GameObject stored in Project View. Prefabs can b ...
- 创建svn仓库脚本
#!/bin/bash svnadmin create $1 cp /home/svndata/conf/svnserver.conf /home/svndata/$1/conf/svnserve.c ...
- 代码SketchPaintCode绘制
作者:codeGlider 在我的上一篇文章中 swift10分钟实现炫酷的导航控制器跳转动画,有一个swift logo的形状 上一篇文章的动画 我说的就是中间用来做遮罩的形状. 它不是图片是用一段 ...
- .Net实现的批量删除(使用了repeater控件)
前台 <asp:Repeater ID="Repeater1" runat="server"> <HeaderTemplate> < ...
- maven 配置文件详解
<projectxmlns="http://maven.apache.org/POM/4.0.0 " xmlns:xsi="http://www.w3.org/20 ...
- (剑指Offer)面试题31:连续子数组的最大和
题目: 输入一个整型数组,数组里有正数也有负数,数组中一个或连续多个整数组成一个子数组,求所有子数组的和的最大值.要求时间复杂度为O(n) 思路: 1.数组累加 从头到尾逐个累加数组中的每个数字,当累 ...
- 面向过程MySQL数据库链接操作
刚好今天复习到这个章节,将就发布出来,就当是为自己复习了 //链接数据库 $link = mysqli_connect('localhost/IP地址','用户名','密码','数据库名'); //设 ...
- HDU 2066 一个人的旅行 - from lanshui_Yang
Problem Description 虽然草儿是个路痴(就是在杭电待了一年多,居然还会在校园里迷路的人,汗~),但是草儿仍然很喜欢旅行,因为在旅途中 会遇见很多人(白马王子,^0^),很多事,还能丰 ...