- Translate each rule into one explicit state update.
- Maintain the invariant after every processed item.
- Return the accumulated state once all relevant input has been handled.
Code notes
- 159 lines of Python from the credited upstream file ccc13s3.py.
- The implementation visibly relies on sequence storage.
- No explicit loop blocks detected.
Complexity
Count the number and nesting of passes over the input, then include the maintained containers in the memory estimate.
Check the problem constraints before deciding whether this complexity will pass.
Use this to learn the idea, then write your own version.
1234567891011121314 15 161718def winning(s, t):19 score = [0,0,0,0]20 if s[0] == "W":21 score[0] = score[0] + 322 score[1] = score[1] + 023 elif s[0] == "L":24 score[0] = score[0] + 025 score[1] = score[1] + 326 else:27 score[0] = score[0] + 128 score[1] = score[1] + 129 if s[1] == "W":30 score[0] = score[0] + 331 score[2] = score[2] + 032 elif s[1] == "L":33 score[0] = score[0] + 034 score[2] = score[2] + 335 else:36 score[0] = score[0] + 137 score[2] = score[2] + 138 if s[2] == "W":39 score[0] = score[0] + 340 score[3] = score[3] + 041 elif s[2] == "L":42 score[0] = score[0] + 043 score[3] = score[3] + 344 else:45 score[0] = score[0] + 146 score[3] = score[3] + 147 if s[3] == "W":48 score[1] = score[1] + 349 score[2] = score[2] + 050 elif s[3] == "L":51 score[1] = score[1] + 052 score[2] = score[2] + 353 else:54 score[1] = score[1] + 155 score[2] = score[2] + 156 if s[4] == "W":57 score[1] = score[1] + 358 score[3] = score[3] + 059 elif s[4] == "L":60 score[1] = score[1] + 061 score[3] = score[3] + 362 else:63 score[1] = score[1] + 164 score[3] = score[3] + 165 if s[5] == "W":66 score[2] = score[2] + 367 score[3] = score[3] + 068 elif s[5] == "L":69 score[2] = score[2] + 070 score[3] = score[3] + 371 else:72 score[2] = score[2] + 173 score[3] = score[3] + 174 if score[t] == max(score) and score.count(max(score)) == 1:75 return True76 else:77 return False78 79file = open("s3.8.in", 'r')80t = int(file.readline()) - 181g = int(file.readline())82 83original = "------"84for i in range(g):85 x = file.readline().strip().split()86 a = int(x[0]) - 187 b = int(x[1]) - 188 sa = x[2]89 sb = x[3]90 if sa > sb:91 letter = "W" 92 elif sa < sb:93 letter = "L" 94 else:95 letter = "T" 96 if a == 0 and b == 1:97 original = letter + original[1:]98 elif a == 0 and b == 2:99 original = original[0] + letter + original[2:]100 elif a == 0 and b == 3:101 original = original[:2] + letter + original[3:]102 elif a == 1 and b == 2:103 original = original[:3] + letter + original[4:]104 elif a == 1 and b == 3:105 original = original[:4] + letter + original[5]106 elif a == 2 and b == 3:107 original = original[:5] + letter 108 109choice = "WLT"110possible = []111 112goto = []113for i in range(6):114 if original[i] == "-":115 goto.append(3)116 else:117 goto.append(1)118 119for a in range(goto[0]):120 for b in range(goto[1]):121 for c in range(goto[2]):122 for d in range(goto[3]):123 for e in range(goto[4]):124 for f in range(goto[5]):125 s = ""126 if goto[0] == 1:127 s = s + original[0]128 else:129 s = s + choice[a]130 if goto[1] == 1:131 s = s + original[1]132 else:133 s = s + choice[b]134 if goto[2] == 1:135 s = s + original[2]136 else:137 s = s + choice[c]138 if goto[3] == 1:139 s = s + original[3]140 else:141 s = s + choice[d]142 if goto[4] == 1:143 s = s + original[4]144 else:145 s = s + choice[e]146 if goto[5] == 1:147 s = s + original[5]148 else:149 s = s + choice[f]150 possible.append(s)151 152count = 0153for s in possible:154 if winning(s, t):155 count = count + 1156 157print count158 159