水题,20分钟AC,最大值最小,一看就是二分答案。。。

代码:

Description

Farmer John has built a new long barn, with N ( <= N <= ,) stalls. The stalls are located along a straight line at positions x1,...,xN ( <= xi <= ,,,). His C ( <= 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?

农夫 John 建造了一座很长的畜栏,它包括NN ( <= N <= ,)个隔间,这些小隔间依次编号为x1,...,xN ( <= xi <= ,,,). 但是,John的C ( <= C <= N)头牛们并不喜欢这种布局,而且几头牛放在一个隔间里,他们就要发生争斗。为了不让牛互相伤害。John决定自己给牛分配隔间,使任意两头牛之间的最小距离尽可能的大,那么,这个最大的最小距离是什么呢
Input
* Line : Two space-separated integers: N and C * Lines ..N+: Line i+ contains an integer stall location, xi
第一行:空格分隔的两个整数N和C
第二行---第N+1行:i+1行指出了xi的位置
Output
* Line : One integer: the largest minimum distance
第一行:一个整数,最大的最小值
Sample Input Sample Output 把牛放在1,,8这样最小距离是3

代码:

#include<iostream>
#include<cstdio>
#include<cmath>
#include<ctime>
#include<queue>
#include<algorithm>
#include<cstring>
using namespace std;
#define duke(i,a,n) for(int i = a;i <= n;i++)
#define lv(i,a,n) for(int i = a;i >= n;i--)
#define clean(a) memset(a,0,sizeof(a))
const long long INF = (60LL);
typedef long long ll;
typedef double db;
template <class T>
void read(T &x)
{
char c;
bool op = ;
while(c = getchar(), c < '' || c > '')
if(c == '-') op = ;
x = c - '';
while(c = getchar(), c >= '' && c <= '')
x = x * + c - '';
if(op) x = -x;
}
template <class T>
void write(T x)
{
if(x < ) putchar('-'), x = -x;
if(x >= ) write(x / );
putchar('' + x % );
}
int l,r,maxn = ,n,c,ok;
ll x[];
int main()
{
read(n);
read(c);
duke(i,,n)
{
read(x[i]);
if(x[i] > maxn)
{
maxn = x[i];
}
}
sort(x + ,x + n + );
l = ;
r = maxn;
while(l != r)
{
ok = ;
int mid = (l + r) >> ;
int tot = ,ans = ;
// cout<<endl;
// cout<<mid<<endl;
duke(i,,n)
{
if(x[i] - x[i - ] + tot < mid)
{
tot = tot + x[i] - x[i - ];
}
else
{
// cout<<tot<<" "<<x[i] - x[i - 1]<<endl;
ans ++;
tot = ;
}
if(ans >= c)
{
ok = ;
break;
}
}
if(ok == )
{
l = mid + ;
}
else
{
r = mid;
}
}
ok = ;
int mid = l;
int tot = ,ans = ;
duke(i,,n)
{
if(x[i] - x[i - ] + tot < mid)
{
tot = tot + x[i] - x[i - ];
}
else
{
ans ++;
tot = ;
}
if(ans >= c)
{
ok = ;
break;
}
}
if(ok == )
write(l);
else
write(l - );
return ;
}
/*
7 3
1
2
3
7
8
9
10
*/

B1734 [Usaco2005 feb]Aggressive cows 愤怒的牛 二分答案的更多相关文章

  1. BZOJ 1734: [Usaco2005 feb]Aggressive cows 愤怒的牛( 二分答案 )

    最小最大...又是经典的二分答案做法.. -------------------------------------------------------------------------- #inc ...

  2. bzoj1734 [Usaco2005 feb]Aggressive cows 愤怒的牛 二分答案

    [Usaco2005 feb]Aggressive cows 愤怒的牛 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 407  Solved: 325[S ...

  3. 1734: [Usaco2005 feb]Aggressive cows 愤怒的牛

    1734: [Usaco2005 feb]Aggressive cows 愤怒的牛 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 217  Solved: ...

  4. bzoj 1734: [Usaco2005 feb]Aggressive cows 愤怒的牛

    1734: [Usaco2005 feb]Aggressive cows 愤怒的牛 Description Farmer John has built a new long barn, with N ...

  5. bzoj1734 [Usaco2005 feb]Aggressive cows 愤怒的牛

    Description Farmer John has built a new long barn, with N (2 <= N <= 100,000) stalls. The stal ...

  6. bzoj 1734: [Usaco2005 feb]Aggressive cows 愤怒的牛【二分+贪心】

    二分答案,贪心判定 #include<iostream> #include<cstdio> #include<algorithm> using namespace ...

  7. 题解 yzoj1663: 愤怒的牛(二分) yzoj1662: 曲线(三分)

    话说二分和三分的题还没有整理过,就趁这两题来整理下笔记 先讲讲关于二分,对于二分的具体边界长期以来对我来说都是个玄学问题,都是边调边拍改对的.思路大体是确定左边界l,和有边界r,判断满足条件缩小范围. ...

  8. [ACM] poj 2456 Aggressive cows (二分查找)

    Aggressive cows Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 5436   Accepted: 2720 D ...

  9. BZOJ 1738: [Usaco2005 mar]Ombrophobic Bovines 发抖的牛( floyd + 二分答案 + 最大流 )

    一道水题WA了这么多次真是.... 统考终于完 ( 挂 ) 了...可以好好写题了... 先floyd跑出各个点的最短路 , 然后二分答案 m , 再建图. 每个 farm 拆成一个 cow 点和一个 ...

随机推荐

  1. 实例化vue发生了什么(详解vue生命周期)

    const app = new Vue({ el:"#app', data:{ message:'hello,lifePeriod' }, methods:{ init(){ console ...

  2. java解析注解的简单例子

    代码是根据慕课网的教程写的. 自定义类的注解: package com.immoc.test; import java.lang.annotation.Documented; import java. ...

  3. eclipse中导入maven项目:org.apache.maven.archiver.MavenArchiver.getManifest(org.apache.maven.proje

    org.codehaus.plexus.archiver.jar.Manifest.write(java.io.PrintWriter) 解决方法为:更新eclipse中的maven插件 1.help ...

  4. node、Mongo项目如何前后端分离提供接口给前端

    node接口编写,vue-cli代理接口方法  通常前端使用的MocK 数据的方法,去模拟假的数据,但是如果有node Mongodb 去写数据的话就不需要在去mock 数据了,具体的方法如下. 首先 ...

  5. jsTree checkbox plugin使用笔记

    引入css文件 <link rel="stylesheet" type="text/css" href="js/assets/global/pl ...

  6. 数据库连接池c3p0

    XML配置文件: <?xml version="1.0" encoding="UTF-8"?> <c3p0-config> <!- ...

  7. 【剑指Offer】1、二维数组中的查找

      题目描述:   在一个二维数组中(每个一维数组的长度相同),每一行都按照从左到右递增的顺序排序,每一列都按照从上到下递增的顺序排序.请完成一个函数,输入这样的一个二维数组和一个整数,判断数组中是否 ...

  8. [bzoj3029] 守卫者的挑战 (概率期望dp)

    传送门 Description 打开了黑魔法师Vani的大门,队员们在迷宫般的路上漫无目的地搜寻着关押applepi的监狱的所在地.突然,眼前一道亮光闪过."我,Nizem,是黑魔法圣殿的守 ...

  9. 自学python 第三天

    #!/usr/bin/env python# -*- coding:utf-8 -*- # name = "***"# if "*" in name:# pri ...

  10. 【Codeforces 639B】Bear and Forgotten Tree 3

    [链接] 我是链接,点我呀:) [题意] [题解] 首先,因为高度是h 所以肯定1下面有连续的h个点依次连成一条链.->用了h+1个点了 然后,考虑d这个约束. 会发现,形成d的这个路径,它一定 ...