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. yii生成Model出错:yii-gii-generators-model-Generator.json No such file or dictory

    讲runtime 这个文件夹添加权限 chmod o+w runtime

  2. SPSS分析技术:无序多元Logistic回归模型;美国总统大选的预测历史及预测模型

    SPSS分析技术:无序多元Logistic回归模型:美国总统大选的预测历史及预测模型 在介绍有序多元Logistic回归分析的理论基础时,介绍过该模型公式有一个非常重要的假设,就是自变量对因变量多个类 ...

  3. API管理的五大规则

    http://www.csdn.net/article/2012-12-18/2812929-5-Rules-For-API-Management 1. 设计 开发人员使用API访问各种不同的类,并且 ...

  4. oracle-Immediate

    从shutdown immediate命令发布起,禁止建立任何新的oracle连接 未提交的事务被回退.因此,处于一个事务中间的用户将失去所有未提交的劳动成果. oracle不等待客户断开连接.任何未 ...

  5. docker 与host互传文件

    docker 的cp命令可以从容器往外复制,也可以从本机复制的容器. docker cp 文件路径 容器id:/容器目录 docker help cp Usage:    docker cp [OPT ...

  6. Oracle函数——MINUS

    解释 “minus”直接翻译为中文是“减”的意思,在Oracle中也是用来做减法操作的,只不过它不是传统意义上对数字的减法,而是对查询结果集的减法.A minus B就意味着将结果集A去除结果集B中所 ...

  7. win10下Anaconda3配置环境变量

    有时候在win10安装好Anaconda3后,使用conda命令时依然会出现: C:\Users\dell\PycharmProjects\pytorch>conda list 'conda' ...

  8. Java 并发工具箱之concurrent包

    概述 java.util.concurrent 包是专为 Java并发编程而设计的包.包下的所有类可以分为如下几大类: locks部分:显式锁(互斥锁和速写锁)相关: atomic部分:原子变量类相关 ...

  9. 笔记:VSCODE 在 WSL 开发时不显示代码差异问题

    笔记:VSCODE 在 WSL 开发时不显示代码差异问题 这个好像和 VSCODE 关系不大,主要是因为 WSL 里使用了软链接接,导致无法显示差异. 因为毕竟是软链接,所以在系统文件中会导致无法识别 ...

  10. Flutter SDK path为空导致工程打开后不显示iOS模拟器的问题

    说明下问题场景,面向git编程时下载了个开源的Flutter项目 Mac系统下AndroidStudio打开工程后,发现顶部不展示iPhone模拟器 根据ide浅黄色提示提示,判断是FlutterSD ...