poj 2456 Aggressive cows && nyoj 疯牛 最大化最小值 二分
poj 2456 Aggressive cows && nyoj 疯牛 最大化最小值 二分
题目链接:
nyoj : http://acm.nyist.net/JudgeOnline/problem.php?pid=586
poj : http://poj.org/problem?id=2456
思路:
二分答案,从前到后依次排放m头牛的位置,检查是否可行
代码:
#include <iostream>
#include <algorithm>
#include <stdio.h>
#include <string.h>
using namespace std;
const int maxn = 1e5+5;
const int INF = 1e9;
int a[maxn];
int n,m;
bool check(int d) {
int pos,pre;
pre=0;
for(int i=0;i<m-1;++i) {
pos=pre+1;
while(pos<n&&a[pos]-a[pre]<d) {pos++;}
if(pos==n) return false;
pre=pos;
}
return true;
}
int main() {
while(~scanf("%d %d",&n,&m)) {
for(int i=0;i<n;++i) scanf("%d",&a[i]);
sort(a,a+n);
int l=0,r=INF,mid,ans;
while(l<=r) {
mid=(l+r)>>1;
if(check(mid)) ans=mid,l=mid+1;
else r=mid-1;
}
printf("%d\n",ans);
}
return 0;
}
poj 2456 Aggressive cows && nyoj 疯牛 最大化最小值 二分的更多相关文章
- poj 2456 Aggressive cows(二分搜索之最大化最小值)
Description Farmer John has built a <= N <= ,) stalls. The stalls are located along a straight ...
- 二分搜索 POJ 2456 Aggressive cows
题目传送门 /* 二分搜索:搜索安排最近牛的距离不小于d */ #include <cstdio> #include <algorithm> #include <cmat ...
- 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: 7924 Accepted: 3959 D ...
- POJ 2456 Aggressive cows (二分)
题目传送门 POJ 2456 Description Farmer John has built a new long barn, with N (2 <= N <= 100,000) s ...
- POJ 2456 Aggressive cows
Aggressive cows Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 11192 Accepted: 5492 ...
- [POJ] 2456 Aggressive cows (二分查找)
题目地址:http://poj.org/problem?id=2456 最大化最小值问题.二分牛之间的间距,然后验证. #include<cstdio> #include<iostr ...
- POJ 2456 Aggressive cows ( 二分搜索)
题目链接 Description Farmer John has built a new long barn, with N (2 <= N <= 100,000) stalls. The ...
- POJ 2456 Aggressive cows(贪心 + 二分)
原题链接:Aggressive cows 题目大意:农夫 建造了一座很长的畜栏,它包括 个隔间,这些小隔间依次编号为. 但是, 的 头牛们并不喜欢这种布局,而且几头牛放在一个隔间里,他们就要发生争 ...
随机推荐
- jerasure 2.0译文
原文地址: 本文译者水平有限,如发现问题请批评指正 Jerasure 2.0:为方便存储相关应用开发的一个基于C开发的纠删码库 版本2.0 James S.Plank Kevin M.Greenan ...
- TinyOS编程思想和Nesc基础语法
TinyOS操作系统由nesc语言写成,从程序员角度看,它的基本作用就是提供了一组API接口以及一些编程规则. 具体来说,基于nesc语言的TinyOS编程行为具有以下特点: a.兼容C语言:使用ne ...
- .13-Vue源码之patch(3)(终于完事)
怎么感觉遥遥无期了呀~这个源码,跑不完了. 这个系列写的不好,仅作为一个记录,善始善终,反正也没人看,写着玩吧! 接着上一节的cbs,这个对象在初始化应该只会调用create模块数组方法,简单回顾一下 ...
- Setup and Configure the vsftpd server in CentOS 7 operation system
############################################################################## 1. close the firewall ...
- 微信小程序左滑删除功能
效果图如下: wxml代码: <view class="container"> <view class="touch-item {{item.isTou ...
- 工具:Entity Framework Profiler
使用方式: 1.项目中添加引用:EntityFrameworkProfiler.Appender 2.应用程序启动方法中添加下面一行代码: HibernatingRhinos.Profiler.App ...
- 【机器学习实战】第12章 使用FP-growth算法来高效发现频繁项集
第12章 使用FP-growth算法来高效发现频繁项集 前言 在 第11章 时我们已经介绍了用 Apriori 算法发现 频繁项集 与 关联规则.本章将继续关注发现 频繁项集 这一任务,并使用 FP- ...
- Ocelot API网关的实现剖析
在微软Tech Summit 2017 大会上和大家分享了一门课程<.NET Core 在腾讯财付通的企业级应用开发实践>,其中重点是基于ASP.NET Core打造可扩展的高性能企业级A ...
- 最最简单的CentOs6在线源搭建
非常实用的在线源搭建,只要4步骤 1.点击进入http://mirrors.aliyun.com/repo/epel-6.repo ,这是阿里云的源 2.复制所有的代码 ctrl+a,ctrl+c ...
- 利用HTML5新特性改变浏览器地址后不刷新页面
原文:http://www.cnblogs.com/xuchengzone/archive/2013/04/18/html5-history-pushstate.html 作为一个程序员,上Git ...