时间限制: 1 Sec 内存限制: 128 MB
提交: 90 解决: 43
[提交] [状态] [命题人:admin]
题目描述
Princess Lucy broke her old reading lamp, and needs a new one. The castle orders a shipment of parts from the Slick Lamp Parts Company, which produces interchangable lamp pieces.
There are m types of lamp pieces, and the shipment contained multiple pieces of each type. Making a lamp requires exactly one piece of each type. The princess likes each piece with some value, and she likes a lamp as much as the sum of how much she likes each of the pieces.
You are part of the castle staff, which has gotten fed up with the princess lately. The staff needs to propose k distinct lamp combinations to the princess (two lamp combinations are considered distinct if they differ in at least one piece). They decide to propose the k combinations she will like the least. How much will the princess like the k combinations that the staff proposes?
输入
The first line of input contains a single integer T (1 ≤ T ≤ 10), the number of test cases. The first line of each test case contains two integers m (1 ≤ m ≤ 100), the number of lamp piece types and k (1 ≤ k ≤ 100), the number of lamps combinations to propose. The next m lines each describe the lamp parts of a type;
they begin with ni (2 ≤ ni ≤ 100), the number of pieces of this type, followed by ni integers vi,1 ,... , vi,ni(1 ≤ vi,j ≤ 10,000) which represent how much the princess likes each piece. It is guaranteed that k is no greater than the product of all ni ’s.
输出
For each test case, output a single line containing k integers that represent how much the princess will like the proposed lamp combinations, in nondecreasing order.
复制样例数据
2
2 2
2 1 2
2 1 3
3 10
4 1 5 3 10
3 2 3 3
5 1 3 4 6 6
样例输出
2 3
4 5 5 6 6 7 7 7 7 7
提示
In the first case, there are four lamp pieces, two of each type. The worst possible lamp has value 1 + 1 = 2,
while the second worst possible lamp has value 2 + 1 = 3.
题意:求在n个组中各选一个数相加和的前k小值
用优先队列存前k小值,选上第i个数组的前k小值就是前i-1个数组的前k小值加上第i个数组的值
代码:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 5e5 + 100;
const int inf = 1e9;
const int mod = 998244353;
priority_queue<int, vector<int>, greater<int> > q;
int a[200];
int x[200];
int main() {
int t;
cin >> t;
while (t--) {
while (!q.empty()) q.pop();
memset(x, 0, sizeof(x));
memset(a, 0, sizeof(a));
int n, k, m;
cin >> n >> k;
int v = k;
for (int i = 1; i <= n; i++) {
cin >> m;
if (i == 1) {
for (int j = 1; j <= m; j++) {
cin >> a[j];
q.push(a[j]);
}
} else {
for (int j = 1; j <= m; j++) {
cin >> a[j];
}
int j = 1;
for (; j <= v && !q.empty(); j++) {
x[j] = q.top();
q.pop();
}
while (!q.empty()) q.pop();
sort(a + 1, a + m + 1);
sort(x + 1, x + j);
for (int u = 1; u < j; u++) {
for (int o = 1; o <= m; o++) {
q.push(x[u] + a[o]);
}
}
}
}
for (int i = 1; i <= k; i++) {
if (i == 1) printf("%d", q.top());
else printf(" %d", q.top());
q.pop();
}
printf("\n");
}
return 0;
}