抄书(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个人复制(抄写),每一个人的抄写速度都一样,一本书不允许给两个(或以上)的人抄写,分给每一个人的书,必须是连续的,比 如不能把第一.第三. ...
随机推荐
- Linux逻辑卷创建
1.创建PV物理卷 [root@localhost ~]# pvcreate /dev/sda4 /dev/sda5 /dev/sda6 /dev/sda7 Physical volume “/ ...
- 关于location.href几种用法的区别
常见的几种开发形式: self.location.href; window.location.href; this.location.href; location.href; parent.locat ...
- [需再总结]SSH整合代码生成器
package cn.itcast.invoice.util.generator; import java.io.BufferedWriter; import java.io.File; import ...
- css3 calc():css简单的数学运算-加减乘除
css3 calc():css简单的数学运算–加减乘除 多好的东西啊,不用js,一个css就解决了. .box{ border:1px solid #ddd; width:calc(100% - 10 ...
- Unity Rigidbody 刚体中的Angular Drag和Freeze Position/Rotation
Rigidbody中 Angular Drag (角阻力):同样指的是空气阻力,只不过是用来阻碍物体旋转的.如果设置成无限的话,物体会立即停止旋转.如果设置成0,物体在上升过程中,会发生侧翻旋转. ...
- 利用ExpandableListView和gridview 显示可展开折叠菜单导航
这篇随身笔带来的是结合聚合数据“菜谱大全”做的一个菜谱可折叠一级+二级列表. 先发来一些截图一睹为快吧. ExpandableListView 可用于折叠型菜单列表,其布局主要通过getGroupVi ...
- SQL Server 编程入门经典(3)之T-SQL基本语句
本章内容简介: 如何从数据库检索数据(SELECT) 如何向表中插入数据(INSERT) 如何适当更新数据(UPDATE) 如何删除表中数据(DELETE) 3.1 基本SELECT语句 如果你在此 ...
- Android之日历触屏测试
结构: 查看运行效果点这里 DiaryTest.apk下载 BaseCalendar: package com.cdp.Activity; import java.util.Calendar; imp ...
- SQL_转换格式的函数—CAST()和CONVERT()
将一种数据类型的表达式显式转换为另一种数据类型的表达式.CAST 和 CONVERT 提供相似的功能. cast SELECT CAST('12.5' AS int) --在将 varchar 值 ' ...
- 第一章、C#委托和事件(Delegate、Event、EventHandler、EventArgs)
第一章.C#委托和事件(Delegate.Event.EventHandler.EventArgs) 分类: 学习笔记-C#网络编程2012-12-08 14:10 7417人阅读 评论(3) 收藏 ...