Cod sursa(job #1631015)

Utilizator PraetorGrigorosoaia Florin Praetor Data 5 martie 2016 12:39:32
Problema Balanta Scor 70
Compilator cpp Status done
Runda Arhiva de probleme Marime 3.37 kb
#include<fstream>
#define MNOC 1026 // maximum number of coins

using namespace std;

FILE*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/2];
bool B[MNOC/2];

int weighing_result; // 0, 1 or 2

//int solutionH;
//int solutionL;

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

void read()
{
    in=fopen("balanta.in", "r");

    fscanf(in, "%d%d", &nr_of_coins, &nr_of_weighings);

    lgH=lgL=nr_of_coins;

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

        fscanf(in, "%d", &lgAB);

        for (int i=1; i<=lgAB; i++)
        {
            int number;
            fscanf(in, "%d", &number);

            if (number)
                A[number]=true;
        }
        for (int i=1; i<=lgAB; i++)
        {
            int number;
            fscanf(in, "%d", &number);

            if (number)
                B[number]=true;
        }

        /*for (int i=1; i<=nr_of_coins; i++)
            out<<A[i]<<" ";
        out<<'\n';
        for (int i=1; i<=nr_of_coins; i++)
            out<<B[i]<<" ";
        out<<'\n';*/

        fscanf(in, "%d", &weighing_result);

        if (weighing_result == 0) // A=B
        {
            for (int i=1; i<=nr_of_coins; i++)
            {
                if (A[i])
                {
                    if (!H[i])
                        lgH--;
                    if (!L[i])
                        lgL--;

                    H[i]=L[i]=true;
                }
                if (B[i])
                {
                    if (!H[i])
                        lgH--;
                    if (!L[i])
                        lgL--;

                    H[i]=L[i]=true;
                }
            }
        }
        else if (weighing_result == 1) // A>B
        {
            lgH=lgL=0;

            for (int i=1; i<=nr_of_coins; i++)
            {
                if (A[i] == H[i])
                    H[i]=true;
                else if (A[i])
                    lgH++;

                if (B[i] == L[i])
                    L[i]=true;
                else if (B[i])
                    lgL++;
            }

        }
        else // A<B
        {
            lgH=lgL=0;

            for (int i=1; i<=nr_of_coins; i++)
            {
                if (B[i] == H[i])
                    H[i]=true;
                else if (B[i])
                    lgH++;

                if (A[i] == L[i])
                    L[i]=true;
                else if (A[i])
                    lgL++;
            }
        }

        /*for (int i=1; i<=nr_of_coins; i++)
            out<<H[i]<<" ";
        out<<'\n';
        for (int i=1; i<=nr_of_coins; i++)
            out<<L[i]<<" ";
        out<<'\n';*/
    }
}

void show()
{
    if ((lgH == 1) && (!lgL))
    {
        for (int i=1; i<=nr_of_coins; i++)
            if (!H[i])
            {
                out<<i;
                break;
            }
    }
    else if ((lgL == 1) && (!lgH))
    {
        for (int i=1; i<=nr_of_coins; i++)
            if (!L[i])
            {
                out<<i;
                break;
            }
    }
    else
        out<<0;
}

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

    return 0;
}