B1734 [Usaco2005 feb]Aggressive cows 愤怒的牛 二分答案
水题,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 愤怒的牛 二分答案的更多相关文章
- BZOJ 1734: [Usaco2005 feb]Aggressive cows 愤怒的牛( 二分答案 )
最小最大...又是经典的二分答案做法.. -------------------------------------------------------------------------- #inc ...
- bzoj1734 [Usaco2005 feb]Aggressive cows 愤怒的牛 二分答案
[Usaco2005 feb]Aggressive cows 愤怒的牛 Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 407 Solved: 325[S ...
- 1734: [Usaco2005 feb]Aggressive cows 愤怒的牛
1734: [Usaco2005 feb]Aggressive cows 愤怒的牛 Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 217 Solved: ...
- bzoj 1734: [Usaco2005 feb]Aggressive cows 愤怒的牛
1734: [Usaco2005 feb]Aggressive cows 愤怒的牛 Description Farmer John has built a new long barn, with N ...
- bzoj1734 [Usaco2005 feb]Aggressive cows 愤怒的牛
Description Farmer John has built a new long barn, with N (2 <= N <= 100,000) stalls. The stal ...
- bzoj 1734: [Usaco2005 feb]Aggressive cows 愤怒的牛【二分+贪心】
二分答案,贪心判定 #include<iostream> #include<cstdio> #include<algorithm> using namespace ...
- 题解 yzoj1663: 愤怒的牛(二分) yzoj1662: 曲线(三分)
话说二分和三分的题还没有整理过,就趁这两题来整理下笔记 先讲讲关于二分,对于二分的具体边界长期以来对我来说都是个玄学问题,都是边调边拍改对的.思路大体是确定左边界l,和有边界r,判断满足条件缩小范围. ...
- [ACM] poj 2456 Aggressive cows (二分查找)
Aggressive cows Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 5436 Accepted: 2720 D ...
- BZOJ 1738: [Usaco2005 mar]Ombrophobic Bovines 发抖的牛( floyd + 二分答案 + 最大流 )
一道水题WA了这么多次真是.... 统考终于完 ( 挂 ) 了...可以好好写题了... 先floyd跑出各个点的最短路 , 然后二分答案 m , 再建图. 每个 farm 拆成一个 cow 点和一个 ...
随机推荐
- 删除django
1.命令行运行python 2.import django3.print(django.__path__)4.删除django目录即可
- android黑科技系列——Apk混淆成中文语言代码
一.前言 最近想爆破一个app,没有加壳,简单的使用Jadx打开查看源码,结果把我逗乐了,代码中既然都是中文,而且是一些比较奇葩的中文字句,如图所示: 瞬间感觉懵逼了,这app真会玩,我们知道因为Ja ...
- 利用string 字符串拷贝
序言:对于laws的代码,完全从Matlab中转来.其中用到了字符串复制和对比的函数. C++要求: 输入字符串,根据字符串,来确定选择数组,用于下一过程 MatLab代码: (1).文件calLaw ...
- Visual Basic for Application
Private Sub Worksheet_SelectionChange(ByVal Target As Range) 'The note of Visual Basic for Applicati ...
- Python总结1
时间:24日下午输入:input()输出:print()格式化输出通过某种占位符用于替换字符串中某个位置的字符.占位符:%s:可以替换任意类型%d:可以替换数字类型 需要掌握的#1.strip去左右两 ...
- MATLAB图形界面设计(下)
文章参考Blue Mountain https://www.cnblogs.com/BlueMountain-HaggenDazs/p/4307777.html 一.菜单设计 1.建立菜单项 (1)建 ...
- Pytorch基础(6)----参数初始化
一.使用Numpy初始化:[直接对Tensor操作] 对Sequential模型的参数进行修改: import numpy as np import torch from torch import n ...
- Pytorch基础(5)——批数据训练
一.知识点: 相关包:torch.utils.data import torch import torch.utils.data as Data 包装数据类:TensorDataset [包装数据和目 ...
- HDU 1241 Oil Deposits (DFS or BFS)
链接 : Here! 思路 : 搜索判断连通块个数, 所以 $DFS$ 或则 $BFS$ 都行喽...., 首先记录一下整个地图中所有$Oil$的个数, 然后遍历整个地图, 从油田开始搜索它所能连通多 ...
- 51nod1256 乘法逆元【扩展欧几里得】
给出2个数M和N(M < N),且M与N互质,找出一个数K满足0 < K < N且K * M % N = 1,如果有多个满足条件的,输出最小的. Input 输入2个数M, N中间用 ...