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 ...
随机推荐
- 在Liferay 7中如何自定义一个Portlet的toolbar
哈哈,懒得自己写了,直接贴教程了,你想为那个portlet添加自定义的toolbar,就在javax.portlet.name=属性中写上它的值.教程博客:Adding Portlet URL in ...
- idea 项目热部署设置
1.引入pom.xml() <!-- 热部署(必须) --> <dependency> <groupId>org.springframework.boot</ ...
- 【JZOJ4869】【NOIP2016提高A组集训第9场11.7】平均数
题目描述 数据范围 解法 二分答案. 对于一个答案mid,要求出区间平均数小于mid的个数ans. 给所有数减去mid,那么问题转化为求出所有区间和为负数的个数. 对于一个区间[l,r],如果sum[ ...
- 【JZOJ4859】【NOIP2016提高A组集训第7场11.4】连锁店
题目描述 Dpstr开了个饮料连锁店,连锁店共有n家,出售的饮料种类相同.为了促销,Dpstr决定让每家连锁店开展赠送活动.具体来说,在第i家店,顾客可以用ai个饮料瓶兑换到bi瓶饮料和1个纪念币(注 ...
- Linux下的python安装
centos7安装python3 以及tab补全功能 1.安装python3 1.1下载python源码包 网址:https://www.python.org/downloads/release/ ...
- [转] android自定义布局中的平滑移动
无意中搜索到这篇文章,大概扫了一眼,知道是篇好文,先转载记录下来学习! 文章主要讲的是自定义view的写法心得. 转自:http://www.apkbus.com/android-48445-1-1. ...
- MyBatis动态SQL(一)
MyBatis 的强大特性之一便是它的动态 SQL.动态 SQL 元素和 JSTL 或基于类似 XML 的文本处理器相似.在 MyBatis 之前的版本中,有很多元素需要花时间了解.MyBatis 3 ...
- LeetCode86 Partition List
题目: Given a linked list and a value x, partition it such that all nodes less than x come before node ...
- day10-04_多线程常用属性方法
一.需要了解的方法 Thread实例对象的方法 # isAlive(): 判断这个线程是否是存活的 # getName(): 获取线程名 # setName(): 设置线程名 #enumerate() ...
- oracle函数 VARIANCE([distinct|all]x)
[功能]统计数据表选中行x列的方差. [参数]all表示对所有的值求方差,distinct只对不同的值求方差,默认为all 如果有参数distinct或all,需有空格与x(列)隔开. [参数]x,只 ...