B二分法
<span style="color:#330099;">/*
B - 二分法 基金会
Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u
Submit Status
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
By Grant Yuan
2014.7.15
*/
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<algorithm>
using namespace std;
int a[100002];
int n,c;
int d;
int l,r;
int top;
bool can(int k)
{ //cout<<"k"<<k<<endl;
int last,sum,sta;
sta=0;
sum=1;
for(int i=1;i<n;i++)
{
if(a[i]-a[sta]>=k)
{//cout<<" i"<<i<<" sta"<<sta<<endl;
sum++;
sta=i;
}
}
//cout<<"sum"<<sum<<endl;
if(sum>=c)
return true;
else
return false;
} int main()
{ int i;
int num;
int last;
while(~scanf("%d%d",&n,&c)){
// cin>>n>>c;
for(i=0;i<n;i++)
cin>>a[i];
//top=i;
sort(a,a+n);
l=a[0];r=a[n-1]+1;
int mid;
while(r-l>1){
mid=(l+r)/2;
if(can(mid)){
l=mid;
}
else
{
r=mid;
}
}
cout<<l<<endl; } }
</span>
版权声明:本文博客原创文章,博客,未经同意,不得转载。
B二分法的更多相关文章
- C语言两种查找方式(分块查找,二分法)
二分法(必须要保证数据是有序排列的): 分块查找(数据有如下特点:块间有序,块内无序):
- poj3122-Pie(二分法+贪心思想)
一,题意: 有f+1个人(包括自己),n块披萨pie,给你每块pie的半径,要你公平的把尽可能多的pie分给每一个人 而且每个人得到的pie来自一个pie,不能拼凑,多余的边角丢掉.二,思路: 1,输 ...
- 二分法&三分法
ural History Exam 二分 #include <iostream> #include <cstdlib> using namespace std; //二分 ...
- [No000087]Linq排序,SortedList排序,二分法排序性能比较
using System; using System.Collections; using System.Collections.Generic; using System.Diagnostics; ...
- [PHP]基本排序(冒泡排序、快速排序、选择排序、插入排序、二分法排序)
冒泡排序: function bubbleSort($array){ $len=count($array); //该层循环控制 需要冒泡的轮数 for($i=1;$i<$len;$i++){ / ...
- iOS常见算法(二分法 冒泡 选择 快排)
二分法: 平均时间复杂度:O(log2n) int halfFuntion(int a[], int length, int number) { int start = 0; int end = l ...
- java简单的二分法排序
二分法排序的思路:数据元素要按顺序排列,对于给定值 x,从序列的中间位置开始比较,如果当前位置值等于 x,则查找成功:若 x 小于当前位置值,则在数列的前半段中查找:若 x 大于当前位置值则在数列的后 ...
- 使用二分法查找mobile文件中区号归属地
#!/usr/bin/env python #coding:utf-8 ''' Created on 2015年12月8日 @author: DL @Description: 使用二分法查找mobil ...
- Atitit 迭代法 “二分法”和“牛顿迭代法 attilax总结
Atitit 迭代法 "二分法"和"牛顿迭代法 attilax总结 1.1. ."二分法"和"牛顿迭代法"属于近似迭代法1 1. ...
- 02python算法-二分法简介
老规矩: 什么是二分法: 其实是一个数学领域的词,但是在计算机领域也有广泛的使用. 为什么需要二分法? 当穷举算法性能让你崩溃时. 二分法怎么用呢? 让我们先玩一个游戏先,我心里想一个100以内的整数 ...
随机推荐
- 【Linux探索之旅】第一部分第三课:测试并安装Ubuntu
内容简介 1.第一部分第三课:测试并安装Ubuntu 2.第一部分第四课预告:磁盘分区 测试并安装Ubuntu 大家好,经过前两个比较偏理论(是否想起了带着瓜皮帽,手拿折扇的老学究,或者腐儒)的课程, ...
- Apple Watch 1.0 开发介绍 1.4 简介 使用iOS技术
WatchKit extension可以使用iOS app同样的技术,但是由于他是extension,使用有些技术的时候会有限制,有些不推荐使用.下面是一些介绍以及什么时候使用什么技术: 有些需要权限 ...
- ef添加字段
先在实体类里添加字段 ,然后执行 Add-Migration updateNumberOfLikes Update-Database -Verbose
- 读书笔记之SQL注入漏洞和SQL调优
原文:读书笔记之SQL注入漏洞和SQL调优 最近读了程序员的SQL金典这本书,觉得里面的SQL注入漏洞和SQL调优总结得不错,下面简单讨论下SQL注入漏洞和SQL调优. 1. SQL注入漏洞 由于“' ...
- Android学习路径(十)怎么会Action Bar堆放在布局
默认情况下.action bar出如今activity窗体的顶部.稍微降低了activity布局的总空间. 假设你想隐藏或者显示action bar.在这堂用户体验的课程中,你能够通过调用hide() ...
- RH133读书笔记(6) - Lab 6 Adding New Filesystems to the Filesystem Tree
Lab 6 Adding New Filesystems to the Filesystem Tree Goal: Develop skills and knowlege related to par ...
- C#中的动态特性
众所周知,C#和Java一样,都是一门静态语言.在C# 4.0之前,想要和动态语言(诸如Python.Javascript等)进行方便地互操作是一件不太容易的事情.而C# 4.0为我们带来的dynam ...
- MEF初体验之七:Using Catalogs
MEF特性化编程模型的价值主张之一是通过catalogs动态发现部件的能力.Catalogs允许应用程序很容易地消费那些通过[Export]已经自我注册的exports. Assembly Catal ...
- 正则、grep、sed、awk
每次用到正则都要蛋疼一下,索性总结一下在这里. 正则 正則表達式主要分为基础正则和扩展正则.注意,正则和一般命令行输入的命令的通配符不同.正则仅仅使用于支持这样的表示法的工具,如:vi,grep,se ...
- UVa - The 3n + 1 problem 解读
这个问题并计算质数了一下相间隔似的.思想上一致. 注意问题: 1 i 可能 大于或等于j -- 这里上传.小心阅读题意,我没有说这个地方不能保证.需要特殊处理 2 计算过程中可能溢出,的整数大于最大值 ...