Description

 

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 calledscribers. 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

题意:要抄N本书,编号为1,2,3...N, 每本书有1<=x<=10000000页, 把这些书分配给K个抄写员,要求分配给某个抄写员的那些书的编号必须是连续的。每个抄写员的速度是相同的,求所有书抄完所用的最少时间的分配方案。如果有多种分配方案,尽可能的往前面划分

解题思路:

1.要求最少时间,取决于抄书最多的人。所以只有抄书最多的人,抄的尽可能少,时间才最少。所以这题就变成了使最大值尽量小的问题了。

下面考虑下一个问题,哪个值是最大序列的最小值呢?我们不妨考虑下最大序列的范围,不难想到它是   最小页数min——整个序列之和sum 。我们要从中找到他的最大序列的最小值。因为每本书有有1<=x<=10000000页,所以sum绝逼会大于int,查找这个范围也是巨大的。为了不超时,所以就用二分法.......(其实为什么用2分法,我也不知道,自己想的一个牵强的理由。反正书上是说用二分法....)

2. 然后就是用二分法缩小范围直到找到那个最大序列的最小值,这里用x表示.....   肿么找x呢!你可以用二分法,先求x范围的中点,然后从序列a[0]开始求和,如果加到第i个数

发现它大于中点,说明x在后半段,变左端点,反之,说明在在前半段,变右端点。最后找到x。

3. 最后就简单了,只需要标记,然后输出就好了....

这里给两个代码,第一个是答案代码,第二个是我测试加的一些输出.....  也许对理解有些帮助........

1.正确代码:

 #include <stdio.h>
#include <string.h>
#define LL long long
int min=,m,k,a[],ans[];
LL sum=;
bool juge(LL x)
{
LL sum2=;
int t=k;
for(int i=;i<m;i++){
sum2+=a[i];
if(sum2>x){
t--; //记录划得次数
i--;
sum2=;
}
if(!t){ // 如果划完了
if(i!=m-) return false; //划完了,但是没有划到最后一个,说明最大序列的最小值比现在的x大.....
else return true;
}
}
return true;
} void zhaox()
{
memset(ans,,sizeof(ans));
LL l=min,r=sum,mid;
while(l<r){ //当左端点等于右端点就确定了一个数x
mid=(r+l)/; //中点
if(juge(mid))  //判断是在中点的哪边
r=mid;
else
l=mid+;
}
LL sum3=;
for(int i=m-;i>=;i--){
sum3+=a[i];
if(sum3>r){
sum3=;
k--;
ans[++i]=; //标记
}
} while(k>){  //如果没有划完,接着划,这里按照题意,需要使前面的尽可能小,所以从前面开始划..(从1开始是因为输出的关系吧,我猜的,还不是很明白...)
for(int i=;i<m;i++){
if(!ans[i]) {
ans[i]=;  //标记
k--;
break;
}
}
}
//print();
printf("%d",a[]);
for(int i=;i<m;i++){
if(ans[i]) printf(" /");
printf(" %d",a[i]);
}
printf("\n");
}
int main()
{
int T;
scanf("%d",&T);
while(T--){
scanf("%d%d",&m,&k);
for(int i=;i<m;i++){
scanf("%d",&a[i]);
if(min>a[i]) min=a[i];  //计算范围
sum+=a[i]; //计算范围
}
zhaox(); // 找x的函数加上输出的部分
}
return ;
}

实验代码:

 #include <stdio.h>
#include <string.h>
#define LL long long
int min=,m,k,a[],ans[];
LL sum=,j=;
bool juge(LL x)
{
printf("\n第%d次\n",j++);
LL sum2=;
int t=k;
for(int i=;i<m;i++){
sum2+=a[i];
if(sum2>x){
sum2=;
t--;
i--;
printf("%d ",a[i]);
} if(!t){
if(i!=m-) {printf("\n%d\n",); printf("%d %d\n",a[i],a[m-]); return false;}
else {printf("\n%d\n",); return true;}
}
}
printf("\n%d\n",);
return true;
} void zhaox()
{
memset(ans,,sizeof(ans));
LL l=min,r=sum,mid;
while(l<r){
mid=(r+l)/;
printf("\nx=%d\n",r);
if(juge(mid))
r=mid;
else
l=mid+;
}
LL sum3=;
for(int i=m-;i>=;i--){
sum3+=a[i];
if(sum3>r){
sum3=;
k--;
ans[++i]=;
}
}
while(k>){
for(int i=;i<m;i++){
if(!ans[i]) {
ans[i]=;
k--;
break;
}
}
}
//print();
printf("%d",a[]);
for(int i=;i<m;i++){
if(ans[i]) printf(" /");
printf(" %d",a[i]);
}
printf("\n");
}
int main()
{
int T;
scanf("%d",&T);
while(T--){
scanf("%d%d",&m,&k);
for(int i=;i<m;i++){
scanf("%d",&a[i]);
if(min>a[i]) min=a[i];
sum+=a[i];
}
zhaox();
}
return ;
}

抄书(UVa714)的更多相关文章

  1. 高效算法——B 抄书 copying books,uva714

    Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Submit Status Description ...

  2. 抄书(B - 二分查找)

    抄书  (二分查找+贪心) 提示:二分查找一般写成非递归形式 时间复杂度:O(logn) 题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action? ...

  3. C#本质论第四版-1,抄书才能看下去,不然两三眼就看完了,一摞书都成了摆设。抄下了记忆更深刻

    C#本质论第四版-1,抄书才能看下去,不然两三眼就看完了,一摞书都成了摆设.抄下了记忆更深刻 本书面向的读者 写作本书时,我面临的一个挑战是如何持续吸引高级开发人员眼球的同时,不因使用assembly ...

  4. UVa 714 抄书(贪心+二分)

    https://vjudge.net/problem/UVA-714 题意:把一个包含m个正整数的序列划分成k个非空的连续子序列,使得每个正整数恰好属于一个序列.设第i个序列的各数之和为S(i),你的 ...

  5. 3162 抄书问题(划分dp)

    3162 抄书问题 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 钻石 Diamond 题解 查看运行结果 题目描述 Description 现在要把M本有顺序的书分给K个人复制( ...

  6. codevs 3162 抄书问题

    3162 抄书问题 题目描述 Description 现在要把M本有顺序的书分给K个人复制(抄写),每一个人的抄写速度都一样,一本书不允许给两个(或以上)的人抄写,分给每一个人的书,必须是连续的,比如 ...

  7. codevs3162抄书问题(划分型dp)

    3162 抄书问题  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 钻石 Diamond   题目描述 Description 现在要把M本有顺序的书分给K个人复制(抄写),每 ...

  8. 抄书抄博客毒害社区的Writer几时休?

    曾几何时,博客园用户也渐渐分成了两类人:Writer和Coder. 何为Coder?就是认认真真写代码,平时分享工作中的一些问题.好的解决方案,或者写一些实用的原理技术文.答疑解惑的教程技术文. 何为 ...

  9. codevs3163 抄书问题2

    题目描述 Description 现在要把M本有顺序的书分给K个人复制(抄写),每一个人的抄写速度都一样,一本书不允许给两个(或以上)的人抄写,分给每一个人的书,必须是连续的,比 如不能把第一.第三. ...

随机推荐

  1. MongoDB,HDFS, Spark to 电影推荐

    http://www.infoq.com/cn/news/2014/12/mongdb-spark-movie-recommend MovieWeb是一个电影相关的网站,它提供的功能包括搜索电影信息. ...

  2. python(5) - 冒泡排序

    data = [10, 4, 33, 21, 54, 3, 8, 11, 5, 22, 2, 1, 17, 13] ''' 思路:有多少个元素就循环多少次,每次循环从第一个元素开始与它后面的元素比较, ...

  3. 关于Eclipse(MyEclipse)中一次性批量导入多个项目Project.

    以前更换Eclipse(MyEclipse)的时候要想把原Eclipse中的项目导入到新的Eclipse中的做法是: 1.先把原Eclipse中工作空间中的项目(不包括.metadata文件夹)复制到 ...

  4. 利用SCI做的一个足球答题系统

    SCI,异步串行通信接口,内置独立的波特率产生电路和SCI收发器,可以选择发送8或9个数据位(其中一位可以指定为奇或偶校验位). SCI是全双工异步串行通信接口,主要用于MCU与其他计算机或设备之间的 ...

  5. 创建触发器在表中播入数据时ID自动增长

    ),age )) create or replace trigger gger_tt before insert on ttt for each row when (new.id is null) b ...

  6. PHP中的Trait

    Trait 自 PHP 5.4.0 起,PHP 实现了一种代码复用的方法,称为 trait. Trait 是为类似 PHP 的单继承语言而准备的一种代码复用机制.Trait 为了减少单继承语言的限制, ...

  7. 《HTML5 and Javascript Web Apps》读书笔记要点摘录

    必须要承认的是这本由Wesley Hales编写的书对要进军web apps 的程序员(媛)来说绝对是福音,很薄的一本书简明扼要的说明了web apps的实现原理,实现工具以及优缺点.拾人牙慧,作此摘 ...

  8. 【高性能服务器】Nginx剖析

    引言 Nginx是一个流行的高性能服务器,官方宣称在压力测试下可以支持5万个并发连接,而且占用内存极低.相比于其他昂贵的硬件负载均衡解决方案,Nginx是开源免费的,可以大大降低成本.本文将从一下几个 ...

  9. asp.net常见面试题(一)

    1.索引器 class Player { ]; public int this[int index] { get { || index >= ) { ; } else { return arr[ ...

  10. CF下Split的使用

    在Compact Framework 下 String的Split不能正确的处理 字符串 分割字符串 如对字符串 "abcfdef12abcedef23" 以"ef&qu ...