bzoj 1734: [Usaco2005 feb]Aggressive cows 愤怒的牛【二分+贪心】
二分答案,贪心判定
#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
const int N=100005;
int n,m,a[N];
int read()
{
int r=0,f=1;
char p=getchar();
while(p>'9'||p<'0')
{
if(p=='-')
f=-1;
p=getchar();
}
while(p>='0'&&p<='9')
{
r=r*10+p-48;
p=getchar();
}
return r*f;
}
bool ok(int w)
{
int tot=1,pr=1,la=1;
while(tot<=m)
{
while(a[pr]-a[la]<w&&pr<=n)
pr++;
if(pr>n&&tot<m)
return 0;
tot++,la=pr;
}
return 1;
}
int main()
{
n=read(),m=read();
for(int i=1;i<=n;i++)
a[i]=read();
sort(a+1,a+1+n);
int l=1,r=1e9,ans=0;
while(l<=r)
{
int mid=(l+r)>>1;
if(ok(mid))
l=mid+1,ans=mid;
else
r=mid-1;
}
printf("%d\n",ans);
return 0;
}
bzoj 1734: [Usaco2005 feb]Aggressive cows 愤怒的牛【二分+贪心】的更多相关文章
- BZOJ 1734: [Usaco2005 feb]Aggressive cows 愤怒的牛( 二分答案 )
最小最大...又是经典的二分答案做法.. -------------------------------------------------------------------------- #inc ...
- bzoj 1734: [Usaco2005 feb]Aggressive cows 愤怒的牛
1734: [Usaco2005 feb]Aggressive cows 愤怒的牛 Description Farmer John has built a new long barn, with N ...
- 1734: [Usaco2005 feb]Aggressive cows 愤怒的牛
1734: [Usaco2005 feb]Aggressive cows 愤怒的牛 Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 217 Solved: ...
- bzoj1734 [Usaco2005 feb]Aggressive cows 愤怒的牛 二分答案
[Usaco2005 feb]Aggressive cows 愤怒的牛 Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 407 Solved: 325[S ...
- B1734 [Usaco2005 feb]Aggressive cows 愤怒的牛 二分答案
水题,20分钟AC,最大值最小,一看就是二分答案... 代码: Description Farmer John has built a <= N <= ,) stalls. The sta ...
- bzoj1734 [Usaco2005 feb]Aggressive cows 愤怒的牛
Description Farmer John has built a new long barn, with N (2 <= N <= 100,000) stalls. The stal ...
- BZOJ 1738: [Usaco2005 mar]Ombrophobic Bovines 发抖的牛( floyd + 二分答案 + 最大流 )
一道水题WA了这么多次真是.... 统考终于完 ( 挂 ) 了...可以好好写题了... 先floyd跑出各个点的最短路 , 然后二分答案 m , 再建图. 每个 farm 拆成一个 cow 点和一个 ...
- 题解 yzoj1663: 愤怒的牛(二分) yzoj1662: 曲线(三分)
话说二分和三分的题还没有整理过,就趁这两题来整理下笔记 先讲讲关于二分,对于二分的具体边界长期以来对我来说都是个玄学问题,都是边调边拍改对的.思路大体是确定左边界l,和有边界r,判断满足条件缩小范围. ...
- [BZOJ 1733] [Usaco2005 feb] Secret Milking Machine 【二分 + 最大流】
题目链接:BZOJ - 1733 题目分析 直接二分这个最大边的边权,然后用最大流判断是否可以有 T 的流量. 代码 #include <iostream> #include <cs ...
随机推荐
- JavaScript编程那些事(牛客网 LeetCode)
计算给定数组 arr 中所有元素的总和 本人提供常规方法 function sum(arr) { var len = arr.length; var sum = 0; if(len == 0){ su ...
- Flask(4):wtforms组件 & 数据库连接池 DBUtils
wtforms 组件的作用: --- 生成 HTML 标签 --- form 表单验证 示例代码: app.py from flask import Flask, render_template, r ...
- Rooks-LightOj1005(规律)
A rook is a piece used in the game of chess which is played on a board of square grids. A rook can o ...
- Maximum Product Subarray(最大连续乘积子序列)
Find the contiguous subarray within an array (containing at least one number) which has the largest ...
- Android GIS开发系列计划
本系列博客的整理与写作计划如下,计划3个月(至2018.2)完成. 第一部分, 入门季 第二部分, Android基础季 第三部分, Data Flow 季 第四部分, 可视化季 第五部分, GIS常 ...
- PLSQL Developer来实现不同数据库的表结构以及表数据同步
PLSQL Developer菜单条中 Tools选项下有Compare User Objects和Compare Table Data功能. 一.Tools --> compare user ...
- Office Excel找不到PERSONAL.XLS怎么办
网上有人说这个文件在XLSTART里面,但是我里面没东西 打开PERSONAL.XLS的情况下,点击文件,属性,弹出窗口就有他的位置 你还是直接用Everything搜索一下吧.
- PHP开发出来的万年历
<?php /** * PHP万年历 */ class Calendar{ protected $_table;//table表格 protected $_currentDate;//当前日期 ...
- Watcher 实现机制之client注冊
Zookeeper 提供的了分布式数据的公布/订阅功能,通过 Watch 机制来实现这样的分布式的通知功能. Zookeeper 同意client向server注冊一个Watch监听.当服务端的一些指 ...
- LeetCode_Mysql_Second Highest Salary
176. Second Highest Salary 1. 问题描写叙述: 写一个sql语句从 Employee 表里获取第二高位的工资. 2. 解决思路: 这道题非常easy,就当热身了.首先用ma ...