ZOJ-2002 Copying Books

本文深入探讨了信息技术领域的核心内容,包括前端开发、后端开发、移动开发、游戏开发、大数据开发、开发工具等多个方面。从技术原理到实际应用,提供了全面的技术指导和案例分析,帮助读者掌握信息技术领域的最新趋势和实践方法。

Copying Books

Time Limit: 2 Seconds      Memory Limit: 65536 KB

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, ... < bk-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


通过二分和贪心,对数据进行划分,而且需要从后往前划分。



#include <stdio.h>
#include <stdlib.h>
long long getbase(long long*, int, int);
int partion(long long*, long long, int, int);

int main()
{
	int N, i, j, temp;
	int k, m, books[510], flag[510];
	int mid, l, r;
	long long b[510], midbase;
	scanf("%d", &N);
	while(N--) {
		scanf("%d%d", &m, &k);
		for(i = m; i > 0; i--) {
			scanf("%d", books + i);
		}
		b[0] = 0;
		for(i = 1; i <= m; i++) {
			b[i] = b[i-1] + books[i];
		}
		midbase = getbase(b, m, k);
		j = 0;
		temp = 0;
		for(i = 1; i <= k; i++) {
			while(1) {
				if(j > m || m-j < k-i || b[j]-b[temp] > midbase) {
					flag[i] = j-1;
					temp = j-1;
					break;
				}
				j++;
			}
		}
		flag[0] = 0;
		for(i = k; i >= 1; i--) {
			printf("%d", books[flag[i]]);
			for(j=flag[i]-1;j>flag[i-1];j--) {
                printf(" %d",books[j]);
            }
            if(i!=1) {
				printf(" / ");
            }
		}
		printf("\n");
	}
	return 0;
}

long long getbase(long long *b, int m, int k)
{
	long long mid, l, r;
	l = b[m] / k;
	r = b[m];
	while(l <= r) {
		mid = (l+r)/2;
		if(partion(b, mid, k, m)) {
			l = mid+1;
		} else {
			r = mid-1;
		}
	}
	return l;
}

int partion(long long *b, long long mid, int k, int m)
{
	int i, j, temp;
	j = 0;
	temp = 0;
	for(i = 0; i < k; i++) {
		while(1) {
			if(j > m || b[j]-b[temp] > mid) {
				temp = j-1;
				break;
			}
			j++;
		}
		if(j > m) {
			return 0;
		}
	}
	return 1;
}


先展示下效果 https://pan.quark.cn/s/a4b39357ea24 遗传算法 - 简书 遗传算法的理论是根据达尔文进化论而设计出来的算法: 人类是朝着好的方向(最优解)进化,进化过程中,会自动选择优良基因,淘汰劣等基因。 遗传算法(英语:genetic algorithm (GA) )是计算数学中用于解决最佳化的搜索算法,是进化算法的一种。 进化算法最初是借鉴了进化生物学中的一些现象而发展起来的,这些现象包括遗传、突变、自然选择、杂交等。 搜索算法的共同特征为: 首先组成一组候选解 依据某些适应性条件测算这些候选解的适应度 根据适应度保留某些候选解,放弃其他候选解 对保留的候选解进行某些操作,生成新的候选解 遗传算法流程 遗传算法的一般步骤 my_fitness函数 评估每条染色体所对应个体的适应度 升序排列适应度评估值,选出 前 parent_number 个 个体作为 待选 parent 种群(适应度函数的值越小越好) 从 待选 parent 种群 中随机选择 2 个个体作为父方和母方。 抽取父母双方的染色体,进行交叉,产生 2 个子代。 (交叉概率) 对子代(parent + 生成的 child)的染色体进行变异。 (变异概率) 重复3,4,5步骤,直到新种群(parentnumber + childnumber)的产生。 循环以上步骤直至找到满意的解。 名词解释 交叉概率:两个个体进行交配的概率。 例如,交配概率为0.8,则80%的“夫妻”会生育后代。 变异概率:所有的基因中发生变异的占总体的比例。 GA函数 适应度函数 适应度函数由解决的问题决定。 举一个平方和的例子。 简单的平方和问题 求函数的最小值,其中每个变量的取值区间都是 [-1, ...
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值