Here you will find solutions of many problems on spoj. If you want solution of some problem which is not listed in blog or have doubt regarding any spoj problem (which i have solved) or any programming concept (data structure) you can mail me @ raj.nishant360@gmail.com

And my humble request to you all that don't copy the code only try to understand the logic and algorithm behind the code. I have started this because if you tried as hard as you can and still can't find any solution to the problem then you can refer to this.
You can read my answer how to start competitive programming CLICK HERE

Saturday, March 29, 2014

BLOPER - Operators

Link to the question

Solution of BLOPER spoj - spoj Operators

Maximum & Minimum Sum possible with N natural number is n*(n+1)/2 and out of it we need to take S. then we'll left with N - S. So, our aim is to make the final N-S effect zero therefore we divide N - S in two equal part and it only possible when N-S is an even number hence would be "Impossible" for odd count. After dividing if possible let us say S1,S2 (equal in magnitude), all we'll have to do is make S1 - S2.
Rest i leave it upto you. And think over it when S is negative.

#include<stdio.h>

int main()
{
    int arr[505]={0},i,j,n,s;

    scanf("%i%i",&n,&s);

    if(s < 0)
    {
        j = (n*(n+1))/2;

        if( (j+s)&1 || j <= -1*s )
        {
            printf("Impossible\n");
        }

        else
        {
            j = (j + s)/2 - 1;

            i = n+1;
            while(--i > 1 && j)
            {
                if( j - i > 1)
                {
                    j -= i;
                    arr[i] = 1;
                }

                else if(j-i == 0)
                {
                    j -= i;
                    arr[i] = 1;
                    break;
                }
            }

            if(j)
                printf("Impossible\n");
            else
            {
                printf("1");

                i = 1;
                while(++i <= n)
                {
                    arr[i] ? printf("+%i",i) : printf("-%i",i);
                }

                printf("\n");
            }

        }
    }

    else
    {
        j = (n*(n+1))/2;

        if( (j-s)&1 || j < s )
        {
            printf("Impossible\n");
        }

        else
        {
            j = (j - s)/2;

            i = n+1;
            while(--i > 1 && j)
            {
                if( j - i > 1)
                {
                    j -= i;
                    arr[i] = 1;
                }

                else if(j-i == 0)
                {
                    j -= i;
                    arr[i] = 1;
                    break;
                }
            }

            if(j)
            {
                printf("Impossible\n");
            }

            else
            {
                printf("1");

                i = 1;
                while(++i <= n)
                {
                    arr[i] ? printf("-%i",i) : printf("+%i",i);
                }

                printf("\n");
            }
        }
    }

    return 0;
}

1 comment:

  1. WTF, understood the logic but couldn't understand the uncommented code :/

    ReplyDelete

Your comment is valuable to us