题目链接:

题目

Copying Books

Time limit: 3.000 seconds

问题描述

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

1, 2, . . . , m) that may have different number of pages (p1, p2, . . . , pm) and you want to make one copy of

each of them. Your task is to divide these books among k scribes, k ≤ m. 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 0 = b0 < b1 < b2, . . . < bk−1 ≤ bk = m 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.

输入

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,

1 ≤ k ≤ m ≤ 500. At the second line, there are integers p1, p2, . . . , pm separated by spaces. All these

values are positive and less than 10000000.

输出

For each case, print exactly one line. The line must contain the input succession p1, p2, . . . pm 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.

样例

input

2

9 3

100 200 300 400 500 600 700 800 900

5 4

100 100 100 100 100

output

100 200 300 400 500 / 600 700 / 800 900

100 / 100 / 100 / 100 100

题意

给你n本书,现在让你把它们分为m堆,每一堆的书编号要连续,并且使得页数最多的那一堆的页数值最小。

题解

最大值最小,考虑二分。

二分最大堆的书页数小于等于x,然后只要贪心线性扫一遍,如果能分出少于m堆,则继续扩大答案,否则缩小。

由于多解的时候要让越前面的堆的页数越小。所以考虑从后面贪心往前面扫,因为后面的堆越大前面的就自然越小了,如果扫一遍发现分出的堆数不够,那就从前往后把没有分割的给分割开,直到分出m堆。

代码

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#define lson (o<<1)
#define rson ((o<<1)|1)
#define M L+(R-L)/2
using namespace std; typedef long long LL;
const int maxn=555;
const LL INF=(LL)5e9; int m,k;
int arr[maxn],tag[maxn];
int Ma; bool ok(LL ma){
if(ma<Ma) return false;
LL sum=0;
int cnt=0;
for(int i=0;i<m;i++){
sum+=arr[i];
if(sum>ma){
sum=arr[i];
cnt++;
}
}
cnt++;
return cnt<=k;
} int main(){
int tc;
scanf("%d",&tc);
while(tc--){
Ma=-1;
memset(tag,0,sizeof(tag));
scanf("%d%d",&m,&k);
for(int i=0;i<m;i++){
scanf("%d",&arr[i]);
Ma=max(Ma,arr[i]);
}
LL l=Ma-1,r=INF;
while(l+1<r){
LL mid=l+(r-l)/2;
if(ok(mid)) r=mid;
else l=mid;
}
LL sum=0; int cnt=1;
for(int i=m-1;i>=0;i--){
sum+=arr[i];
if(sum>r){
sum=arr[i];
cnt++;
tag[i]=1;
}
}
for(int i=0;i<m-1&&cnt<k;i++){
if(!tag[i]){
tag[i]=1; cnt++;
}
}
for(int i=0;i<m-1;i++){
printf("%d ",arr[i]);
if(tag[i]) printf("/ ");
}
printf("%d\n",arr[m-1]);
}
return 0;
}

UVA 714 Copying Books 二分的更多相关文章

  1. UVa 714 Copying Books - 二分答案

    求使最大值最小,可以想到二分答案. 然后再根据题目意思乱搞一下,按要求输出斜杠(这道题觉得就这一个地方难). Code /** * UVa * Problem#12627 * Accepted * T ...

  2. uva 714 Copying Books(二分法求最大值最小化)

    题目连接:714 - Copying Books 题目大意:将一个个数为n的序列分割成m份,要求这m份中的每份中值(该份中的元素和)最大值最小, 输出切割方式,有多种情况输出使得越前面越小的情况. 解 ...

  3. UVa 714 Copying Books(二分)

    题目链接: 传送门 Copying Books Time Limit: 3000MS     Memory Limit: 32768 KB Description Before the inventi ...

  4. UVA 714 Copying Books 最大值最小化问题 (贪心 + 二分)

      Copying Books  Before the invention of book-printing, it was very hard to make a copy of a book. A ...

  5. 【NOIP提高组2015D2T1】uva 714 copying books【二分答案】——yhx

    Before the invention of book-printing, it was very hard to make a copy of a book. All the contents h ...

  6. uva 714 - Copying Books(贪心 最大值最小化 二分)

    题目描写叙述开头一大堆屁话,我还细致看了半天..事实上就最后2句管用.意思就是给出n本书然后要分成k份,每份总页数的最大值要最小.问你分配方案,假设最小值同样情况下有多种分配方案,输出前面份数小的,就 ...

  7. UVa 714 Copying books 贪心+二分 最大值最小化

    题目大意: 要抄N本书,编号为1,2,3...N, 每本书有1<=x<=10000000页, 把这些书分配给K个抄写员,要求分配给某个抄写员的那些书的编号必须是连续的.每个抄写员的速度是相 ...

  8. UVA 714 Copying Books 抄书 (二分)

    题意:把一个包含m个正整数的序列划分成k个非空的连续子序列.使得所有连续子序列的序列和Si的最大值尽量小. 二分,每次判断一下当前的值是否满足条件,然后修改区间.注意初始区间的范围,L应该为所有正整数 ...

  9. UVA - 714 Copying Books (抄书)(二分+贪心)

    题意:把一个包含m个正整数的序列划分成k个(1<=k<=m<=500)非空的连续子序列,使得每个正整数恰好属于一个序列(所有的序列不重叠,且每个正整数都要有所属序列).设第i个序列的 ...

随机推荐

  1. Jquery AJax Post 返回值问题

    var msg=0; Validater('abc'); function Validater(Name) { var itemId = 1; $.ajax({ url: 'adminmenu/Val ...

  2. Java学习之Java的单例模式

    单例模式有一下特点: 1.单例类只能有一个实例.2.单例类必须自己自己创建自己的唯一实例.3.单例类必须给所有其他对象提供这一实例. 单例模式确保某个类只有一个实例,而且自行实例化并向整个系统提供这个 ...

  3. ajax请求简写

    <script type="text/javascript"> function changle() { $.post( "SendMail", / ...

  4. python基础:os模块中关于文件/目录常用的函数使用方法

    Python是跨平台的语言,也即是说同样的源代码在不同的操作系统不需要修改就可以同样实现 因此Python的作者就倒腾了OS模块这么一个玩意儿出来,有了OS模块,我们不需要关心什么操作系统下使用什么模 ...

  5. BeanDefinition的Resource定位

    1.以编程的方式使用DefaultListableBeanFactory时,首先定义一个Resource来定位容器使用的BeanDefiniton.这时使用的是ClassPathResource,这意 ...

  6. JavaScript开发技巧

    1.在编写js代码时,应尽量避免全局变量的使用.如果实在需要使用全局变量,则可以使用一个function来规避全局变量的使用. 2.数字解析 //1.丢弃小数部分,保留整数部分 alert( pars ...

  7. Facade模式和Mediator模式

    相同的目的:把某种策略施加到另一组对象上. Facade从上面施加策略. 其使用是明显且受限的.当策略涉及范围广泛并且可见时. 约定的关注点.都同意使用Facade而不是隐藏于其下的对象. Media ...

  8. 10款经典的web前端特效的预览及源码

    1.CSS3响应式导航菜单 今天我给大家介绍一下如何使用纯CSS来实现的一个响应式导航菜单,我们使用的是HTML5+CSS3技术,当浏览器窗口变小或者使用手机浏览器访问的时候,原本横条菜单会收缩成一个 ...

  9. ThinkPHP中的模型

    ThinkPHP中的模型 1.什么是模型(Model) 模型表示企业数据和业务规则,实际项目开发中,主要实现与数据库进行操作. 2.模型的定义规则 模型类的命名规则是除去表前缀的数据表名称,采用驼峰法 ...

  10. tar 「解/壓」 包

    1.tar: short read tar壓縮包文件發生了損壞,或者说不完整. 最近,在寫下載程序的時候,發生了這個問題.初步猜測文件寫完沒有關閉,沒有寫完.