-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmaxelement2.py
73 lines (58 loc) · 1.74 KB
/
maxelement2.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#! /usr/bin/env python3
from bisect import bisect_left, bisect, insort
import pathlib
import sys
import time
def get_max(data):
stack = []
sorted_list = []
res = []
for line in data:
match line.split():
case ['1', n]:
n = int(n)
stack.append(n)
insort(sorted_list, n)
case ['2']:
val = stack.pop()
index = bisect_left(sorted_list, val)
sorted_list.pop(index)
case ['3']:
res.append(sorted_list[-1])
case _:
raise ValueError(f'Unexpected query: {line}')
return res
def get_input(file=None):
def process_input():
# Input format:
# n queries
# Queries:
# 1 x -Push the element x into the stack
# 2 -Delete the element present at the top of the stack.
# 3 -Print the maximum element in the stack.
n = int(input())
return [input() for _ in range(n)]
if file:
with open(file) as infile:
save_stdin = sys.stdin
sys.stdin = infile
res = process_input()
sys.stdin = save_stdin
return res
return process_input()
if __name__ == '__main__':
start_time = time.time()
file = None
if len(sys.argv) == 2 and pathlib.Path(sys.argv[1]).is_file():
file = sys.argv[1]
elif len(sys.argv) >= 2:
print(f'Usage: {pathlib.Path(__file__).name} [<input_file>]')
sys.exit(1)
# Get input...
data = get_input(file)
# Process...
res = get_max(data)
for item in res:
print(item)
end_time = time.time()
print(f'\nRun time: {(end_time - start_time):,.4f} seconds\n')