- 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
- 68 lines of Python from the credited upstream file ccc12s3.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.
123456789101112 13class Info:14 def __init__(self, r, c):15 self.reading = r16 self.count = c17 18 19file = open("s3.5.in", 'r')20 2122freq = []23for i in range(1001):24 freq.append(Info(i,0))25 262728firstline = True29for line in file:30 if firstline:31 firstline = False32 else:33 r = eval(line)34 freq[r].count = freq[r].count + 135 363738for i in range (1,1001):39 hold = freq[i]40 j = i - 141 while j >= 0 and (freq[j].count < hold.count or42 (freq[j].count == hold.count and43 freq[j].reading < hold.reading)):44 freq[j+1] = freq[j]45 j = j - 146 freq[j+1] = hold 47 4849multihigh = freq[0].count == freq[2].count50multisecondhigh = freq[0].count != freq[1].count and freq[1].count == freq[2].count51 5253if multihigh:54 i = 155 while i < 1001 and freq[0].count == freq[i].count:56 i = i + 1 57 print abs(freq[0].reading - freq[i-1].reading)58elif multisecondhigh:59 i = 360 m = abs(freq[0].reading - freq[1].reading)61 while i < 1001 and freq[1].count == freq[i].count:62 m = max (m, abs(freq[0].reading - freq[i].reading))63 i = i + 1 64 print m65else:66 print abs(freq[0].reading - freq[1].reading)67 68