Skip to content

Instantly share code, notes, and snippets.

@DariaPlotnikova
Created July 21, 2018 17:48
Show Gist options
  • Save DariaPlotnikova/278fe322981aff4dc0995c7872269075 to your computer and use it in GitHub Desktop.
Save DariaPlotnikova/278fe322981aff4dc0995c7872269075 to your computer and use it in GitHub Desktop.
import re
# Константы
space = ' '
astart = 'a'
dstart = 'd'
def write_to_dct(dct, day, dcnt):
# Если этот день еще не заносился - самый последний, вставляем в конец
if day in dct:
dct[day] += dcnt
else:
dct[day] = dcnt
def print_result(mef_max, kir_max):
with open('output.txt', 'w') as of:
of.write(
f'{mef_max if mef_max > 0 else 0} {kir_max if kir_max > 0 else 0}',
)
try:
with open('input.txt', 'r') as f:
tot = int(f.readline())
day = int(f.readline())
arrs = dict()
deps = dict()
# Иду по строкам
arr_re = r'arrival \d+ \d+'
dep_re = r'departure \d+ \d+'
for line in f:
if re.match(dep_re, line) or re.match(arr_re, line):
# Читаю день, заношу в арривалс, депарчурс колво
cur_type, cur_day, cnt = line.split(space)
cur_day, cnt = int(cur_day), int(cnt)
if cur_type[0] == astart: # прибытие
write_to_dct(arrs, cur_day, cnt)
elif cur_type[0] == dstart: # отбытие
write_to_dct(deps, cur_day, cnt)
else:
continue
# Добиваю нулями недостающие дни
arr_keys = set(arrs.keys())
dep_keys = set(deps.keys())
days_to_arr = dep_keys - arr_keys
days_to_deps = arr_keys - dep_keys
{arrs.update({k: 0}) for k in days_to_arr}
{deps.update({k: 0}) for k in days_to_deps}
arrs = sorted(arrs.items())
deps = sorted(deps.items())
mef_max = 0
kir_max = 0
prev_people = 0
for ar, dep in zip(arrs, deps):
cur_day = ar[0]
# Ушли за нужный день, остановка
if cur_day > day:
print_result(mef_max, kir_max)
exit()
# Кирилл, четные
if cur_day % 2 == 0:
cur_kir_people = ar[1] - dep[1] + prev_people
if cur_kir_people > kir_max:
kir_max = cur_kir_people
prev_people = cur_kir_people
# Мефодий, нечетные
else:
cur_mef_people = ar[1] - dep[1] + prev_people
if cur_mef_people > mef_max:
mef_max = cur_mef_people
prev_people = cur_mef_people
print_result(mef_max, kir_max)
except:
exit()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment