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输出即可。

  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4. typedef long long LL;
  5.  
  6. const int maxn = + ;
  7. const LL INF = 1e15;
  8.  
  9. int n, m;
  10. LL num[maxn];
  11.  
  12. int Check(LL x) {
  13. LL tmp = ;
  14. int cnt = ;
  15. for (int i = ; i < n; i++) {
  16. if (tmp + num[i] <= x) tmp += num[i];
  17. else tmp = num[i], cnt++;
  18. }
  19.  
  20. return cnt;
  21. }
  22.  
  23. bool should_put[maxn];
  24.  
  25. void output(LL x) {
  26. memset(should_put, false, sizeof(should_put));
  27. LL tmp = ;
  28. int i, cnt = ;
  29. for (i = n - ; i >= ; i--) {
  30. if (tmp + num[i] <= x) tmp += num[i];
  31. else {
  32. should_put[i] = true;
  33. tmp = num[i];
  34. cnt++;
  35. }
  36. if (i == m - cnt) break;
  37. }
  38.  
  39. if (i != -) {
  40. for (int j = ; j < i; j++) {
  41. should_put[j] = true;
  42. }
  43. }
  44. for (int i = ; i < n - ; i++) {
  45. printf("%lld ", num[i]);
  46. if (should_put[i]) printf("/ ");
  47. }
  48. printf("%lld\n", num[n - ]);
  49. }
  50.  
  51. int main()
  52. {
  53. //freopen("input.txt", "r", stdin);
  54. //freopen("output.txt", "w", stdout);
  55. int iCase;
  56. scanf("%d", &iCase);
  57. while (iCase--) {
  58. scanf("%d%d", &n, &m);
  59. LL sum = , Max = -INF;
  60. for (int i = ; i < n; i++) {
  61. scanf("%lld", &num[i]);
  62. sum += num[i];
  63. Max = Max > num[i] ? Max : num[i];
  64. }
  65. LL ans = ;
  66. LL l = Max, r = sum;
  67. while (l <= r) {
  68. LL mid = (l + r) / ;
  69. if (Check(mid) <= m) {
  70. ans = mid;
  71. r = mid - ;
  72. }
  73. else l = mid + ;
  74. }
  75.  
  76. //printf("%lld\n", l);
  77.  
  78. output(l);
  79. }
  80. return ;
  81. }

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. Maven(八)Eclipse创建Web项目(复杂方式)

    1. 生成标准的Web工程结构 2. 勾选结尾为webapp的包 3. 生成的文件结构如下 3.1 生成的目录结构若存在错误,缺少servlet.api 3.1.1 添加步骤如下 4.生成后存在的缺点 ...

  2. Bean实例化的三种方式

    1. 构造器实例化 spring容器通过bean对应的默认的构造函数来实例化bean. 2. 静态工厂方式实例化 首先创建一个静态工厂类,在类中定义一个静态方法创建实例. 静态工厂类及静态方法: pu ...

  3. spring-framework-中文文档一:IoC容器、介绍Spring IoC容器和bean

    5. IoC容器 5.1介绍Spring IoC容器和bean 5.2容器概述 本章介绍Spring Framework实现控制反转(IoC)[1]原理.IoC也被称为依赖注入(DI).它是一个过程, ...

  4. Tomcat启动时项目重复加载,导致资源初始化两次!

    一.现象: 每次启动Tomcat 的时候,工程会被加载两次 二.原因: 在tomcat/conf/server.xml配置虚拟目录引起,如下配置: 我们在Host标签里配置了appBase=" ...

  5. [HTML/CSS]三角形

    CSS盒子模型 当我们把padding和width,height全部设置为0,border设为一个较大的像素时 即:我们需要什么方向的三角形,只需把其余的三角形背景色设置为transparent:

  6. 获取请求的ip工具类

    package com.example.util; import javax.servlet.http.HttpServletRequest; /** * get remote msg * 获取访问的 ...

  7. application.properties多环境配置文件、jar包外部配置文件、配置项加密、程序中配置使用

    一.简介 spring boot项目application.properties文件存放及使用介绍 二.方法一多环境配置文件 我们一般都会有多个应用环境,开发环境.测试环境.生产环境,各个环境的配置会 ...

  8. Linux扩展分区记录

    Vmware中新建的Linux虚拟机,数据盘规划太小,数据量超出磁盘大小,本文介绍如何快速扩充空间 参考:https://blog.csdn.net/lyd135364/article/details ...

  9. Python 标准类库-Windows特殊服务之msvcrt

    标准类库-Windows特殊服务之msvcrt   by:授客 QQ:1033553122 广告:出售自研自动化小平台(无需编码也可用),有需要请联系 测试环境 win7 64位 Python 3.4 ...

  10. 排序算法----快速排序java

    快速排序是对冒泡排序的一种改进,平均时间复杂度是O(nlogn) import java.util.Arrays; import java.util.Scanner; public class tes ...