UVA - 714 Copying Books
2015-11-12 10:55
381 查看
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 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
) 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 andbi. 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.
integers m and k,
. At the second line, there are integers
separated
by spaces. All these values are positive and less than 10000000.
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.
分析:考虑把输入序列划分成m个连续的子序列,使得每个序列的和不超过x,问题即转化为求x的最小值;设输入最大值为maxd,总和为tot,则x在[maxd, tot],可以二分找x
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
) 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 andbi. 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 twointegers 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 successiondivided
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
分析:考虑把输入序列划分成m个连续的子序列,使得每个序列的和不超过x,问题即转化为求x的最小值;设输入最大值为maxd,总和为tot,则x在[maxd, tot],可以二分找x
#include <cstdio> #include <cstring> #include <algorithm> using namespace std; typedef long long LL; const int maxn = 510; int m, k, d[maxn], last[maxn]; int solve(LL M) { int ans = 1; LL sum = 0; for(int i=0; i<m; i++) { if(sum + d[i] <= M) sum += d[i]; else { sum = d[i]; ans++; } } return ans; } void print(LL M) { LL sum = 0; int rest = k; memset(last, 0, sizeof(last)); for(int i=m-1; i>=0; i--) { if(sum + d[i] > M || i+1 < rest) { sum = d[i]; rest--; last[i] = 1; } else sum += d[i]; } for(int i=0; i<m-1; i++) { printf("%d ", d[i]); if(last[i]) printf("/ "); } printf("%d\n", d[m-1]); } int main() { int T; scanf("%d", &T); while(T--) { scanf("%d%d", &m, &k); LL tot = 0; int maxd = 0; for(int i=0; i<m; i++) { scanf("%d", &d[i]); tot += d[i]; maxd = max(maxd, d[i]); } LL L = maxd, R = tot; while(L < R) { LL M = L + (R-L)/2; if(solve(M) <= k) R = M; else L = M + 1; } print(L); } return 0; }
相关文章推荐
- 令人尖叫的架构
- Linux 根文件系统中各个主要目录存放的内容
- docker下如何安装和使用gogs
- 阿里云系列——3.网站备案初步核审(详细步骤)---2015-11.12
- Linux du命令查看文件夹大小并按降序排列
- Linux 压缩与解压缩命令
- opencv中Mat与IplImage,CVMat类型之间转换
- VirtualBox下安装linux虚拟机固定host-only的ip不可用
- Optical zoom & smartphones – the next big thing?
- 哪种监控工具才是运维人的最爱?
- (总结)Nginx配置文件nginx.conf中文详解
- SSH免密码登陆远程服务器配置
- 网站优化:浏览器缓存控制简介及配置策略
- opencv中VideoCapture和cvCapture有什么区别?
- linux命令:tail 命令
- Linux 命令每天必学(34)之du命令
- php linux 安装 phalcon扩展
- centos 升级python2.6 到python3.3
- Region.Op与Path.FillType
- Linux下SVN客户端rapidsvn