抄书  (二分查找+贪心)

提示:二分查找一般写成非递归形式

时间复杂度:O(logn)

题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=85904#problem/B

Description

 

Copying Books

Before the invention of book-printing, it was very hard to make a copy of a book. All the contents had to be re-written by hand by so called scribers. The scriber had been given a book and after several months he finished its copy. One of the most famous scribers lived in the 15th century and his name was Xaverius Endricus Remius Ontius Xendrianus (Xerox). Anyway, the work was very annoying and boring. And the only way to speed it up was to hire more scribers.

Once upon a time, there was a theater ensemble that wanted to play famous Antique Tragedies. The scripts of these plays were divided into many books and actors needed more copies of them, of course. So they hired many scribers to make copies of these books. Imagine you have m books (numbered ) that may have different number of pages (     ) and you want to make one copy of each of them. Your task is to divide these books among k scribes, . Each book can be assigned to a single scriber only, and every scriber must get a continuous sequence of books. That means, there exists an increasing succession of numbers such that i-th scriber gets a sequence of books with numbers between bi-1+1 and bi. The time needed to make a copy of all the books is determined by the scriber who was assigned the most work. Therefore, our goal is to minimize the maximum number of pages assigned to a single scriber. Your task is to find the optimal assignment.

Input

The input consists of N cases. The first line of the input contains only positive integer N. Then follow the cases. Each case consists of exactly two lines. At the first line, there are two integers m and k,     . At the second line, there are integers separated by spaces. All these values are positive and less than 10000000.

Output

For each case, print exactly one line. The line must contain the input succession divided into exactly k parts such that the maximum sum of a single part should be as small as possible. Use the slash character (`/') to separate the parts. There must be exactly one space character between any two successive numbers and between the number and the slash.

If there is more than one solution, print the one that minimizes the work assigned to the first scriber, then to the second scriber etc. But each scriber must be assigned at least one book.

Sample Input

2
9 3
100 200 300 400 500 600 700 800 900
5 4
100 100 100 100 100

Sample Output

100 200 300 400 500 / 600 700 / 800 900
100 / 100 / 100 / 100 100 题意:
输入多组案例T,给你N本书,一个有M个人抄写,使M个人所抄写书所用的时间最少,输出分配方案。(1<=N<=100000,1<=M<=N,每个数在1到10000之间)
如果有多种可能的话,尽量在前面进行划分。 分析:
1.经典的最大值最小化问题,采用 贪心+二分查找
2.输出的时候用到贪心的思想,既尽量往前进行划分
3.利用二分查找找出满足题意的最大值,再利用贪心从后往前的方式划分成段,如果剩余可划分段与i+1的值相等(尽量靠前),则将剩余的段往前划分 代码:
 #include <cstdio>
#include <iostream>
#include <cstring>
using namespace std;
const int MAXN = ; int n, m;
int p[MAXN], use[MAXN]; //标记数组use表示是否划分
long long low_bound, high_bound; //由于high_bound可能很多,用long long 来存储 void init()
{
low_bound=-;
high_bound=;
memset(use,,sizeof(use));
} int solve(int mid) //判断最大值所在区间是左还是右
{
int sum=,group=;
for(int i=n-;i>=;i--)
{
if(sum+p[i]>mid)
{
sum=p[i];
group++;
if(group>m) //看可以分成几组,若group>m,舍弃
return ;
}
else sum+=p[i];
}
return ;
} void print(int high_bound)
{
int group=,sum=;
for(int i=n-;i>=;i--)
{
if(sum+p[i]>high_bound) //从右边开始确定划分
{
use[i]=;
sum=p[i];
group++;
}
else sum+=p[i];
if(m-group==i+)
{
for(int j=;j<=i;j++)
use[j]=;
break;
}
}
for(int i=;i<n-;i++)
{
printf("%d ",p[i]);
if(use[i]) //确定‘/’的位置
printf("/ ");
}
printf("%d\n",p[n-]);
} int main()
{
int T;
scanf("%d",&T);
while(T--)
{
init();
scanf("%d%d",&n,&m);
for(int i=;i<n;i++)
{
scanf("%d",&p[i]);
if(low_bound<p[i])
low_bound=p[i];
high_bound+=p[i];
}
long long x=low_bound,y=high_bound;
while(x<=y) //二分,判断最小的最大值的区间
{
long long mid=x+(y-x)/;
if(solve(mid))
y=mid-;
else x=mid+;
}
print(x);
}
return ;
}
之前上选修课的时候听过贪心算法,但之前没有做过类似的题,感觉做的不是很好。
 

抄书(B - 二分查找)的更多相关文章

  1. jvascript 顺序查找和二分查找法

    第一种:顺序查找法 中心思想:和数组中的值逐个比对! /* * 参数说明: * array:传入数组 * findVal:传入需要查找的数 */ function Orderseach(array,f ...

  2. Java实现的二分查找算法

    二分查找又称折半查找,它是一种效率较高的查找方法. 折半查找的算法思想是将数列按有序化(递增或递减)排列,查找过程中采用跳跃式方式查找,即先以有序数列的中点位置为比较对象,如果要找的元素值小 于该中点 ...

  3. 从一个NOI题目再学习二分查找。

    二分法的基本思路是对一个有序序列(递增递减都可以)查找时,测试一个中间下标处的值,若值比期待值小,则在更大的一侧进行查找(反之亦然),查找时再次二分.这比顺序访问要少很多访问量,效率很高. 设:low ...

  4. java实现二分查找

    /** * 二分查找 * @param a * @param n * @param value * @return * @date 2016-10-8 * @author shaobn */ publ ...

  5. 最新IP地址数据库 二分逼近&二分查找 高效解析800万大数据之区域分布

    最新IP地址数据库  来自 qqzeng.com 利用二分逼近法(bisection method) ,每秒300多万, 比较高效! 原来的顺序查找算法 效率比较低 readonly string i ...

  6. c#-二分查找-算法

    折半搜索,也称二分查找算法.二分搜索,是一种在有序数组中查找某一特定元素的搜索算法. A 搜素过程从数组的中间元素开始,如果中间元素正好是要查找的元素,则搜素过程结束: B 如果某一特定元素大于或者小 ...

  7. 【Python】二分查找算法

    二分查找:在一段数字内,找到中间值,判断要找的值和中间值大小的比较.如果中间值大一些,则在中间值的左侧区域继续按照上述方式查找.如果中间值小一些,则在中间值的右侧区域继续按照上述方式查找.直到找到我们 ...

  8. PHP实现文本快速查找 - 二分查找

    PHP实现文本快速查找 - 二分查找法 起因 先说说事情的起因,最近在分析数据时经常遇到一种场景,代码需要频繁的读某一张数据库的表,比如根据地区ID获取地区名称.根据网站分类ID获取分类名称.根据关键 ...

  9. java二分查找举例讨论

    最近做笔试题有这么一个关于二分查找的例子. 给一个有序数组,和一个查找目标,用二分查找找出目标所在index,如果不存在,则返回-1-(其应该出现的位置),比如在0,6,9,15,18中找15,返回3 ...

随机推荐

  1. 【Delphi内联汇编学习1】Delphi与汇编

    我一直认为Delphi功能与C++相比毫不逊色,提供了丰富的控件和类.全部API以及嵌入的汇编.最近小弟在把C版的Huffman压缩改用Delphi写时,顺便“研究”了一下Delphi的位操作和嵌入式 ...

  2. php 接口示例

    php 接口示例: public function dev(){ $m=new Model('machine_info'); $ip=$_GET['ip']; echo $ip; //$arr=$m- ...

  3. 第25周五迷茫定位&转行理论建议

    今天下午请假办了无房证明和单身证明,准备开始贷款买房的征程,在犹豫纠结中我选择推进这个事情,之前的经验告诉我生活中可以面临改变或不改变境况的选择是要尽可能的选择改变,因为我还年轻.回来后知乎上看了一个 ...

  4. orcale装完sqldevelop启动不了

    一直在搞考试,昨天考java企业级开发要交项目搞得我装系统后又装了个orcale,每次重新配百度太麻烦,还好记得点,记录下碰到的错误 64位的系统下的orcale11 64位里面的sqldevelop ...

  5. python闭包以及装饰器

    通俗的定义:如果在一个内部函数里,对在外部作用域(但不是在全局作用域)的变量进行引用,那么内部函数就被认为是闭包(closure).它只不过是个“内层”的函数,由一个名字(变量)来指代,而这个名字(变 ...

  6. STL-multimap

    转自:http://www.cnblogs.com/xiaoka/archive/2011/08/09/2132342.html multimap提供了可以一种可以有重复键值的STL map类型.其插 ...

  7. SQL Server索引进阶:第四级,页和区

    原文地址: Stairway to SQL Server Indexes: Level 4, Pages and Extents 本文是SQL Server索引进阶系列(Stairway to SQL ...

  8. ASP.NET之电子商务系统开发-4(二级分类)

    一.前言 继上次的订单,这是第四篇.记录一下分类和筛选.这功能是最后做的,因为我完全不懂其原理.后来通过同学的指导(一位很有天赋的同学,比我牛逼一个层次,同样是高三.:D),终于也是完成了.在写这篇博 ...

  9. hdu acm 2154(多解取一解)

    //题目中结果有一条限制就是最后必须跳回A,如果我们的思想框在这个条件上就很容易卡住,因为这样的条件下的路径很难有规律的罗列,然而我们说这个图形中有三个区域,我们算出每个区域的第n-1次的种类数,然后 ...

  10. 上一篇下一篇 排序 (非ID字段排序)

    网上看了很多关于"上一篇下篇"的文章,可大都是按ID排序. 实际上,很少有按ID排序的. 分享下我的单独排序字段的写法,主要分为ms sql2000 和 ms 2005及以上版本. ...