Cod sursă (job #317795)

Utilizator avatar codrut_lemeni Lemeni Ioan Codrut codrut_lemeni IP ascuns
Problemă Domino Compilator cpp | 1,97 kb
Rundă Arhiva de probleme Status evaluat
Dată 21 oct. 2017 13:40:56 Scor 100
#include <iostream>
#include <stdio.h>

using namespace std;

const int N = 1e5 + 5 ;

struct Solution{
    int val ;
    bool isRot ;
};

int noPiese , noRot , noDel ;

Solution sol [ N ] ;
int crSol = -1 ;


int canRotate( int domVal , int crRot , int crDel ){
    static int crDom ;

    crDom = crSol ;

    while ( crDel && crDom >=0 && domVal > sol[ crDom ].val ){
        if ( sol [ crDom ].isRot == 1 ){
            crRot ++ ;
        }
        crDom -- ;
        crDel -- ;
    }

    if ( crRot ){
        return 1 ;
    }

    return 0 ;
}

void addToSol ( int domVal , int isRotated ){

    while ( noDel && crSol >= 0 && domVal > sol [ crSol ].val ){
        if ( sol[ crSol ].isRot == 1 ){
            noRot ++ ;
        }
        crSol -- ;
        noDel -- ;
    }

    sol [ ++crSol ].isRot = isRotated ;
    sol [ crSol ].val = domVal ;

    if ( crSol >=1 && sol [ crSol ].val == sol [ crSol - 1 ].val && sol [ crSol ].isRot == 0 && sol [ crSol -1 ].isRot == 1 ){
        sol [ crSol - 1 ].isRot = 0;
        sol [ crSol ].isRot = 1 ;

    }

}


int main(){
    int i ;
    int x , y ;

    freopen("domino.in","r",stdin);
    freopen("domino.out","w",stdout);

    scanf("%d%d%d",&noPiese , &noRot , &noDel );


    int isRotated , domVal ;
    for ( i = 0 ; i < noPiese ; i++ ){
        scanf("%d %d",&x , &y);

        if ( y > x ){
            isRotated = 1 ;
            domVal = y * 10 + x ;
        }else{
            isRotated = 0 ;
            domVal = x * 10 + y ;
        }

        if ( isRotated ){
            int temp = canRotate( domVal , noRot , noDel );

            if ( temp == 0 ){
                isRotated = 0;
                domVal = x * 10 + y ;
            }else{
                noRot -- ;
            }
        }

        addToSol( domVal , isRotated );


    }

    for ( i = 0 ; i <= crSol - noDel ; i ++ ){
        printf("%d",sol[ i ].val );
    }

    return 0;
}