0

This is my solution to Spoj 11373. Coke madness

#include <cstdio>

int main(){
    int T; scanf("%d",&T);

    for(int j = 1; j <= T; j++){
        int n;
        scanf("%d",&n);
        long long sum = 0, small = 0;
        int t;
        for(int i = 0; i < n; i++) {
            scanf("%d",&t);
            sum += t;
            if( sum < small) small = sum;
        }
        printf("Scenario #%d: %lld\n", j, -1*small+1);
    }
}

The problem is simple. This solution takes 0.12 seconds on Spoj, though there are 0.01 second solutions. I am curious what optimizations may be done to this code to get faster. I thought -1*small+1 may be got using bit-wise operations but didn't find how. Also I can't get rid of long long since sum may exceed int.

4
  • 1
    This program is probably I/O bound, so I'm not sure you can make it faster (by improving CPU time only) Commented Feb 2, 2013 at 15:25
  • 1
    You can simply change -1*small+1 to 1 - small Commented Feb 2, 2013 at 15:25
  • 1
    @David your suggestions didn't help so much, trying to read the input file at once Commented Feb 2, 2013 at 15:34
  • 1
    @David Riddle me this: do you know the difference between an int outside of a loop and one inside of it? Commented Feb 2, 2013 at 15:51

1 Answer 1

11

You're making way too many I/O calls. Read the whole file at once, and then parse it, then create your output, and then write it all at once.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.