抄书(UVa714)

Description

Download as PDF
 

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 calledscribers. 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

 

 题意:要抄N本书,编号为1,2,3...N, 每本书有1<=x<=10000000页, 把这些书分配给K个抄写员,要求分配给某个抄写员的那些书的编号必须是连续的。每个抄写员的速度是相同的,求所有书抄完所用的最少时间的分配方案。如果有多种分配方案,尽可能的往前面划分

解题思路:

       1.要求最少时间,取决于抄书最多的人。所以只有抄书最多的人,抄的尽可能少,时间才最少。所以这题就变成了使最大值尽量小的问题了。

下面考虑下一个问题,哪个值是最大序列的最小值呢?我们不妨考虑下最大序列的范围,不难想到它是   最小页数min——整个序列之和sum 。我们要从中找到他的最大序列的最小值。因为每本书有有1<=x<=10000000页,所以sum绝逼会大于int,查找这个范围也是巨大的。为了不超时,所以就用二分法.......(其实为什么用2分法,我也不知道,自己想的一个牵强的理由。反正书上是说用二分法....)

     2. 然后就是用二分法缩小范围直到找到那个最大序列的最小值,这里用x表示.....   肿么找x呢!你可以用二分法,先求x范围的中点,然后从序列a[0]开始求和,如果加到第i个数

发现它大于中点,说明x在后半段,变左端点,反之,说明在在前半段,变右端点。最后找到x。

    3. 最后就简单了,只需要标记,然后输出就好了....

这里给两个代码,第一个是答案代码,第二个是我测试加的一些输出.....  也许对理解有些帮助........

 

1.正确代码:

 

 1 #include <stdio.h>
 2 #include <string.h>
 3 #define LL long long
 4 int min=0,m,k,a[505],ans[505];
 5 LL sum=0;
 6 bool juge(LL x)
 7 {
 8     LL sum2=0;
 9     int t=k;
10     for(int i=0;i<m;i++){
11         sum2+=a[i];
12         if(sum2>x){
13             t--;    //记录划得次数
14             i--;
15             sum2=0;
16         }
17         if(!t){    // 如果划完了
18             if(i!=m-1) return false;  //划完了,但是没有划到最后一个,说明最大序列的最小值比现在的x大.....
19             else return true;
20         }
21     }
22     return true;
23 }
24 
25 void zhaox()
26 {
27     memset(ans,0,sizeof(ans));
28     LL l=min,r=sum,mid;
29     while(l<r){       //当左端点等于右端点就确定了一个数x
30         mid=(r+l)/2;    //中点
31         if(juge(mid))   //判断是在中点的哪边
32             r=mid;
33         else
34             l=mid+1;
35     }
36     LL sum3=0;
37     for(int i=m-1;i>=0;i--){
38         sum3+=a[i];
39         if(sum3>r){
40             sum3=0;
41             k--;
42             ans[++i]=1;   //标记
43         }
44     }
45    
46     while(k>1){     //如果没有划完,接着划,这里按照题意,需要使前面的尽可能小,所以从前面开始划..(从1开始是因为输出的关系吧,我猜的,还不是很明白...)
47         for(int i=1;i<m;i++){   
48             if(!ans[i]) {
49                 ans[i]=1;    //标记
50                 k--;
51                 break;
52             }
53         }
54     }
55     //print();
56     printf("%d",a[0]);
57     for(int i=1;i<m;i++){
58         if(ans[i]) printf(" /");
59         printf(" %d",a[i]);
60     }
61     printf("\n");
62 }
63 int main()
64 {
65     int T;
66     scanf("%d",&T);
67     while(T--){
68         scanf("%d%d",&m,&k);
69         for(int i=0;i<m;i++){
70             scanf("%d",&a[i]);
71             if(min>a[i]) min=a[i];  //计算范围
72             sum+=a[i];            //计算范围
73         }
74         zhaox();    // 找x的函数加上输出的部分
75     }
76     return 0;
77 }


 

 

实验代码:

 1 #include <stdio.h>
 2 #include <string.h>
 3 #define LL long long
 4 int min=0,m,k,a[505],ans[505];
 5 LL sum=0,j=1;
 6 bool juge(LL x)
 7 {
 8     printf("\n第%d次\n",j++);
 9     LL sum2=0;
10     int t=k;
11     for(int i=0;i<m;i++){
12         sum2+=a[i];
13         if(sum2>x){
14             sum2=0;
15             t--;
16             i--;
17             printf("%d ",a[i]);
18         }
19 
20         if(!t){
21             if(i!=m-1) {printf("\n%d\n",0); printf("%d %d\n",a[i],a[m-1]); return false;}
22             else {printf("\n%d\n",1); return true;}
23         }
24     }
25     printf("\n%d\n",1);
26     return true;
27 }
28 
29 void zhaox()
30 {
31     memset(ans,0,sizeof(ans));
32     LL l=min,r=sum,mid;
33     while(l<r){
34         mid=(r+l)/2;
35         printf("\nx=%d\n",r);
36         if(juge(mid))
37             r=mid;
38         else
39             l=mid+1;
40     }
41     LL sum3=0;
42     for(int i=m-1;i>=0;i--){
43         sum3+=a[i];
44         if(sum3>r){
45             sum3=0;
46             k--;
47             ans[++i]=1;
48         }
49     }
50     while(k>1){
51         for(int i=1;i<m;i++){
52             if(!ans[i]) {
53                 ans[i]=1;
54                 k--;
55                 break;
56             }
57         }
58     }
59     //print();
60     printf("%d",a[0]);
61     for(int i=1;i<m;i++){
62         if(ans[i]) printf(" /");
63         printf(" %d",a[i]);
64     }
65     printf("\n");
66 }
67 int main()
68 {
69     int T;
70     scanf("%d",&T);
71     while(T--){
72         scanf("%d%d",&m,&k);
73         for(int i=0;i<m;i++){
74             scanf("%d",&a[i]);
75             if(min>a[i]) min=a[i];
76             sum+=a[i];
77         }
78         zhaox();
79     }
80     return 0;
81 }

 

 

 

 

posted @ 2015-08-04 20:41  果冻0_0  阅读(434)  评论(0编辑  收藏  举报