Cod sursa(job #628774)

Utilizator MarcvsHdrMihai Leonte MarcvsHdr Data 2 noiembrie 2011 02:45:52
Problema Interclasari Scor Ascuns
Compilator cpp Status done
Runda Marime 0.99 kb
#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;
}