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.
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,
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.
Output
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.
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

解题思路:
  本题的优化目标是使最大连续子序列的和最小,并且在最大子序列和相同的情况下s1、s2...尽量小。那么我们可以从右边开始,尽量向左划分,当目前剩余书本数等于剩余的人数时,剩余每本书的分配策略只能是每人一本。

代码如下:

 #include <iostream>
#include <cstring>
#include <vector>
#include <cstdio>
#include <algorithm>
using namespace std;
#define maxm 500+5
typedef long long LL;
int m,k;
int p[maxm];
vector<int> s;
int ans[maxm];
LL M; bool judge(LL x){ s.clear();
bool flag=true;
int cnt=k;
for(int i = m;i>;){
LL sum = ;
while(i > && sum + p[i] <= x){
if(i + == cnt) break;
sum += p[i--];
}
s.push_back(i);
cnt--;
if(s.size()>k){
flag = false;
break;
}
}
if(flag){
int j=;
for(int i = s.size() - ;i >= ;i--){
ans[j++] = s[i];
}
return true;
}
else return false;
}
int main(int argc, const char * argv[]) {
freopen("/Users/hujiacheng/Desktop/input.txt", "r", stdin);
int N;
scanf("%d",&N);
while(N--){
M=;
memset(ans, , sizeof ans);
scanf("%d%d",&m,&k);
for(int i=;i<=m;i++){
scanf("%d",&p[i]);
M += p[i];
}
LL l=,r=M;
LL mid=(l+r)/;
while(l<r){
if(judge(mid)){
r=mid;
mid=(l+r)/; }
else {
l=mid+;
mid=(l+r)/;
}
}
int j=;
for(int i=;i<=m;i++){
if(i!=) cout<<" ";
cout<<p[i];
if(i==ans[j]){
cout<<" /";
j++;
}
}
cout<<endl;
}
return ;
}

714 - Copying Books——[贪心、二分查找]的更多相关文章

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

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

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

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

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

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

  4. 贪心/二分查找 BestCoder Round #43 1002 pog loves szh II

    题目传送门 /* 贪心/二分查找:首先对ai%=p,然后sort,这样的话就有序能使用二分查找.贪心的思想是每次找到一个aj使得和为p-1(如果有的话) 当然有可能两个数和超过p,那么an的值最优,每 ...

  5. Codeforces Round #768 (Div. 2) D. Range and Partition // 思维 + 贪心 + 二分查找

    The link to problem:Problem - D - Codeforces   D. Range and Partition  time limit per test: 2 second ...

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

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

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

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

  8. UVa 714 Copying Books(二分)

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

  9. UVA 714 Copying Books 二分

    题目链接: 题目 Copying Books Time limit: 3.000 seconds 问题描述 Before the invention of book-printing, it was ...

随机推荐

  1. 【JZOJ4919】【NOIP2017提高组模拟12.10】神炎皇

    题目描述 神炎皇乌利亚很喜欢数对,他想找到神奇的数对. 对于一个整数对(a,b),若满足a+b<=n且a+b是ab的因子,则成为神奇的数对.请问这样的数对共有多少呢? 数据范围 对于100%的数 ...

  2. MSSQL→ 03:数据库操作

    一.数据库的操作 1.1.新增 使用SSMS图形界面创建数据库 在SQL Server 2008 中,通过SQL Server Management Studio 创建数据库 使用Transact-S ...

  3. java方法重写规则 重载

    方法的重写规则 参数列表必须完全与被重写方法的相同: 返回类型必须完全与被重写方法的返回类型相同: 访问权限不能比父类中被重写的方法的访问权限更低.例如:如果父类的一个方法被声明为public,那么在 ...

  4. python 对象的封装性

  5. js赋值符号“=”的小例子

    var obj1={x:5}; var obj2=obj1; obj1.a=obj1={x:6}; console.log(obj1.a); console.log(obj2.a); 为什么obj1. ...

  6. nodeJs学习-10 模板引擎 ejs语法案例

    ejs语法案例 <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <t ...

  7. python世界里的局部变量和全局变量: 潜规则太重要了!!!

    python世界里的局部变量和全局变量: 潜规则太重要了!!! 先上代码: def fun(): def test_global(): ''' 内层和外层都需要声明为global, 才能彻底打通变量名 ...

  8. 【NS2】使用SourceInsight阅读NS源代码全攻略(转载)

    NS的源码底层是C++,采用了C++/Tcl分裂对象模型,架构完善,堪称OOP编程的典范.但是NS源码体系庞大,源文件有2千多个,阅读起来不是特别方便,我推荐使用SourceInsight3.5.具体 ...

  9. 在SAE上使用Flask插件

    因为我之前学习的时候使用的是虚拟环境,下载的所有需要用到的插件都在flask这个文件夹里面,SAE上Flask的版本和我本地用的版本对不上,导致有时候import都不对,于是我就把本地的环境直接放到S ...

  10. 2018-2-13-win10-uwp-如何让WebView标识win10手机

    title author date CreateTime categories win10 uwp 如何让WebView标识win10手机 lindexi 2018-2-13 17:23:3 +080 ...