Pagini recente » Cod sursa (job #2325152) | Cod sursa (job #1466823) | Cod sursa (job #1158497) | Cod sursa (job #38616) | Cod sursa (job #628774)
Cod sursa(job #628774)
#include <iostream>
#include <fstream>
#include <vector>
#include <queue>
std::vector<int> photos[20];
int index[20];
bool less_list(int a, int b) {
return photos[a][index[a]] > photos[b][index[b]];
}
int main()
{
std::ifstream in("interclasari.in");
std::ofstream out("interclasari.out");
/* Read in the K lists */
int k, n, x;
in >> k;
for (int i = 0; i < k; ++i) {
in >> n;
for (int j = 0; j < n; ++j) {
in >> x;
photos[i].push_back(x);
}
}
std::priority_queue<int, std::vector<int>, bool (*)(int, int)> pq(less_list);
/* Add in the K lists to the PQ */
x = 0;
for (int i = 0; i < k; ++i) {
x += photos[i].size();
if (photos[i].size()) {
pq.push(i);
}
}
/* Print the results */
out << x << "\n";
while (!pq.empty()) {
k = pq.top();
x--;
pq.pop();
out << photos[k][index[k]++] << (x == 0 ? '\n' : ' ');
if (index[k] < photos[k].size()) {
pq.push(k);
}
}
in.close();
out.close();
return 0;
}