Cod sursa(job #1631032)

Utilizator PraetorGrigorosoaia Florin Praetor Data 5 martie 2016 12:46:04
Problema Balanta Scor 70
Compilator cpp Status done
Runda Arhiva de probleme Marime 4.1 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;
                }
                else
                {
                    if (!H[i])
                        solutionH=i;
                    if (!L[i])
                        solutionL=i;
                }

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

                    H[i]=L[i]=true;
                }
                else
                {
                    if (!H[i])
                        solutionH=i;
                    if (!L[i])
                        solutionL=i;
                }
            }
        }
        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++;
                    solutionH=i;
                }

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

        }
        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++;
                    solutionH=i;
                }

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

        /*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;
            }*/
        out<<solutionH;
    }
    else if ((lgL == 1) && (!lgH))
    {
        /*for (int i=1; i<=nr_of_coins; i++)
            if (!L[i])
            {
                out<<i;
                break;
            }*/
        out<<solutionL;
    }
    else
        out<<0;
}

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

    return 0;
}