Cod sursa(job #1811880)

Utilizator oldatlantianSerban Cercelescu oldatlantian Data 21 noiembrie 2016 17:48:32
Problema Interclasari Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.38 kb
// "Autismul se manifesta prin cai misterioase"
#include <bits/stdc++.h>
using namespace std;

FILE *fin[21];
int buff[21], offset[21], k[21];
char buck[21][1 << 12];

inline char nextch(int idx) {
  if(buff[idx] == 1 << 12) {
    buff[idx] = 0;
    fread(buck[idx], 1, 1 << 12, fin[idx]);
  }
  ++ offset[idx];
  return buck[idx][ buff[idx]++ ];
}

void get(int &arg, int idx) {
  char ch;
  do {
    ch = nextch(idx);
  } while(ch < '0' || ch > '9');
  arg = 0;
  do {
    arg = arg * 10 + ch - '0';
    ch = nextch(idx);
  } while(ch >= '0' && ch <= '9');
}

set<pair<int, int>> pq;

int main(void) {
  ofstream fout("interclasari.out");
  int n, t, s, src;

  s = 0;

  for(int i = 0; i < 21; ++i)
    buff[i] = 1 << 12;
  for(int i = 0; i < 21; ++i)
    fin[i] = fopen("interclasari.in", "r");

  get(n, 0);

  for(int i = 0; i < n; ++i) {
    get(k[i], i);
    s+= k[i];

    fseek(fin[i + 1], offset[i], SEEK_SET);
    offset[i + 1] = offset[i];
    for(int j = 0; j < k[i]; ++j)
      get(t, i + 1);
  }

  for(int i = 0; i < n; ++i) {
    if(k[i]) {
      -- k[i];
      get(t, i);
      pq.insert(make_pair(t, i));
    }
  }

  fout << s << '\n';
  while(!pq.empty()) {
    fout << pq.begin()->first << ' ';
    src = pq.begin()->second;
    pq.erase(pq.begin());
    if(k[src] > 0) {
      -- k[src];
      get(t, src);
      pq.insert(make_pair(t, src));
    }
  }
  fout << '\n';

  return 0;
}