程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> C++入門知識 >> [貪心&&二分]uva714 Copying Books

[貪心&&二分]uva714 Copying Books

編輯:C++入門知識



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



Miguel A. Revilla
2000-02-15

題意:給出m個數字,一個數字k,把這m個數字劃分為k個區間使得這k個區間中的所有的數相加的最大值最小,k個區間中的數字必須按照原來的順序。

思路:典型的最大值最小化,這是劉汝佳的算法競賽經典入門中的講解,然後使用貪心從後往前找。

\

<喎?http://www.Bkjia.com/kf/ware/vc/" target="_blank" class="keylink">vcD4KPHByZSBjbGFzcz0="brush:java;">#include using namespace std; int arry[550],vis[550]; int m,n; int judge(long long pos,int len) { int i=0,tag=0; long long sum=0; while(len>0&&ipos) { sum=0; len--; } } if(i0;i--) { if(len>i) { vis[i]=1; len--; } else if(sum+arry[i]<=pos&&sum+arry[i]+arry[i-1]>pos) { sum=0; vis[i]=1; len--; } else { sum=sum+arry[i]; } } for(i=0;inum; while(num--) { int i; long long sum=0; cin>>m>>n; for(i=0;i>arry[i]; sum=sum+arry[i]; vis[i]=0; } long long left=1,right=sum,mid; mid=(left+right)/2; int tag=judge(mid,n); while(tag!=0) { if(tag==-1) { left=mid+1; mid=(left+right)/2; tag=judge(mid,n); } else if(tag==1) { right=mid-1; mid=(right+left)/2; tag=judge(mid,n); } } output(mid,n); //cout<

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved