Problem UVA1471-Copying Books

Accept: 2669  Submit: 22797
Time Limit: 3000 mSec

Problem 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. Onceuponatime,therewasatheaterensemblethatwantedtoplayfamousAntiqueTragedies. 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. Ifthereismorethanonesolution,printtheonethatminimizestheworkassignedtothefirstscriber, 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

题解:最大化最小值,目前遇到的题不是贪心就是二分,这个题明显二分答案。输出格式需要注意一下,因为要字典序最小所以尽量让后面的数凑在一起,这样前面的数就能尽量分散。如果当前剩余分块数等于剩余数字个数,直接break输出即可。

 #include <bits/stdc++.h>

 using namespace std;
typedef long long LL; const int maxn = + ;
const LL INF = 1e15; int n, m;
LL num[maxn]; int Check(LL x) {
LL tmp = ;
int cnt = ;
for (int i = ; i < n; i++) {
if (tmp + num[i] <= x) tmp += num[i];
else tmp = num[i], cnt++;
} return cnt;
} bool should_put[maxn]; void output(LL x) {
memset(should_put, false, sizeof(should_put));
LL tmp = ;
int i, cnt = ;
for (i = n - ; i >= ; i--) {
if (tmp + num[i] <= x) tmp += num[i];
else {
should_put[i] = true;
tmp = num[i];
cnt++;
}
if (i == m - cnt) break;
} if (i != -) {
for (int j = ; j < i; j++) {
should_put[j] = true;
}
}
for (int i = ; i < n - ; i++) {
printf("%lld ", num[i]);
if (should_put[i]) printf("/ ");
}
printf("%lld\n", num[n - ]);
} int main()
{
//freopen("input.txt", "r", stdin);
//freopen("output.txt", "w", stdout);
int iCase;
scanf("%d", &iCase);
while (iCase--) {
scanf("%d%d", &n, &m);
LL sum = , Max = -INF;
for (int i = ; i < n; i++) {
scanf("%lld", &num[i]);
sum += num[i];
Max = Max > num[i] ? Max : num[i];
}
LL ans = ;
LL l = Max, r = sum;
while (l <= r) {
LL mid = (l + r) / ;
if (Check(mid) <= m) {
ans = mid;
r = mid - ;
}
else l = mid + ;
} //printf("%lld\n", l); output(l);
}
return ;
}

UVA1471-Copying Books(二分答案)的更多相关文章

  1. UVa 714 Copying Books - 二分答案

    求使最大值最小,可以想到二分答案. 然后再根据题目意思乱搞一下,按要求输出斜杠(这道题觉得就这一个地方难). Code /** * UVa * Problem#12627 * Accepted * T ...

  2. UVA 714 Copying Books 二分

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

  3. 2019杭电多校第三场hdu6606 Distribution of books(二分答案+dp+权值线段树)

    Distribution of books 题目传送门 解题思路 求最大值的最小值,可以想到用二分答案. 对于二分出的每个mid,要找到是否存在前缀可以份为小于等于mid的k份.先求出这n个数的前缀和 ...

  4. ZOJ 2002 Copying Books 二分 贪心

    传送门:Zoj2002 题目大意:从左到右把一排数字k分,得到最小化最大份,如果有多组解,左边的尽量小. 思路:贪心+二分(参考青蛙过河). 方向:从右向左. 注意:有可能最小化时不够k分.如     ...

  5. UVa 714 Copying Books(二分)

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

  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. Copying Books

    Copying Books 给出一个长度为m的序列\(\{a_i\}\),将其划分成k个区间,求区间和的最大值的最小值对应的方案,多种方案,则按从左到右的区间长度尽可能小(也就是从左到右区间长度构成的 ...

  8. CH Round #72树洞[二分答案 DFS&&BFS]

    树洞 CH Round #72 - NOIP夏季划水赛 描述 在一片栖息地上有N棵树,每棵树下住着一只兔子,有M条路径连接这些树.更特殊地是,只有一棵树有3条或更多的路径与它相连,其它的树只有1条或2 ...

  9. [CF752E]Santa Claus and Tangerines(二分答案,dp)

    题目链接:http://codeforces.com/contest/752/problem/E 题意:给n个橘子,每个橘子a(i)片,要分给k个人,问每个人最多分多少片.每个橘子每次对半分,偶数的话 ...

随机推荐

  1. 【Java每日一题】20170119

    20170118问题解析请点击今日问题下方的“[Java每日一题]20170119”查看(问题解析在公众号首发,公众号ID:weknow619) package Jan2017; import jav ...

  2. python多线程-共享全局变量

    目录 多线程-共享全局变量 多线程-共享全局变量 列表当作实参传递到线程中 总结 多线程-共享全局变量问题 多线程开发可能遇到的问题 测试1 测试2 多线程-共享全局变量 多线程-共享全局变量 imp ...

  3. JavaScript定时器实现的原理分析

    原文链接:http://www.cnblogs.com/st-leslie/p/6082450.html 一.储备知识 在我们在项目中一般会遇见过这样的两种定时器,第一种是setTimeOut,第二种 ...

  4. hash 和pushState,replaceState

    hash 要点: 1.不会向后台发请求:#是用来指导浏览器动作的,对服务器端完全无用. 2.用来跳转到页面的指定位置:   为网页位置指定标识符,有两个方法.一是使用锚点,比如<a name=& ...

  5. Sysbench Sysbench在centos系统下的安装

    Sysbench在centos系统下的安装   by:授客 QQ:1033553122       测试环境: CentOS-7-x86_64-DVD-1503-01.iso 下载地址: http:/ ...

  6. Android为TV端助力 apk静默安装

    转载请注明出处:http://blog.csdn.net/guolin_blog/article/details/47803149 之前有很多朋友都问过我,在Android系统中怎样才能实现静默安装呢 ...

  7. Testlink1.9.17使用方法(第一章 前言)

    第一章 前言 QQ交流群:585499566 一.Testlink主要功能: 测试项目管理 测试需求管理 测试用例管理 测试计划的制定 测试用例对测试需求的覆盖管理 测试用例的执行 大量测试数据的度量 ...

  8. JS 调试中常见的报错的解决办法

    报错:Uncaught SyntaxError: Unexpected token o in JSON at position 1 at JSON.parse (<anonymous>) ...

  9. python 函数参数为*和**的作用与区别

    def function(*args):print(args) 中*的作用:表示此时参数为一个元祖. def function(**args):print(args)中**的作用:表示此时参数为一个字 ...

  10. SQL Server has encountered 1 occurrence(s) of cachestore flush for the 'Object Plans' cachestore (part of plan cache) due to some database maintenance or reconfigure operations.

    2017-11-01 09:49:44.35 spid166 SQL Server has encountered 1 occurrence(s) of cachestore flush for th ...