Skip to content

Instantly share code, notes, and snippets.

@aagontuk
Created April 9, 2018 19:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aagontuk/e5f4e7f30b2481d96a418882f8a2fa7e to your computer and use it in GitHub Desktop.
Save aagontuk/e5f4e7f30b2481d96a418882f8a2fa7e to your computer and use it in GitHub Desktop.
Saving the universe again - cj2018 problem 1
def flip(s):
sz = len(s)
flip = 0
for i in xrange(0, sz - 1):
if s[i] == 'C' and s[i + 1] == 'S':
s[i], s[i + 1] = 'S', 'C'
flip = 1
break
return flip
def damage_count(s):
charge = 1
damage = 0
for ins in s:
if ins == 'S':
damage += charge
else:
charge *= 2
return damage
if __name__ == "__main__":
tcase = int(raw_input())
for i in xrange(0, tcase):
d, ins = raw_input().split(' ')
d = int(d)
ins = list(ins)
hacks = 0
possible = 1
while damage_count(ins) > d:
if not flip(ins):
possible = 0
break
else:
hacks += 1
if possible:
print "Case #%d: %d" % (i + 1, hacks)
else:
print "Case #%d: IMPOSSIBLE" % (i + 1)
@Jimbly
Copy link

Jimbly commented Apr 12, 2018

What did it fail with? TIME_LIMITE_EXCEEDED, or Wrong Answer?

@jamarju
Copy link

jamarju commented Apr 12, 2018

Because you should consider flips from right to the left. Consider this test case:

6 CSSCS

Your program outputs 2, should be 1.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment