Created
April 9, 2018 19:13
-
-
Save aagontuk/e5f4e7f30b2481d96a418882f8a2fa7e to your computer and use it in GitHub Desktop.
Saving the universe again - cj2018 problem 1
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 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) |
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
What did it fail with? TIME_LIMITE_EXCEEDED, or Wrong Answer?