The second algorithm practical test problem is solved with python. We will update it as soon as it is solved.
A Replace B with-and be careful when comparing the basement and ground floors (e.g. 1F --B1 $ = $ 1 $ \ neq $ 2).
# A
def f(i):
num = int(i.replace('B', '-').replace('F', ''))
return num if 'B' not in i else num + 1
inp = input().split()
arr = list(map(f, inp))
ans = arr[0] - arr[1]
print(ans if ans >= 0 else -ans)
B Just count.
# B
S = input()
import collections
d = dict(collections.Counter(S))
print(max(d.keys(), key=lambda k: d[k]))
C The maximum number of elements is about $ 50 ^ 2 = 2500 $ (the number actually processed is smaller), so it is enough to honestly examine all the elements.
# C
N = int(input())
Ss = list(input() for i in range(N))
for i in range(N-2, -1, -1):
for j in range(2*N-1):
if Ss[i][j] == '#':
if j != 0 and Ss[i+1][j-1] == 'X':
Ss[i] = Ss[i][:j] + 'X' + Ss[i][j+1:]
if j != 2 * N - 1 and Ss[i+1][j+1] == 'X':
Ss[i] = Ss[i][:j] + 'X' + Ss[i][j+1:]
if Ss[i+1][j] == 'X':
Ss[i] = Ss[i][:j] + 'X' + Ss[i][j+1:]
for s in Ss:
print(s)
D This also has a small number of patterns, so it is enough to investigate all the patterns.
# D
S = input()
N = len(S)
# N = 0
if N == 0:
print(0)
exit()
# lenth = 1
ans = set([S[i] for i in range(N)]) | {'.'}
if N == 1:
print(len(ans))
exit()
# lenth = 2
for i in range(1, N):
ans |= {'{}{}'.format(S[i-1], S[i]), '.{}'.format(S[i]), '{}.'.format(S[i-1])}
ans |= {'..'}
if N == 2:
print(len(ans))
exit()
for i in range(2, N):
ans |= {'{}{}{}'.format(S[i-2], S[i-1], S[i]), '.{}{}'.format(S[i-1], S[i]), '{}.{}'.format(S[i-2], S[i]), \
'{}{}.'.format(S[i-2], S[i-1]), '..{}'.format(S[i-2]), '.{}.'.format(S[i-1]), '{}..'.format(S[i])}
ans |= {'...'}
print(len(ans))
E This also requires a small number of calculations, so all the elements should be investigated.
# E
N = int(input())
As = list(map(int, input().split()))
def replace_and_recursion(x, target, iter):
x = As[x-1]
if x-1 == target:
return str(iter)
else:
return replace_and_recursion(x, target, iter+1)
print(' '.join([replace_and_recursion(i+1, i, 1) for i in range(N)]))
F From the first day, you can process the tasks with the highest points. Sorting every day doesn't have enough time, so adding it to an element with bisect will give you enough processing time.
# F
N = int(input())
ABs = [list(map(int, input().split())) for i in range(N)] + [[N, 0]]
import itertools
ABs = sorted(ABs, key=lambda ab: ab[0])
groups = {k:list(g) for k, g in itertools.groupby(ABs, key=lambda ab: ab[0])}
import bisect
sorted_arr = []
last = 0
for k in range(1, N+1):
if k in groups.keys():
group = groups[k]
for g in group:
index = bisect.bisect_right(sorted_arr, g[1])
sorted_arr.insert(index, g[1])
task = sorted_arr.pop(-1)
print(last + task)
last += task```
Create a string and delete it from it. .. .. If you do, you will not have enough time, so you can store the query information in a queue and process it in time.
G
Q = int(input())
Qs = [input().split() for i in range(Q)]
from collections import Counter, deque
deq = deque()
for q in Qs:
if q[0] == '1':
deq.append({'char': q[1], 'size': int(q[2])})
if q[0] == '2':
size = int(q[1])
dic = {}
while deq and size > 0:
data = deq[0]
if data['size'] >= size:
dic[data['char']] = dic.get(data['char'], 0) + size
deq[0]['size'] -= size
size = 0
else:
dic[data['char']] = dic.get(data['char'], 0) + data['size']
size -= data['size']
deq.popleft()
print(sum([dic[moji]**2 for moji in dic.keys()]))
S -> {1} -> {2} ... ->The state transition to G should be performed.
H
N, M = list(map(int, input().split()))
A_matrix = [input() for i in range(N)]
max_cost = 10**9
number_loc_cost = {}
rec
for i in range(N):
A_row = A_matrix[i]
for j in range(M):
val = A_row[j]
if val not in number_loc_cost.keys():
number_loc_cost[val] = {}
number_loc_cost[val][i * M + j] = max_cost
S_loc = number_loc_cost['S'].keys()
for s_loc in S_loc:
number_loc_cost['S'][s_loc] = 0
targets = ['S'] + list(range(1, 9+1)) + ['G']
try:
for i in range(len(targets)-1):
frm = str(targets[i])
to = str(targets[i+1])
for f_loc in number_loc_cost[frm].keys():
f_x, f_y = f_loc % M, f_loc // M
for t_loc in number_loc_cost[to].keys():
t_x, t_y = t_loc % M, t_loc // M
cost = abs(f_x - t_x) + abs(f_y - t_y)
number_loc_cost[to][t_loc] = min(number_loc_cost[to][t_loc], number_loc_cost[frm][f_loc] + cost)
G_loc = list(number_loc_cost['G'].keys())[0]
print(number_loc_cost['G'][G_loc])
except:
print(-1)
Divide the list of participants into even and odd numbers and compare them. Only the participants who have won the comparison can update the participant list, and then repeat the same comparison.
I
N = int(input())
As = tuple(map(int, input().split()))
results = {}
id_to_power = {i: As[i] for i in range(2 ** N)}
ids = list(id_to_power.keys())
n = 1
while len(ids) > 1:
size = len(ids)
res = [id_to_power[ids[2*i]] > id_to_power[ids[2*i+1]] for i in range(size // 2)]
for i in range(size // 2): results[ids[2*i+1] if res[i] else ids[2*i]] = n
ids = [ids[2*i] if res[i] else ids[2*i+1] for i in range(size // 2)]
n += 1
results[ids[0]] = n-1
for i in range(len(id_to_power)):
print(results[i])
Data is stored for each depth of parentheses, and information is applied to the next higher hierarchy when the parentheses are closed.(d[depth-1] += d[depth] + d[depth][::-1])Just do.
J
S = input()
res = ''
depth = 0
bracket_depth_to_str = {depth: ''}
for s in S:
if s == '(':
depth += 1
bracket_depth_to_str[depth] = ''
elif s == ')':
bracket_depth_to_str[depth-1] += bracket_depth_to_str[depth] + bracket_depth_to_str[depth][::-1]
depth -= 1
else:
bracket_depth_to_str[depth] += s
print(bracket_depth_to_str[0])
Not yet after K.
Recommended Posts