题意:

给定N个位置,把C头牛分别放入,求相邻两头牛的最大距离。

分析:

即为求两头牛之间最小距离的最大值。二分搜索答案。

代码:

#include<iostream>
#include<algorithm>
#include<cstdio>
using namespace std;
const int maxn = 100005, INF =0x3fffffff;
typedef long long ll;
int x[maxn];
int N, C;
int judge(int a)
{
int last = 0, cnt = 1, tmp;
while(last < N - 1){
tmp = last + 1;
while(x[tmp] - x[last] < a) tmp++;
if(tmp < N) cnt++;
if(cnt >= C) return 1;
last = tmp ;
}
return 0;
}
int main (void)
{
scanf("%d%d",&N,&C);
for(int i = 0; i < N; i++) scanf("%d",&x[i]);
sort(x, x + N);
int l = 0, r = INF, mid;
while(l<r-1){
mid = l + (r - l)/2;
if(judge(mid)) l = mid;
else r = mid;
}
printf("%d\n",l);
}

POJ 2456_Aggressive cows的更多相关文章

  1. 树状数组 POJ 2481 Cows

    题目传送门 #include <cstdio> #include <cstring> #include <algorithm> using namespace st ...

  2. POJ 2481 Cows

    Cows Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 16546   Accepted: 5531 Description ...

  3. 2018.07.08 POJ 2481 Cows(线段树)

    Cows Time Limit: 3000MS Memory Limit: 65536K Description Farmer John's cows have discovered that the ...

  4. POJ 2481 Cows (线段树)

    Cows 题目:http://poj.org/problem?id=2481 题意:有N头牛,每仅仅牛有一个值[S,E],假设对于牛i和牛j来说,它们的值满足以下的条件则证明牛i比牛j强壮:Si &l ...

  5. POJ 2481 Cows(树状数组)

                                                                      Cows Time Limit: 3000MS   Memory L ...

  6. POJ 3348 - Cows 凸包面积

    求凸包面积.求结果后不用加绝对值,这是BBS()排序决定的. //Ps 熟练了template <class T>之后用起来真心方便= = //POJ 3348 //凸包面积 //1A 2 ...

  7. POJ 2186-Popular Cows (图论-强联通分量Korasaju算法)

    题目链接:http://poj.org/problem?id=2186 题目大意:有n头牛和m对关系, 每一对关系有两个数(a, b)代表a牛认为b牛是“受欢迎”的,且这种关系具有传递性, 如果a牛认 ...

  8. POJ 2481 Cows (数组数组求逆序对)

    题目链接:http://poj.org/problem?id=2481 给你n个区间,让你求每个区间被真包含的区间个数有多少,注意是真包含,所以要是两个区间的x y都相同就算0.(类似poj3067, ...

  9. POJ 3621Sightseeing Cows

    Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 9851   Accepted: 3375 Description Farme ...

随机推荐

  1. $.extend(x,y); 函数用法介绍。

    第一篇资料:  转自: https://www.cnblogs.com/yuqingfamily/p/5813650.html 语法:jQuery.extend( [deep ], target, o ...

  2. koa2实现简易的webpack-dev-server

    koa2实现简易的webpack-dev-server热更新 原文https://github.com/zhuangZhou/Blog/issues/3 闲来无事,用koa2撸了一个简易的webpac ...

  3. 如何向妻子解释OOD (转)

       此文译自CodeProject上<How I explained OOD to my wife>一文,该文章在Top Articles上排名第3,读了之后觉得非常好,就翻译出来,供不 ...

  4. windows 下使用sqlplus

    1.连接数据库 找到tnsnames.ora 监听器配置 TESTDB = (DESCRIPTION = (ADDRESS = (PROTOCOL = TCP)(HOST = "服务器端ip ...

  5. js获取select选中的标签option的值

      js中获取方法 var obj = document.getElementByIdx_xx_x(”testSelect”); //定位id var index = obj.selectedInde ...

  6. Request.Form("id")与Request.QueryString("id")的区别

    获取以表单提交的post类型的值,也就是获取<form method="post">这种方式提交的值request包含各种方式的值,没有指名是什麽,所以Request. ...

  7. Fragment中获取Activity的Context (转)

    Fragment中获取Activity的Context时只需要this.getActivity()即可.     而不是许多人说的this.getActivity().getApplicationCo ...

  8. (转)淘淘商城系列——使用maven tomcat插件启动web工程

    http://blog.csdn.net/yerenyuan_pku/article/details/72672138 上文我们一起学习了怎样搭建maven工程,这篇文章我就来教大家一起学习怎样用to ...

  9. IDEA常见问题

    IDEA常见问提解决 一:拉取git代码认证失败(无法重新输入账户和密码) git config --system --unset credential.helper   二:取消新建文件自动添加到S ...

  10. freenas 系统可能存在的bug

    1.portal 中ip端口显示有问题. 2.创建extend/target映射之后重启iscsi服务有的时候不能启动. 3.后台/usr /etc 重启系统会自动还原.