Hi all,
I've been having some problems with getting my input/output (mainly output) to be fast enough. Or at least, I think that is the problem. With the DIVSUM problem, I think that I am using a fast enough method to solve it even in Python, but I am having problems writing out the solution fast enough I think. You see, I made my own practice set of about 200000 numbers between 1 and 500000 to run my program on. In Linux, if I write to screen (don't redirect to a file), then my script takes .761 seconds, but if I don't redirect, it takes 3.5 seconds (2.9 of this is outputting to the screen). Does SPOJ redirect the output to a file? I'm guessing not because I would think that my script would be done in time otherwise.
My algorithm to solve this basically finds all of the answers from 1 to 500000 in about .5 seconds. Then it just looks up the answers from the input array and writes them to screen. I'm reading all of the input in at once and converting it to integers in about .07 seconds so that isn't the problem either. I've tried various things for output, but so far a loop with a print statement has been fastest. The code for that part is:
for n in xrange(N):
print sums[nums[n]]
where nums is the input data, and sums are the sums that I have calculated for the numbers 1 to 500000 (it actually is 500001 in length and I don't use the 0 spot).
I have also tried things like
sys.stdout.write('\n'.join([str(sums[n]) for n in nums]))
but that takes much longer because it has to create such an enormous string. I've tried using both sys.stdout.writelines and mapping a generator to sys.stdout.write, but those methods are also taking longer. Does anyone have any suggestions?