Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu

 

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.

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 ) that may have different number of pages ( ) and you want to make one copy of each of them. Your task is to divide these books among k scribes, . 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 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, . At the second line, there are integers 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 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

  1. 2
  2. 9 3
  3. 100 200 300 400 500 600 700 800 900
  4. 5 4
  5. 100 100 100 100 100

Sample Output

  1. 100 200 300 400 500 / 600 700 / 800 900
  2. 100 / 100 / 100 / 100 100

Miguel A. Revilla
2000-02-15

程序分析:题目大意是要抄N本书,编号为1,2,3……N,每本书有1<=x<=10000000页,把这些书分配给K个抄写员,要求分配给某个抄写员的那些书的编号必须是连续的,每个抄写员的速度是相同的,求所有书抄完所用最少时间的分配方案。此题不但用到的二分法,同时在输出的时候也用到的贪心算法,还有一个值得注意的地方就是如果把X,Y,MID定义成int型会导致数据的溢出所以我们应该使用long long型。

程序代码:

  1. #include <cstdio>
  2. #include <cstdlib>
  3. #include <iostream>
  4. #include <cstring>
  5. using namespace std;
  6. const int MAXN = ;
  7. int a[MAXN], use[MAXN];
  8. long long low_bound, high_bound;
  9. int n, m;
  10. void init()
  11. {
  12. low_bound = -;
  13. high_bound = ;
  14. memset(use, , sizeof(use));
  15. }
  16.  
  17. int solve(int mid)
  18. {
  19. int sum = , group = ;
  20. for(int i = n-; i >= ; i--)
  21. {
  22. if(sum + a[i] > mid)
  23. {
  24. sum = a[i];
  25. group++;
  26. if(group > m) return ;
  27. }
  28. else sum += a[i];
  29. }
  30. return ;
  31. }
  32.  
  33. void print(int high_bound)
  34. {
  35. int group = , sum = ;
  36. for(int i = n-; i >= ; i--)
  37. {
  38. if(sum + a[i] > high_bound)
  39. {
  40. use[i] = ;
  41. sum = a[i];
  42. group++;
  43. }
  44. else sum += a[i];
  45. if(m-group == i+)
  46. {
  47. for(int j = ; j <= i; j++)
  48. use[j] = ;
  49. break;
  50. }
  51. }
  52. for(int z = ; z< n-; z++)
  53. {
  54. printf("%d ", a[z]);
  55. if(use[z]) printf("/ ");
  56. }
  57. printf("%d\n", a[n-]);
  58. }
  59.  
  60. int main()
  61. {
  62. int T;
  63. scanf("%d%*c", &T);
  64. while(T--)
  65. {
  66. init();
  67. scanf("%d%d", &n, &m);
  68. for(int i = ; i < n; i++)
  69. {
  70. scanf("%d", &a[i]);
  71. if(low_bound < a[i]) low_bound = a[i];
  72. high_bound += a[i];
  73. }
  74. long long x = low_bound, y = high_bound;
  75. while(x <= y)
  76. {
  77. long long mid = x+(y-x)/;
  78. if(solve(mid)) y = mid-;
  79. else x = mid+;
  80. }
  81. print(x);
  82. }
  83. return ;
  84. }

uVa 714 (二分法)的更多相关文章

  1. uva 714 Copying Books(二分法求最大值最小化)

    题目连接:714 - Copying Books 题目大意:将一个个数为n的序列分割成m份,要求这m份中的每份中值(该份中的元素和)最大值最小, 输出切割方式,有多种情况输出使得越前面越小的情况. 解 ...

  2. UVa 714 Copying Books(二分)

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

  3. 抄书 Copying Books UVa 714

    Copying  Books 题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=85904#problem/B 题目: Descri ...

  4. uva 714 - Copying Books(贪心 最大值最小化 二分)

    题目描写叙述开头一大堆屁话,我还细致看了半天..事实上就最后2句管用.意思就是给出n本书然后要分成k份,每份总页数的最大值要最小.问你分配方案,假设最小值同样情况下有多种分配方案,输出前面份数小的,就 ...

  5. UVa 714 Copying Books - 二分答案

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

  6. UVa 714 抄书(贪心+二分)

    https://vjudge.net/problem/UVA-714 题意:把一个包含m个正整数的序列划分成k个非空的连续子序列,使得每个正整数恰好属于一个序列.设第i个序列的各数之和为S(i),你的 ...

  7. UVa 714 Copying books 贪心+二分 最大值最小化

    题目大意: 要抄N本书,编号为1,2,3...N, 每本书有1<=x<=10000000页, 把这些书分配给K个抄写员,要求分配给某个抄写员的那些书的编号必须是连续的.每个抄写员的速度是相 ...

  8. 【NOIP提高组2015D2T1】uva 714 copying books【二分答案】——yhx

    Before the invention of book-printing, it was very hard to make a copy of a book. All the contents h ...

  9. UVa 714 (二分) Copying Books

    首先通过二分来确定这种最大值最小的问题. 假设每个区间的和的最大值为x,那么只要判断的时候只要贪心即可. 也就是如果和不超过x就一直往区间里放数,否则就开辟一个新的区间,这样来判断是否k个区间容得下这 ...

随机推荐

  1. BZOJ 2208: [Jsoi2010]连通数( DFS )

    n只有2000,直接DFS就可以过了... -------------------------------------------------------------------------- #in ...

  2. ThinkPHP第十八天(Widget类的使用,连贯操作where IN用法,缓存S函数使用)

    1.Widget类的使用方法: 第一步:在Action同级目录中新建Widget文件夹(独立分组需要自己建立) 第二步:根据不同功能在Widget文件夹中建立不同的Widget类,如热门文章HotWi ...

  3. php 计算多维数组中所有值的总和

    php 内置函数 array_sum() 函数返回数组中所有值的总和,只能返回一维数组的总和: 计算多维数组所有值的和就要自定义函数了: function get_sum($array) { $num ...

  4. codeforces 616E. Sum of Remainders 数学

    题目链接 给两个数n, m. 求n%1+n%2+.......+n%m的值. 首先, n%i = n-n/i*i, 那么原式转化为n*m-sigma(i:1 to m)(n/i*i). 然后我们可以发 ...

  5. OSG项目经验2<在场景中添加文字面版>

    添加文字版需要用到osg的三个名字空间:                         osgText::Text,这个类用来添加文字和设置文字的一些属性:                     ...

  6. ORACLE模拟一个数据文件坏块并使用RMAN备份来恢复

    1.创建一个实验用的表空间并在此表空间上创建表 create tablespace blocktest datafile '/u01/oradata/bys1/blocktest.dbf' size ...

  7. 基于Visual C++2013拆解世界五百强面试题--题7-链表的各种操作

    请用C实现一个链表,实现链表的查找,逆置,替换,删除,添加,清空,创建. 查找.替换和删除.添加里面都会用到遍历链表的操作,所以重点在于遍历, 链表的逆置和清空考虑到效率,我们可以用递归实现, 至于创 ...

  8. _WSAStartup@8,该符号在函数 _main 中被引用

    int WSAStartup( __in WORD wVersionRequested, __out LPWSADATA lpWSAData ); WSAStartup 格  式: int PASCA ...

  9. One Person Game(扩展欧几里德求最小步数)

    One Person Game Time Limit: 2 Seconds      Memory Limit: 65536 KB There is an interesting and simple ...

  10. MessageDigest简单介绍

    本文博客原文 參考文章:http://blog.sina.com.cn/s/blog_4f36423201000c1e.html 一.概述 java.security.MessageDigest类用于 ...