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每日一题】20170328

    20170327问题解析请点击今日问题下方的“[Java每日一题]20170328”查看(问题解析在公众号首发,公众号ID:weknow619) package Mar2017; public cla ...

  2. Redis的数据结构

    Redis的数据结构 redis是一种高级的key-value的存储系统,其中value支持五种数据类型. 字符串(String) 哈希(hash) 字符串列表(list) 字符串集合(set) 有序 ...

  3. Laravel篇之Laravel的安装及使用

      想搭建一个基于Lavarel和vuejs的个人网站,使用git推送到github存储,千里之行,始于足下,首先要做的用composer来下载laravel框架,并成功运行. 一.使用compose ...

  4. 前端入门8-JavaScript语法之数据类型和变量

    声明 本系列文章内容全部梳理自以下几个来源: <JavaScript权威指南> MDN web docs Github:smyhvae/web Github:goddyZhao/Trans ...

  5. 小tips:JS之浅拷贝与深拷贝

    浅拷贝: function extendCopy(p) { var c = {}; for (var i in p) { c[i] = p[i]; } return c; } 深拷贝: functio ...

  6. Netty实现一个简单聊天系统(点对点及服务端推送)

    Netty是一个基于NIO,异步的,事件驱动的网络通信框架.由于使用Java提供 的NIO包中的API开发网络服务器代码量大,复杂,难保证稳定性.netty这类的网络框架应运而生.通过使用netty框 ...

  7. RabbitMQ 消费消息

    1, 创建一个 springboot 项目, 导入依赖(和生产者一致) 2, application.properties (基础配置和生产者一致, 消费者需要再额外配置一些) # rabbitmq ...

  8. 数据库连接池(基于MySQL数据库)

    使用JDBC是怎么保证数据库客户端和数据库服务端进行连接的? 通过代码: conn=DriverManager.getConnection(url, username, password); JDBC ...

  9. Android RecycleView多种布局实现(工厂模式)

    RecycleView是个很常用的控件,很多APP中都可以看到它的身影,同时它也是个很难用的控件,主要就难在多种布局的实现. 在<第一行代码—Android>这本书里边有个RecycleV ...

  10. onmouseover和onmouseenter区别

    onmouseover和onmouseenter都是鼠标进入时触发,onmouseover在所选元素的子元素间切换的时候也触发! <!doctype html><html lang= ...