D - Copying Books

D - 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 ... m) that may have different number of pages (p1, p2 ... pm) and you want to make one copy of each of them. Your task is to divide these books among k scribes, k <= 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 = b0 < b1 < b2, ... < b k-1 <= bk = 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 <= k <= m <= 500. At the second line, there are integers p1, p2, ... pm 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 p1, p2, ... pm 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 

再给定一个M大小的数字序列  要求连续的划分成K段

并要求满足每段的最大值尽可能小

同时要求  如果有多组答案输出的话, 要保证越前面划分下的数字总和越小越好

思路:

定义一个 fun C(num) : 在这个num大小下作为阈值划分, 使得划分出的数量小于等于K

通过二分搜索所有可能的num  取出满足条件的最小的num。

即这个就是每个划分小数字总和中最大值的最小值,即上确界

最后得到划分后,通过贪心输出,使得前面的数字划分的数字总和尽可能小

code  (AC):

#include <iostream>
#include<queue>
#include<stdio.h>
#include<string.h>
using namespace std;
 
int N;
const int MAXN = 505;
int page[MAXN];
int M,K;
const int INF = 0x3f3f3f3f;

int out[MAXN];
int MAX;
bool C(int num){
	int sta = 0;
	int count =0;
	//必须要保证划分段要大于最大的一个数 
	if( num < MAX) return false;
	//开始枚举段数,看看是否符合 
	for(int i=0;i<M;i++){
		
		if(sta+page[i]>num) {
			count++;
			sta =0;
		}
		sta+=page[i];
	}
	if(count+1 <=K) return true;
	return false;
}

//贪心输出
//从后往前,使得后面的输出在满足 tempk<=i 
// page[i]+sta<=ub 的同时 尽量多输出 
void output(int ub){
	int tempk = K;
	int sta =0;
	
	memset(out,0,sizeof(out));
	tempk--;
	out[tempk]++;
	sta = page[M-1];
	for(int i=M-2;i>=0;i--){
		if(tempk<=i && page[i]+sta<=ub){
			out[tempk]++;
		}else{
			tempk--;
			out[tempk]++;
			sta =0;
		}
		sta +=page[i];
	}
	
//	printf("%d",page[0]);
	int q = 0;
	printf("%d",page[q++]);
	for(int z=1;z<out[tempk];z++){
		if(q>=M) return;
		printf(" %d",page[q]);
		q++;
	}
	if (tempk != K-1)
		printf(" /");
	for(int i=tempk+1;i<K;i++){
		for(int z=0;z<out[i];z++){
			if(q>=M) return;
			printf(" %d",page[q]);
			q++;
		}
		if (i != K-1)
			printf(" /");
	}
	printf("\n");
} 

 
int main() {
	
	scanf("%d",&N);
	
	while(N--){
		scanf("%d%d",&M,&K);	
		MAX = -1;	
		for(int i=0;i<M;i++){
			scanf("%d",&page[i]);
			if(MAX< page[i])
				MAX = page[i];
		}
		
		int lb = -1;  int ub = INF;
		//使用二分法进行确定,可以知道无穷大是绝对符合的
		//(lb,ub]  左闭右开,原因是由于 INF是肯定能够满足的 
		//所以最后二分的结果也是取ub 
		while(ub-lb>1){
			int mid = (ub+lb)/2;
			if(C(mid)) ub = mid;
			else lb = mid;
		}
		output(ub);
	}
 
	return 0;
}

猜你喜欢

转载自blog.csdn.net/Willen_/article/details/87864232