Cod sursă (job #237672)

Utilizator avatar alex_alex Alexandru Magan alex_alex IP ascuns
Problemă Domino Compilator cpp | 1,64 kb
Rundă Simulare olimpiada clasele 7/8 grea Status evaluat
Dată 20 apr. 2016 17:30:43 Scor 100
#include <stdio.h>
#include <stdlib.h>
#include <deque>
#include <algorithm>

using namespace std;

struct piesa
{
    int a1, a2;
    bool rot;

    piesa(){rot = 0;}
    piesa(int _a1, int _a2, bool _rot = 0){a1 = _a1, a2 = _a2; rot = _rot;}

    bool operator== (const piesa& p){ return a1 == p.a1 && a2 == p.a2; }
    bool operator<  (const piesa& p){ return a1 < p.a1 || (a1 == p.a1 && a2 < p.a2); }
};

deque<piesa> D;

int main()
{
    FILE *fin = fopen("domino.in", "r"),
         *fout = fopen("domino.out", "w");
    int n, R, E, cR;
    int last_rotated = -1;


    for(fscanf(fin, "%d %d %d", &n, &R, &E), cR = R; n > 0; n--)
    {
        piesa p;
        int x, y;
        fscanf(fin, "%d %d", &x, &y);

        bool isRotated = false;
        if(cR == 0);
        else
        {
            if(y > x && R == 0 && E && !D.empty() && last_rotated != -1 && D[last_rotated] < piesa(y, x) && D.size() - last_rotated <= E)
                swap(x, y), D[last_rotated].rot = false, isRotated = true;
            else if(R > 0 && y > x)
                swap(x, y), R--, isRotated = true;
        }
        p = piesa(x, y, isRotated);



        while(!D.empty() && E && D.back() < p)
        {
            if(D.back().rot)
                R ++;
            D.pop_back();
            E--;
        }

        if(!D.empty() && D.back() == p && D.back().rot && p.rot == false)
            D.back().rot = false, p.rot = true;

        D.push_back(p);
        if(p.rot) last_rotated = D.size() - 1;
    }

    while(E) D.pop_back(), E--;

    for(int i = 0; i < D.size(); i++)
        fprintf(fout, "%d%d", D[i].a1, D[i].a2);

    fclose(fin);
    fclose(fout);
    return 0;
}