Created
May 7, 2011 14:46
-
-
Save lprsd/960549 to your computer and use it in GitHub Desktop.
gcj.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def compute_count(case): | |
hops = int(case[0]) | |
hop = 1 | |
total_time = 0 | |
pos = {'O':1, | |
'B':1} | |
available_time = 0 | |
consumed_time = {'robot':'O', | |
'time': 0} | |
while hop<hops*2+1: | |
time_taken = abs(int(case[hop+1]) - pos[case[hop]]) +1 | |
#print "time_taken=%s"%time_taken | |
actual_time = time_taken | |
if consumed_time['robot'] == case[hop]: | |
consumed_time['time'] += actual_time | |
else: | |
consumed_time['robot'] = case[hop] | |
actual_time = time_taken - consumed_time['time'] | |
actual_time = actual_time if actual_time>1 else 1 | |
consumed_time['time'] = actual_time | |
total_time += actual_time | |
pos[case[hop]] = int(case[hop+1]) | |
hop+=2 | |
return total_time | |
def test_cases(): | |
with open("sample3.txt") as f: | |
size = f.readline() | |
print 'size=%s'%size | |
for i,line in enumerate(f): | |
#print "line=%s"%line | |
print "Case #%s: %s"%(i+1,compute_count(line.split())) | |
test_cases() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment