Cod sursa(job #1506814)

Utilizator BLz0rDospra Cristian BLz0r Data 20 octombrie 2015 23:48:17
Problema Interclasari Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.48 kb
#include <fstream>
#include <algorithm>
#include <queue>
#include <string>
#include <vector>
using namespace std;

#define Nmax 22

ifstream fin ( "interclasari.in" );
ofstream fout ( "interclasari.out" );

priority_queue < pair < int , int > > Heap;
vector < string > Buffer;
string aux;
int K[Nmax], Poz[Nmax];

int ReadInt ( int ind ){

    int nr = 0;

    while ( Buffer[ind][Poz[ind]] < '0' || Buffer[ind][Poz[ind]] > '9' )
        Poz[ind]++;

    while ( Buffer[ind][Poz[ind]] >= '0' && Buffer[ind][Poz[ind]] <= '9' ){
        nr = nr * 10 + ( Buffer[ind][Poz[ind]] - '0' );
        Poz[ind]++;
    }

    return nr;
}

int main(){

    int N, Sum = 0;
    fin >> N;

    for ( int i = 0; i < N; ++i ){
        fin >> K[i];
        Sum += K[i];

        if ( !K[i] ){
            Buffer.push_back(" ");
            continue;
        }

        do{
            getline( fin, aux, '\n' );
        }while ( aux.size() == 0 );

        Buffer.push_back(aux);
    }

    fout << Sum << "\n";

    for ( int i = 0; i < N; ++i ){
        if ( K[i] ){
            Heap.push ( make_pair ( -ReadInt(i), i ) );
            K[i]--;
        }
    }

    while ( !Heap.empty() ){
        int val = -Heap.top().first;
        int ind = Heap.top().second;
        Heap.pop();

        fout << val << " ";

        if ( K[ind] > 0 ){
            Heap.push( make_pair( -ReadInt(ind), ind ) );
            K[ind]--;
        }
    }

    return 0;
}