Cod sursă (job #324888)

Utilizator avatar sionescu1707 ionescu stefan sionescu1707 IP ascuns
Problemă Domino Compilator cpp | 1,61 kb
Rundă Arhiva de probleme Status evaluat
Dată 20 nov. 2017 18:57:26 Scor 100
#include <iostream>
#include <fstream>

using namespace std;

ifstream in("domino.in");
ofstream out("domino.out");

int n,cntRot,cntEl;

int lastSt,lastDr;
bool lastRot;
struct piesa
{
    int val;
    bool rotit;
} v[10005];

int sz;

bool canRotate(int val, int rot, int el)
{
    int cnt = sz;
    while(el && cnt >=1 && val > v[cnt].val)
    {
        if (v[cnt].rotit == 1)
        {
            rot++;
        }
        cnt--;
        el--;
    }
    return rot>0;
}

void adunare(int domVal, int isRotated)
{

    while(cntEl && sz >= 1 && domVal > v[sz].val)
    {
        if(v[sz].rotit == 1)
        {
            cntRot++;
        }
        sz--;
        cntEl--;
    }

    v[++sz].rotit = isRotated;
    v[sz].val = domVal;

    if(sz >=1 && v[sz].val == v[sz-1].val && v[sz].rotit == 0 && v[sz-1].rotit == 1 )
    {
        v[sz-1].rotit = 0;
        v[sz].rotit = 1 ;
    }

}


int main()
{
    in>>n>>cntRot>>cntEl;
    for(int i = 1; i<=n; i++)
    {
        int st,dr;
        int val=0;
        bool rotit=false;
        in>>st>>dr;
        if(dr>st)
        {
            swap(dr,st);
            rotit=true;
        }
        val=st*10+dr;
        if(rotit)
        {
            if(canRotate(val,cntRot,cntEl))
            {
                cntRot--;
            }
            else
            {
                swap(st,dr);
                val=st*10+dr;
                rotit=false;
            }
        }
        adunare(val,rotit);
    }
    for(int i = 1; i<=sz-cntEl; i++)
    {
        out<<v[i].val;
    }
    return 0;
}