抄书(UVa714)
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)的更多相关文章
- 高效算法——B 抄书 copying books,uva714
Time Limit:3000MS Memory Limit:0KB 64bit IO Format:%lld & %llu Submit Status Description ...
- 抄书(B - 二分查找)
抄书 (二分查找+贪心) 提示:二分查找一般写成非递归形式 时间复杂度:O(logn) 题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action? ...
- C#本质论第四版-1,抄书才能看下去,不然两三眼就看完了,一摞书都成了摆设。抄下了记忆更深刻
C#本质论第四版-1,抄书才能看下去,不然两三眼就看完了,一摞书都成了摆设.抄下了记忆更深刻 本书面向的读者 写作本书时,我面临的一个挑战是如何持续吸引高级开发人员眼球的同时,不因使用assembly ...
- UVa 714 抄书(贪心+二分)
https://vjudge.net/problem/UVA-714 题意:把一个包含m个正整数的序列划分成k个非空的连续子序列,使得每个正整数恰好属于一个序列.设第i个序列的各数之和为S(i),你的 ...
- 3162 抄书问题(划分dp)
3162 抄书问题 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 钻石 Diamond 题解 查看运行结果 题目描述 Description 现在要把M本有顺序的书分给K个人复制( ...
- codevs 3162 抄书问题
3162 抄书问题 题目描述 Description 现在要把M本有顺序的书分给K个人复制(抄写),每一个人的抄写速度都一样,一本书不允许给两个(或以上)的人抄写,分给每一个人的书,必须是连续的,比如 ...
- codevs3162抄书问题(划分型dp)
3162 抄书问题 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 钻石 Diamond 题目描述 Description 现在要把M本有顺序的书分给K个人复制(抄写),每 ...
- 抄书抄博客毒害社区的Writer几时休?
曾几何时,博客园用户也渐渐分成了两类人:Writer和Coder. 何为Coder?就是认认真真写代码,平时分享工作中的一些问题.好的解决方案,或者写一些实用的原理技术文.答疑解惑的教程技术文. 何为 ...
- codevs3163 抄书问题2
题目描述 Description 现在要把M本有顺序的书分给K个人复制(抄写),每一个人的抄写速度都一样,一本书不允许给两个(或以上)的人抄写,分给每一个人的书,必须是连续的,比 如不能把第一.第三. ...
随机推荐
- LeetCode 260
Single Number III Given an array of numbers nums, in which exactly two elements appear only once and ...
- 【补】【FZU月赛】【20150515】【待续】
A FZU-2054 水题,比较A,B双方的最大值即可. B FZU-2055 string,截取‘.’之前和之后然后和给出的文件夹名和拓展名比较就好了啊,不明白为什么那么多人错. 代码: #incl ...
- Angular2 从0到1 (二)
第一节:Angular2 从0到1 (一)第三节:Angular2 从0到1 (三)第四节:Angular2 从0到1 (四) 作者:王芃 wpcfan@gmail.com 第二节:用Form表单做一 ...
- 关于oracle误删数据的恢复
与数据打交道,免不了会误删一些数据,之后还commit了,连回滚的机会都没了,而更糟糕的是你又没有备份,这种事终于在今天被我不幸的遇上了... 唯一一点值得欣慰的是,我删除表记录的时候,时间不长,一天 ...
- html accesskey (단축키 지정)
accesskey 속성은 마우스 등을 쓰지 않는 환경을 위해 링크나 입력 폼에서 키보드의 키 입력만으로 동작을 실행할 수 있도록 accesskey 속성값에 access 할 영어 또 ...
- Ehcache(2.9.x) - API Developer Guide, Searching a Cache
About Searching The Search API allows you to execute arbitrarily complex queries against caches. The ...
- 浅谈.NET中加密和解密的实现方法分享
这篇文章介绍了.NET中加密和解密的实现方法,有需要的朋友可以参考一下 .NET将原来独立的API和SDK合并到一个框架中,这对于程序开发人员非常有利.它将CryptoAPI改编进.NET的Syste ...
- 设置 MyEclipse 默认打开文件方式
在web开发时,我们在打开JSP页面时,MyEclipse默认使用 Vistual 的方式打开.这种方式打开JSP页面时,很慢.在实际的开发过程中我们不想使用这种默认的方式打开JSP.设置如 ...
- Ajax和JSON基础
Ajax (核心是XMLHttpRequest对象) 1.XMLHttpRequest对象: request=new XMLHttpRequest() 支持Firefox opera Safari ...
- 定制的Server-Sent Events 聊天服务器
//匿名聊天服务器 //将新的消息POST到/chat地址,或者以GET形式从通一个URL获取文本或事件流 //创建一个GET请求到"/"来返回一个简单的HTML文件,这个文件包括 ...