From eb6c9479d0bea3511c5ab206fcc3a2921f263f65 Mon Sep 17 00:00:00 2001 From: Ilya Nikokoshev Date: Thu, 18 Jan 2024 17:22:12 +0100 Subject: [PATCH] Add the Lineland problem --- lineland.py | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 lineland.py diff --git a/lineland.py b/lineland.py new file mode 100644 index 0000000..90fbdbc --- /dev/null +++ b/lineland.py @@ -0,0 +1,18 @@ +from math import inf + + +def main(cities): + def gen(): + stack = [(-inf, -1)] + for i, v in enumerate(cities[::-1], -len(cities) + 1): + while stack[-1][0] >= v: + _ = stack.pop() + yield stack[-1][1] + stack.append((v, -i)) + + return list(gen())[::-1] + + +if __name__ == "__main__": + _ = input() + print(*main([int(s) for s in input().split()]))