ENG  RUSTimus Online Judge
Online Judge
Problems
Authors
Online contests
About Online Judge
Frequently asked questions
Site news
Webboard
Links
Problem set
Submit solution
Judge status
Guide
Register
Update your info
Authors ranklist
Current contest
Scheduled contests
Past contests
Rules
back to board

Discussion of Problem 1915. Titan Ruins: Reconstruction of Bygones

python-solvable ?
Posted by PSV 3 Jul 2013 21:26
request for any optimizations possible for python solution: not it get TLE #42

import sys

#sys.stdin = open("smalltest.txt")
#sys.stdin = open("input.txt")

top = 0
stack = [0] * 2000020
input = sys.stdin.read().split()
operations = int(input[0])
ans = ""

for i in xrange(operations) :
    op = int(input[i+1])
    if op > 0 :
        stack[top] = op
        top += 1
    elif op < 0 :
        top -= 1
        ans += str(stack[top]) + "\n"
    else :
        if operations - i > top :
            l = max( 0, top - operations + i )
            cnt = top - l
            stack[top:top+cnt] = stack[l:top]
            top += cnt

sys.stdout.write( ans )