- 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
- 75 lines of Python from the credited upstream file ccc15j4.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.
12345678910111213141516 17file = open("j4.3.in", "r")18m = int(file.readline())19 20database = []21time = -122for i in range(m):23 line = file.readline().split()24 letter = line[0]25 friend = int(line[1])26 27 if letter == "W":28 time = time + friend - 1 29 30 else:31 time = time + 132 33 if letter != "W":34 35 found = False36 k = 037 while not found and k < len(database):38 if friend == database[k][0]:39 found = True40 else:41 k = k + 142 43 if letter == "R": 44 45 if found:46 database[k][1] = time47 else:48 database.append ([friend, time, 0])49 elif letter == "S":50 51 database[k][2] = database[k][2] + (time - database[k][1])52 database[k][1] = -153 545556for i in range(len(database)-1):57 for j in range(i+1, len(database)):58 if database[i][0] > database[j][0]:59 temp = database[i]60 database[i] = database[j]61 database[j] = temp62 6364for i in range(len(database)):65 if database[i][1] == -1:66 print database[i][0], database[i][2]67 else:68 print database[i][0], -169 70 71 72 73 74 75