这是一道二分的神奇贪心题,先上题目

Aggressive cows
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 11714 Accepted: 5732
Description

Farmer John has built a new long barn, with N (2 <= N <= 100,000) stalls. The stalls are located along a straight line at positions x1,…,xN (0 <= xi <= 1,000,000,000).

His C (2 <= 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?
Input

  • Line 1: Two space-separated integers: N and C

  • Lines 2..N+1: Line i+1 contains an integer stall location, xi
    Output

  • Line 1: One integer: the largest minimum distance
    Sample Input

5 3
1
2
8
4
9
Sample Output

3
Hint

OUTPUT DETAILS:

FJ can put his 3 cows in the stalls at positions 1, 4 and 8, resulting in a minimum distance of 3.

Huge input data,scanf is recommended.
题目翻译:
大概就是说fj与他的牛相爱相杀,fj要给牛建棚子,每个棚子都在一条直线上,都有不同的坐标,但是牛并不喜欢跟其他的牛住得太近,所以要求找出最小的最大距离。
输入:
一个棚子数,一个牛数。
输出:
最小的最大距离。

这个题很多人先懵在最小最大距离上,这个我们图示样例看一看:
所以,我们要找的最优解就是一个放1,一个放4,一个9。
最小的距离就是4-1=3。
这样的意思就是让牛们住得最分散,找出最小的距离。
然后我们考虑到算法上去。
代码(注释详解代码):

#include<stdio.h>
#include<cmath>
#include<vector>
#include<functional>
#include<queue>
#include<string.h>
#include<algorithm>
#include<iostream>
using namespace std;
const int maxn=100011;struct cmp{bool operator()(int &a,int &b){return a>b;}
};//这是一个优先队列的比较函数。 int n,c,k;
int array[maxn],qu[maxn];
int main()
{priority_queue<int,vector<int>,cmp>que;//让优先队列从小到大排序。 cin>>n>>c;for(int i=1;i<=n;i++){int x;cin>>x;que.push(x);}for(int i=0;i<n;i++){array[i]=que.top();que.pop();}//以上为输入,将输入的牛棚按与原点的距离排一遍 int ls=0,rs=1e9;//二分套路,总在一个范围内进行寻找。这里没有办法知道最大值可能是什么情况,所以用一个1e9代替。 while(rs-ls>1){int midd=(rs+ls)/2;//以下为算法核心 int find=0,flag=1;//先找一个标识变量find和一个判断变量flag for(int i=1;i<c;i++)//因为寻找的是距离,所以遍历的次数是总数-1{int cnt=find+1; // 而且我们默认了第一头牛在第一个棚子里while(cnt<n &&array[cnt]-array[find]<midd){cnt++;}if(cnt==n){flag=0;break;}elsefind=cnt;//这个地方是比较神奇的,我们先默认了第一个牛在第一个棚子里,然后让他开始找,跟每一个标识find进行比较,如果小于midd,就让cnt++; //当cnt在重复的比较中满足了条件,一直加到了n,就是能找到小的,就break。 }if(flag)//他满足了条件,就说明要么是找到了,要么是找小了,就让它等于ls,接着二分进去。 {ls=midd;}else{rs=midd;}//*********** }cout<<ls<<endl;return 0;
}

其实这个题的输出更是一个神奇的东西,这里输出要输出ls,因为我们每次都是在满足条件的情况下让ls=midd,所以,即使找到了答案,也是ls找到的,故要输出ls。
这个二分题应该已经不是入门难度了……

二分入门——poj 2456 aggressive cows相关推荐

  1. 二分搜索 POJ 2456 Aggressive cows

    题目传送门 1 /* 2 二分搜索:搜索安排最近牛的距离不小于d 3 */ 4 #include <cstdio> 5 #include <algorithm> 6 #incl ...

  2. POJ 2456 Aggressive cows(二分答案)

    Aggressive cows Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 22674 Accepted: 10636 Des ...

  3. poj 2456 Aggressive cows 【二分+最大化最小值】

    题目链接:http://poj.org/problem?id=2456 Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 210 ...

  4. POJ 2456 - Aggressive cows(二分)

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

  5. POJ 2456 Aggressive cows ( 二分 贪心 )

    题意 : 农夫 John 建造了一座很长的畜栏,它包括N (2 <= N <= 100,000)个隔间,这些小隔间依次编号为x1,...,xN (0 <= xi <= 1e9) ...

  6. poj 2456 Aggressive cows(贪心+二分)

    描述 农夫 John 建造了一座很长的畜栏,它包括N (2 <= N <= 100,000)个隔间,这些小隔间依次编号为x1,...,xN (0 <= xi <= 1,000, ...

  7. OpenJudge 2456 Aggressive cows

    OpenJudge原题链接 题意: 约翰有N个牛棚位,分别在X1,X2,X3-Xn,约翰有C头牛,但是牛不喜欢这样的布局,一旦放进牛棚就会互相斗争,因此约翰希望将C头牛安置进牛棚,使任意两头牛的距离的 ...

  8. 暑期集训3:几何基础 练习题H: POJ - 2456

    2018学校暑期集训第三天--几何基础 练习题H  --   POJ - 2456 Aggressive cows Farmer John has built a new long barn, wit ...

  9. Aggressive cows POJ - 2456

    2456 -- Aggressive cows 题目大意:有c只牛,要把他们放在n个屋子里,每个屋子放一只牛,每个屋子都在一根数轴上,给出他们的坐标,要让每个屋子间的最小距离尽可能的大,问这个最大距离 ...

最新文章

  1. 虚拟中央处理器新星软机公司即将被收购
  2. 小黑小波比.404 (Not Found)
  3. struts2控制标签(一)选择标签,iterator标签,append标签
  4. 全球及中国停车场建设产业十四五盈利模式与建设现状分析报告2022版
  5. Rumor CodeForces - 893C
  6. 最大权闭合图hdu3996
  7. jmeter 插件 监视器 图形界面使用
  8. react学习(73)--子组件this
  9. ios与android指纹识别,iOS开发swift -- 指纹识别
  10. BugkuCTF-MISC题一切有为法如梦幻泡影
  11. 支付宝为何放弃社交梦?
  12. EasyRecovery如何恢复CMake项目文件
  13. 关于本地yum源的创建方法和使用
  14. 图解TCPIP---第五章---IP协议相关技术
  15. Sublime Text安装及配置Python3
  16. 作为程序员,如何防辐射?
  17. matlab 半导体激光模拟工具箱,MATLAB中的激光器仿真
  18. 8.系统研发中的领导意志
  19. 哈工大计算机系统Lab4.Tiny Shell
  20. 走过一片麦田,只能摘一次,并且不能回头,如何保证摘到的麦穗尽可能大

热门文章

  1. 前端人工智能:通过机器学习推导函数方程式--铂金Ⅲ
  2. Word内安装Mathtype
  3. 在线信号测试软件,基站信号测试软件的使用
  4. 丹霞地貌峡谷第一景---云台山
  5. 使用OpenCV+Python进行图像处理的初学者指南
  6. aotm对php加密,在 Atom 中使用 PHP-CS-Fixer
  7. MCI classification
  8. js 验证手机号、密码、短信验证码
  9. xp 本地计算机策略组,Windows XP组策略应用
  10. 第二篇-用Flutter手撸一个抖音国内版,看看有多炫