GCJ2011 Qualification Round A Bot Trust

今度はPythonで書いてみるという趣旨で.いまいちPythonに慣れる機会がないので,ここらでお勉強をしようと思う.問題は以前書いたので省略.

プログラム

#! c:/Python27/python.exe
# -*- coding: utf-8 -*-

input = "A-large-practice.in"
output = "GCJ2011_QA_output.txt"

def step(lresult,lp,input,lpot):
    pot = max(abs(lp-input)-lpot,0) + 1
    result = lresult +  pot
    return result,input, pot

if __name__ == '__main__':
    f = open(input)
    fout = open(output,'w')

    a = int( f.readline() )
    for i in range(a):
        curB = 1
        curO = 1
        result = 0
        bpot = 0
        opot = 0
        line = f.readline().split()
        n = int(line[0])
        k = 1
        for j in range(n):
            if line[k] == 'B':
                result, curB ,time = step(result, curB, int(line[k+1]), bpot)
                opot += time
                bpot = 0
            if line[k] == 'O':
                result, curO ,time = step(result, curO, int(line[k+1]), opot)
                bpot += time
                opot = 0
            k = k + 2
        print "Case #"+str(i+1)+":", result
        fout.write('Case #' + str(i+1) + ': ' + str(result) + '\n')
    fout.close()

うーむ,難しい.