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. HTTP协议理解

    HTTP(Hyper Text Transfer Protocol)超文本传输协议,是一种请求响应式协议,类似两国会晤中需要遵守的规则.那么,其中有什么内容特点呢? HTTP特点: 1)支持客户端/服 ...

  2. 故事板 — 视图切换(segue)与传值

    1.传值问题:为什么不能给控件的接口赋值 如执行Segue跳转 [self performSegueWithIdentifier:GAPlayeVideo sender:gaVideo]; //在跳转 ...

  3. fancybox的使用

    fancybox,个人没有深入了解,只是为了工作需要,做的一些界面,主要是用的AJAX功能. 首先,需要下载fancybox的js文件以及CSS文件(可能用不到) 其次,在页面中引入 <scri ...

  4. CentOS(八)--crontab命令的使用方法

    crontab命令常见于Unix和Linux的操作系统之中,用于设置周期性被执行的指令.该命令从标准输入设备读取指令,并将其存放于"crontab"文件中,以供之后读取和执行. 在 ...

  5. hdu1560 搜索

    从原串的最大长度开始枚举,当某个长度的值能保存所有串时,即成功.对每个长度进行深搜,每次取某个串的第一个. #include<iostream> #include<cstdio> ...

  6. sql中在查询时两列合并为一列

    select  a + ' '+b  as ab from tableName 如果是有整型的(例,b字段为int类型): select a +' '+Convert(VARCHAR(50),b) a ...

  7. Android OOM 解决方案

    Out of Memory(内存溢出) 几乎是每个Android程序员都会遇到的事.在网上也能找到一大堆的解决方案,之前写过一篇<Android 内存溢出管理与测试>的博文.但感觉写得不是 ...

  8. vs如何新建自己工程的环境变量(局部)和 Windows系统(全局).

    来源:http://blog.csdn.net/jtop0/article/details/7574139        在vs2008的Project->Property设置里经常会看到类似$ ...

  9. JS 截取字符串函数

    一.函数:split() 功能:使用一个指定的分隔符把一个字符串分割存储到数组 例子: str=”jpg|bmp|gif|ico|png”; arr=theString.split(”|”); //a ...

  10. OCI的结果输出

    绑定变量,把结果以列的方式输出到每一字段输出到一个数组里