抄书 Copying Books UVa 714

Copying  Books

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

题目:

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 

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

题意:
把一个包含m个正整数的序列划分成k个非空的连续子序列,使得这k个子序列中和的最大值最小。
如果有多解和最大的尽量在后面。(序列中的元素位置不能发生改变)

分析:
用二分法求出最小值,
把0设为l,sum设为r,mid=(l+r)/2;
如果以mid为最小值可以划分成k个或者小于k个子序列,说明最小值比mid小或者等于mid,相反最小值比mid大。
注意值的数据类型

 1 #include
 2 #include
 3 #include
 4 using namespace std;
 5 const int maxn=510;
 6 long long a[maxn],b[maxn],sum;
 7 int m,k;
 8 int bj( long long x) 
 9 {  
10     long long sum = 0;
11     int t = k;  
12     for(int i = 0; i < m; i ++)
13     {  
14         sum += a[i];  
15         if(sum > x)
16         {  
17             i --;  
18             sum = 0;  
19             t --;  
20         }  
21         if(!t) 
22         {  
23             if(i != m - 1) return 0;  
24                else return 1;  
25         }  
26     }  
27     return 1;  
28 }  
29 void slove()
30 {
31     int i;
32     memset(b,0,sizeof(b));
33    long long l=0,r=sum,mid;
34    while(r>l)
35    {
36        mid=(l+r)/2;
37      if(bj(mid))   r=mid;
38       else  l=mid+1;
39    }
40    int sum1=0;
41      for( i=m-1;i>=0;i--)              //标记需要划分的地方,由于和大的尽量往后所以从序列后面开始划分
42      {
43       sum1+=a[i];
44           if(sum1>r)
45           {   i++;
46                sum1=0;
47                b[i]=1;
48                  k--;
49                  
50           }
51      }
52 
53     while(k >1)                 //如果子序列小于k就在还没划分的地方划分直到等于k
54     {  
55         for(i = 1;i < m; i ++ ) 
56         {  
57             if(!b[i]) 
58             {  
59                 b[i] = 1;  
60                 k --;  
61                 break;  
62             } 
63         }
64     }
65     return;
66 }
67  int main()
68  {
69   int n,i;
70   cin>>n;
71   while(n--)
72   {
73      sum=0;
74      cin>>m>>k;
75    for(i=0;i)
76    {
77     cin>>a[i];
78    sum+=a[i];
79    }
80    slove();
81    printf("%lld",a[0]);  
82     for( i = 1; i < m; i ++) 
83     {  
84         if(b[i]) printf(" /");  
85         printf(" %lld", a[i]);  
86     }  
87     printf("\n");  
88   }
89 return 0;
90 }

 






 

 

转载于:https://www.cnblogs.com/fenhong/p/4702564.html

你可能感兴趣的:(抄书 Copying Books UVa 714)