水题,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
*/

最新文章

  1. jarsigner签名报错Invalid keystore format
  2. 浅谈一下关于iscroll的使用心得
  3. 利用统计学知识为android应用的启动时间做数据分析
  4. Win10添加简体中文美式键盘的方法
  5. 全局变量报错:UnboundLocalError: local variable &#39;l&#39; referenced before assignment
  6. [UEditor]百度编辑器配置总结
  7. 项目需要简单些了个WEB APP 的弹出窗
  8. BZOJ2726: [SDOI2012]任务安排
  9. CMDB资产采集
  10. 宁撞金钟一下,不打破鼓三千,IT人要有志气,要进就进大的好的公司
  11. windows mysql zip 安装
  12. VS2015 使用 Visual Studio Emulator For Android 调试无法命中断点的解决办法?
  13. linux下的网络通信设置:openssh、PuTTY、tightVNC
  14. 20.多线程.join()和setDaemon()的使用
  15. 内联/块级元素的宽高及margin/padding的说明 |||||| 为何img、input等内联元素可以设置宽、高
  16. C# 初学
  17. 数据仓库基础(十二)Informatica组件(2)
  18. Struct2笔记②--完善登陆代码
  19. Mac之如何查看已用端口
  20. IntentService介绍

热门文章

  1. Laravel5.1学习笔记7 视图
  2. CSS样式优先级和权重问题(部分)
  3. Css小动画
  4. java实现简单窗体小游戏----球球大作战
  5. AndroidStudio 内存泄漏的分析过程
  6. 504 Gateway Timeout 异常
  7. UNIX C 信号
  8. tp5 权限设置
  9. Asp.Net使用Yahoo.Yui.Compressor.dll压缩Js|Css
  10. 【[Offer收割]编程练习赛15 C】过河问题