Monday, 29 October 2012

COKE MADNESS


SPOJ Problem Set (classical)

11373. Coke madness

Problem code: RPLC



David likes coke, lets say he likes it a lot... One day he was walking by a narrow street when he sees a lot of bottles of cokes, from different brands, he wants to drink it all, but he noticed that one brand gives him power, the other brand weaken him, now, he can wait and regain more energy, but he don't want to do that, he will wait at the beginning and, when he has the sufficient energy he will drink all the cokes in the street.

Please, help him find when he will be in the perfect moment to drink all the cokes.



INPUT:

Will start with an integer T denoting the number of test cases, then, T lines will follow, for each test case, there will be an integer N, then, in the next line will be N integers, this will be the number of cokes, and the values of the cokes in the floor (the positive one gives energy, the negative ones will take his energy).



OUTPUT:

Each test case will output the string “Scenario #i: “ where i is the number of test case analyzed, followed by the minimum energy required by David to pass the street.



INPUT

OUTPUT

2

5

4 -10 4 4 4



5

1 2 3 4 5

Scenario #1: 7

Scenario #2: 1




“Blank line between test cases for clarification and separation”
“The life of David should never reach 0 or less”



CONSTRAINTS:
1<=N<=1000000
-10000000<=Ni<=10000000

SOLUTION----


#include
#include
int main()
{
    int t,i,count=1;
    scanf("%d",&t);
    while(t--)
    {
        long long int num,sum=0,coke=0;
        scanf("%lld",&num);
        long long int arr[num];
        for(i=0;i<num;i++)
        {
            scanf("%lld",&arr[i]);
            sum+=arr[i];
            if(sum<0)
            {
                coke+=fabs(sum);
                sum=0;
            }
        }
        
        printf("Scenario #%d: %lld\n",count,coke+1);
        count+=1;
        
    }
    return 0;
}

5 comments:

Anonymous said...

Your solution fails for the given test cases. The sums will be 4 -6 -2 2 6 for that first test case. Your solution would say we sum 6 + 2 = 8 and then add 1, which is 9, whereas the answer is 7.

The correct solution is to take min of the sum array, and return -min+1. So here min = -6. return -(-6) +1 = 7.

CHANDAN SINGH said...

no its correct !! the answer for ur test case is 5 not 7 !!
you checked ur test case woith not proper input formet thtas why u got answer 9.
put input in proper way
1(test case)
5(no. of elements)
4 -6 -2 2 6
then outputs is 5 not 7 .

Anonymous said...

No, you haven't understood me.

I said that your solution fails for the given test cases, not ones I made up myself. The first test case is

2

5

4 -10 4 4 4

I was saying that the sums you're computing in your program, those will be 4 -6 -2 2 6 for that first test case.

Your solution would say we sum 6 + 2 = 8 and then add 1, which is 9, whereas the answer is 7.

The answer is definitely 7, this is evidenced by "Scenario #1: 7" in the problem statement.

If the actual test case (not the sum array), were to be:

1(test case)
5(no. of elements)
4 -6 -2 2 6

...then I would agree the answer is 5. However, your program would produce the wrong answer there too, as the sums calculated would be 4 -2 -4 -2 8, and here you would sum 2+4+2 and add 1, giving you 9.


We could see that your program isn't right by running it, but unfortunately it doesn't compile to begin with.

CHANDAN SINGH said...

no sir, its correct solution
http://ideone.com/Ytx9ZA
check above link same code as on my blog!!
first the above solution not compiled becuz u should mention
#include"stdio.h"
#include"math.h"
another thing is that for ur test cases
1

5

4 -10 4 4 4

output is 7.
now ur doubt is how above program calculated the sum

now sum calculated is
4 -6 4 8 12

here only one negative term that is -6
so the value of coke will be fabs(-6) so coke become 6 unit
and after that code print the value "coke+1" that is 7.
look when the value of sum become negative then coke=fabs(sum) and sum initialize to "zero". thats the point..

Anonymous said...

Hmmm, did you really have that sum = 0 there before? I could have sworn I'm seeing it for the first time. Maybe I just didn't see it.

It would help if the code given on your site was ready to run, so that people could actually test your code when in doubt. I know it was just imports now, but when I tried it and saw syntax errors, just thought "another person who posted code without ever testing it", and I just stopped right there.