Cod sursa(job #1631096)

Utilizator PraetorGrigorosoaia Florin Praetor Data 5 martie 2016 13:08:09
Problema Balanta Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 2.06 kb
#include<fstream>
#define MNOC 1026 // maximum number of coins

using namespace std;

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

int nr_of_coins;
int nr_of_weighings;

int lgH;
bool H[MNOC];

int lgL;
bool L[MNOC];

int lgAB;
bool A[MNOC];
bool B[MNOC];

int weighing_result; // 0, 1 or 2

int solutionH;
int solutionL;

int number;

void initialize_AB()
{
    for (int i=1; i<=nr_of_coins; i++)
        A[i]=B[i]=false;
}

void read()
{
    in>>nr_of_coins>>nr_of_weighings;

    for (int i=1; i<=nr_of_weighings; i++)
    {
        initialize_AB();

        in>>lgAB;

        for (int i=1; i<=lgAB; i++)
        {
            in>>number;
            A[number]=true;
        }
        for (int i=1; i<=lgAB; i++)
        {
            in>>number;
            B[number]=true;
        }

        in>>weighing_result;

        if (weighing_result == 0) // A=B
        {
            for (int i=1; i<=nr_of_coins; i++)
            {
                if ((A[i]) || (B[i]))
                    H[i]=L[i]=true;
            }
        }
        else if (weighing_result == 1) // A>B
        {
            for (int i=1; i<=nr_of_coins; i++)
            {
                if (A[i] == H[i])
                    H[i]=true;
                if (B[i] == L[i])
                    L[i]=true;
            }
        }
        else // A<B
        {
            for (int i=1; i<=nr_of_coins; i++)
            {
                if (B[i] == H[i])
                    H[i]=true;
                if (A[i] == L[i])
                    L[i]=true;
            }
        }
    }
}

void count_lgH_lgL()
{
    for (int i=1; i<=nr_of_coins; i++)
    {
        if (!H[i])
        {
            lgH++;
            solutionH=i;
        }
        if (!L[i])
        {
            lgL++;
            solutionL=i;
        }
    }
}

void show()
{
    if (lgH + lgL == 1)
        out<<max(solutionH, solutionL);
    else
        out<<0;
}

int main()
{
    read();
    count_lgH_lgL();
    show();

    return 0;
}