LightOJ - Shadow Sum

 Problem Link Shadow Sum


/*

    Accepted


    It is a data structure problem. I have used the array and map to track same positive

    , negative, single, and multiple times appeared numbers. Done the summation finally.


*/


#include <bits/stdc++.h>


using namespace std;


#define sf scanf

#define pf printf


typedef long long LL;

const LL high = 2e4 + 10;


int ar[high];


map<int,int>br;

map<int,int>flag;


void clean()

{

    br.clear();

    flag.clear();

}


int main()

{

    int i, N, test, tc=0;


    sf("%d", &test);


    while(test--)

    {

        clean();


        sf("%d", &N);


        for(i=0;i<N;i++)

        {

            sf("%d", &ar[i]);

        }


        //for(i=0;i<N;i++) cout << br[ar[i]] << "; ";


        int x , y;


        for(i=0;i<N;i++)

        {

            x = ar[i];


            if(x < 0)

            {

                y = x * (-1);


                br[y] = x;

            }


            else

            {

                br[x] = x;

            }

        }


        LL sum = 0;


        for(i=0;i<N;i++)

        {

            x = ar[i];


            if(x < 0)

            {

                y = x * (-1);


                if(flag[br[y]] == 0)

                {

                    sum += br[y];


                    flag[br[y]] = 1;


                    //cout << br[y] << "; ";

                }

            }


            else

            {

                if(flag[br[x]] == 0)

                {

                    sum += br[x];


                    flag[br[x]] = 1;


                    //cout << br[x] << "; ";

                }

            }

        }


        //cout << "\nsum = " << sum << "\n";

        pf("Case %d: %lld\n", ++tc, sum);

    }


    return 0;

}


Comments

Popular posts from this blog

SPOJ-CMG - Collecting Mango

LightOJ 1009 - Back to Underworld

LeetCode Palindrome Number