714 - Copying Books——[贪心、二分查找]
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——[贪心、二分查找]的更多相关文章
- uva 714 - Copying Books(贪心 最大值最小化 二分)
题目描写叙述开头一大堆屁话,我还细致看了半天..事实上就最后2句管用.意思就是给出n本书然后要分成k份,每份总页数的最大值要最小.问你分配方案,假设最小值同样情况下有多种分配方案,输出前面份数小的,就 ...
- UVA 714 Copying Books 抄书 (二分)
题意:把一个包含m个正整数的序列划分成k个非空的连续子序列.使得所有连续子序列的序列和Si的最大值尽量小. 二分,每次判断一下当前的值是否满足条件,然后修改区间.注意初始区间的范围,L应该为所有正整数 ...
- uva 714 Copying Books(二分法求最大值最小化)
题目连接:714 - Copying Books 题目大意:将一个个数为n的序列分割成m份,要求这m份中的每份中值(该份中的元素和)最大值最小, 输出切割方式,有多种情况输出使得越前面越小的情况. 解 ...
- 贪心/二分查找 BestCoder Round #43 1002 pog loves szh II
题目传送门 /* 贪心/二分查找:首先对ai%=p,然后sort,这样的话就有序能使用二分查找.贪心的思想是每次找到一个aj使得和为p-1(如果有的话) 当然有可能两个数和超过p,那么an的值最优,每 ...
- 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 ...
- UVA 714 Copying Books 最大值最小化问题 (贪心 + 二分)
Copying Books Before the invention of book-printing, it was very hard to make a copy of a book. A ...
- UVa 714 Copying books 贪心+二分 最大值最小化
题目大意: 要抄N本书,编号为1,2,3...N, 每本书有1<=x<=10000000页, 把这些书分配给K个抄写员,要求分配给某个抄写员的那些书的编号必须是连续的.每个抄写员的速度是相 ...
- UVa 714 Copying Books(二分)
题目链接: 传送门 Copying Books Time Limit: 3000MS Memory Limit: 32768 KB Description Before the inventi ...
- UVA 714 Copying Books 二分
题目链接: 题目 Copying Books Time limit: 3.000 seconds 问题描述 Before the invention of book-printing, it was ...
随机推荐
- springboot(十九)使用actuator监控应用【转】【补】
springboot(十九)使用actuator监控应用 微服务的特点决定了功能模块的部署是分布式的,大部分功能模块都是运行在不同的机器上,彼此通过服务调用进行交互,前后台的业务流会经过很多个微服务的 ...
- 一个iOS开发者对tvOS SDK的初探
http://www.cocoachina.com/ios/20151001/13652.html 作者:Chris Wagner原文地址:tvOS SDK: An iOS Developer’s I ...
- 百度语音识别REST API用法(含JAVA代码)——不须要集成SDK的方法
版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/zpf8861/article/details/32329457 上一篇文章http://blog.c ...
- jQuery $.isNumeric vs. $.isNaN vs. isNaN
在jQuery中,有几种方式可以判断一个对象是否是数字,或者可否转换为数字. 首先,jQuery.isNaN()在最新版本中已经被移除了(1.7之后),取而代之的是 jQuery.isNumeric ...
- 运行docker容器镜像
docker容器可以理解为在盒中运行的进程. 这个盒包含了该进程运行所必须的资源,包括文件系统.系统类库.shell 环境等等. 但这个盒默认是不会运行任何程序的. 1.运行镜像之前,可以先查看本地有 ...
- Ubuntu18.10创建软件图标
解压下载包都/opt目录 创建并编辑/usr/share/applications/xxx.desktop [Desktop Entry] Encoding=UTF-8 Name=Pycharm Co ...
- 使用 Javascript 将二进制字符串转成数字
使用 Javascript 将二进制字符串转成数字 Javascript 转成 数学太简单了. 原来 parseInt 还有这样的用法. function binaryAgent(str) { str ...
- Django之内置组件
Django组件介绍 分页器的使用 Form modelForm orm cookie和session 中间件 信号 ...
- oracle函数 least(exp1,exp2,exp3,……,expn)
[功能]返回表达式列表中值最小的一个.如果表达式类型不同,会隐含转换为第一个表达式类型. [参数]exp1……n,各类型表达式 [返回]exp1类型 [示例] SELECT least(10,32,' ...
- 阿里云应用实时监控 ARMS 再升级,支持 Prometheus 开源生态
摘要: 应用实时监控服务 (ARMS) 是一款APM类的监控产品. 用户可基于 ARMS 的前端.应用.自定义监控,快速构建实时的应用性能和业务监控能力.ARMS 让所有性能问题“一屏了然”,不遗余力 ...