-
Notifications
You must be signed in to change notification settings - Fork 0
/
Running_Fumes-chap1.py
52 lines (46 loc) · 1.51 KB
/
Running_Fumes-chap1.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
from collections import deque
import sys
import os
script_dir = os.path.dirname(__file__) #<-- absolute dir the script is in
# rel_path = 'running_on_fumes_chapter_1_input_sample.txt'
rel_path = 'running_on_fumes_chapter_1_input.txt'
abs_file_path = os.path.join(script_dir, rel_path)
in_file = open(abs_file_path, 'r')
out_file_check = True
rel_path = rel_path.replace('_input', '_output')
abs_file_path = os.path.join(script_dir, rel_path)
out_file = open(abs_file_path, 'w')
def printf(s, linebreak = True):
#print(s)
if out_file_check:
if linebreak:
out_file.write(s + '\n')
else:
out_file.write(s)
def running_on_fumes_chapter_1():
N, M = map(int, in_file.readline().strip().split())
C = [int(in_file.readline()) for _ in range(N)]
dq = deque([(0, 0)])
for i in range(1, len(C)):
count = 0
if dq and i-dq[0][0] > M:
count += 1
dq.popleft()
if not dq:
return -1
if not C[i]:
continue
#print(dq[0][1],C[i])
d = dq[0][1] + C[i]
while dq and dq[-1][1] >= d:
dq.pop()
dq.append((i, d))
return dq[0][1]
t = int(in_file.readline())
for case in range(t):
check_result = running_on_fumes_chapter_1()
if(case < t - 1):
printf('Case #{}: {}'.format(case+1, check_result))
else:
printf('Case #{}: {}'.format(case+1, check_result), linebreak=False)
out_file.close()