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/459657bcfd45 的源码改编 Classic-ML-Methods-Algo 引言 建立这个项目,是为了梳理和总结传统机器学习(Machine Learning)方法(methods)或者算法(algo),和各位同仁相互学习交流. 现在的深度学习本质上来自于传统的神经网络模型,很大程度上是传统机器学习的延续,同时也在不少时候需要结合传统方法来实现. 任何机器学习方法基本的流程结构都是通用的;使用的评价方法也基本通用;使用的一些数学知识也是通用的. 本文在梳理传统机器学习方法算法的同时也会顺便补充这些流程,数学上的知识以供参考. 机器学习 机器学习是人工智能(Artificial Intelligence)的一个分支,也是实现人工智能最重要的手段.区别于传统的基于规则(rule-based)的算法,机器学习可以从数据中获取知识,从而实现规定的任务[Ian Goodfellow and Yoshua Bengio and Aaron Courville的Deep Learning].这些知识可以分为四种: 总结(summarization) 预测(prediction) 估计(estimation) 假想验证(hypothesis testing) 机器学习主要关心的是预测[Varian在Big Data : New Tricks for Econometrics],预测的可以是连续性的输出变量,分类,聚类或者物品之间的有趣关联. 机器学习分类 根据数据配置(setting,是否有标签,可以是连续的也可以是离散的)和任务目标,我们可以将机器学习方法分为四种: 无监督(unsupervised) 训练数据没有给定...
本系统采用微信小程序作为前端交互界面,结合Spring Boot与Vue.js框架实现后端服务及管理后台的构建,形成一套完整的电子商务解决方案。该系统架构支持单一商户独立运营,亦兼容多商户入驻的平台模式,具备高度的灵活性与扩展性。 在技术实现上,后端以Java语言为核心,依托Spring Boot框架提供稳定的业务逻辑处理与数据接口服务;管理后台采用Vue.js进行开发,实现了直观高效的操作界面;前端微信小程序则为用户提供了便捷的移动端购物体验。整套系统各模块间紧密协作,功能链路完整闭环,已通过严格测试与优化,符合商业应用的标准要求。 系统设计注重业务场景的全面覆盖,不仅包含商品展示、交易流程、订单处理等核心电商功能,还集成了会员管理、营销工具、数据统计等辅助模块,能够满足不同规模商户的日常运营需求。其多店铺支持机制允许平台方对入驻商户进行统一管理,同时保障各店铺在品牌展示、商品销售及客户服务方面的独立运作空间。 该解决方案强调代码结构的规范性与可维护性,遵循企业级开发标准,确保了系统的长期稳定运行与后续功能迭代的可行性。整体而言,这是一套技术选型成熟、架构清晰、功能完备且可直接投入商用的电商平台系统。 资源来源于网络分享,仅用于学习交流使用,请勿用于商业,如有侵权请联系我删除!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值