- 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
- 70 lines of Python from the credited upstream file abc406_c.py.
- The implementation visibly relies on sequence storage, ordered lookup.
- 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.
12 3 4from typing import List5 6 7def run_length_encoding(iterable: list) -> List[list]:8 """9 Args:10 iterable: A list of numbers or strings.11 12 Returns:13 A list containing consecutive characters and their count.14 15 See:16 https:qiita.com/DaikiSuyama/items/07e237b7372e7c7b343217 """18 19 from itertools import groupby20 21 results = [[key, len(list(group))] for key, group in groupby(iterable)]22 23 return results24 25 26def main():27 import sys28 from itertools import pairwise29 30 input = sys.stdin.readline31 32 n = int(input())33 p = list(map(int, input().split()))34 q = list()35 36 for pi, pj in pairwise(p):37 if pi < pj:38 q.append("<")39 elif pi > pj:40 q.append(">")41 42 if len(set(q)) == 1:43 print(0)44 exit()45 46 rle = run_length_encoding(q)47 48 if q[0] == ">":49 rle = [("<", 0)] + rle50 if q[-1] == ">":51 rle = rle + [("<", 0)]52 53 m = len(rle)54 ans = 055 56 for i in range(0, m, 2):57 if i + 2 >= m:58 break59 60 _, ci = rle[i]61 _, cj = rle[i + 2]62 63 ans += ci * cj64 65 print(ans)66 67 68if __name__ == "__main__":69 main()70