轻量级前端框架助力开发者提升项目效率与性能
742
2022-08-26
POJ 2442 Sequence (堆)
Description
Given m sequences, each contains n non-negative integer. Now we may select one number from each sequence to form a sequence with m integers. It’s clear that we may get n ^ m this kind of sequences. Then we can calculate the sum of numbers in each sequence, and get n ^ m values. What we need is the smallest n sums. Could you help us?
Input
The first line is an integer T, which shows the number of test cases, and then T test cases follow. The first line of each case contains two integers m, n (0 < m <= 100, 0 < n <= 2000). The following m lines indicate the m sequence respectively. No integer in the sequence is greater than 10000.
Output
For each test case, print a line with the smallest n sums in increasing order, which is separated by a space.
Sample Input
12 31 2 32 2 3
Sample Output
3 3 4
题意
给出m组数,每组n个,求从每一组挑选出一个数相加产生前n小的和。
思路
假设只有一组数,那么前n小的和便是这组的所有元素,如果增加一组数之后,只有原来组的前n小的和才会对新组产生贡献(因为其他数相加产生的和一定不会是结果所说的前n个里面的),于是用一个二重循环来枚举计算添加完这一组之后的前n小的和。
AC 代码
#include
发表评论
暂时没有评论,来抢沙发吧~