Uva--714(二分,贪心)

2014-07-23 21:03:14

  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, \dots, m$) that may have different number of pages ( $p_1, p_2, \dots, p_m$) and you want to make one copy of each of them. Your task is to divide these books among k scribes, $k \le 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 = b_0 <
b_1 < b_2, \dots < b_{k-1} \le b_k = 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 \le k \le m \le 500$. At the second line, there are integers $p_1, p_2, \dots p_m$ 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 $p_1, p_2, \dots p_m$ 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

思路:比较经典的二分,这里用了倒序输入的小技巧(虽然后面貌似看起来弄巧成拙了?TAT),二分下界为各个数的最大值,上界为各个数之和。
 1 /*************************************************************************
 2     > File Name: Uva714.cpp
 3     > Author: Nature
 4     > Mail: 564374850@qq.com
 5     > Created Time: Wed 23 Jul 2014 03:38:31 PM CST
 6 ************************************************************************/
 7 
 8 #include <cstdio>
 9 #include <cstring>
10 #include <cstdlib>
11 #include <cmath>
12 #include <iostream>
13 #include <algorithm>
14 using namespace std;
15 
16 long long p[505],sum,N,m,k,tmax;
17 
18 bool Judge(long long x){
19     long long num = 0,tsum = 0;
20     for(int i = 1; i <= m; ++i){
21         if(tsum + p[i] > x){
22             ++num;
23             tsum = 0;
24         }
25         tsum += p[i];
26     }
27     if(num > k - 1)
28         return false;
29     return true;
30 }
31 
32 int main(){
33     scanf("%lld",&N);
34     while(N--){
35         scanf("%lld %lld",&m,&k);
36         tmax = 0;
37         sum = 0;
38         for(int i = 1; i <= m; ++i){
39             scanf("%lld",&p[m - i + 1]);
40             tmax = max(tmax,p[m - i + 1]);
41             sum += p[m - i + 1];
42         }
43         long long  l = tmax;
44         long long r = sum + 1;
45         long long mid;
46         while(l < r){
47             mid = l + (r - l) / 2;
48             //printf("%lld l: %lld  r: %lld\n",mid,l,r);
49             if(Judge(mid)) r = mid;
50             else l = mid + 1;
51         }
52         long long tsum = 0;
53         int sep[505] = {0},num = 0;
54         for(int i = 1; i <= m; ++i){
55             if(tsum + p[i] > r){
56                 sep[i] = 1;
57                 ++num;
58                 tsum = 0;
59             }
60             tsum += p[i];
61         }
62         num = k - 1 - num;
63         for(int i = m; i >= 1; --i){
64             printf("%d",p[i]);
65             if(i != 1) printf(" ");
66             if(sep[i]) printf("/ ");
67             else if(num){
68                 printf("/ ");
69                 --num;
70             }
71         }
72         printf("\n");
73     }
74     return 0;
75 }

 


posted @ 2014-07-23 21:05  Naturain  阅读(161)  评论(0编辑  收藏  举报