I could only solve up to D in time. .. .. After that, I googled and referred to other people's codes and completed the race to F.
A Compare the part of the string T excluding the last character with S.
# A
S = input()
T = input()
T = T[:len(S)]
print('Yes' if T == S else 'No')
B Take as many A cards as you can, then B as many as you can, and C if you still can't reach K.
# B
A, B, C, K = list(map(int, input().split()))
score = 0
if K <= A:
    score = K
    print(score)
    exit()
score += A
K -= A
if K <= B:
    print(score)
    exit()
K -= B
print(score - K)
C Since the maximum number of patterns is $ 2 ^ {12} = 4096 $, it is enough time to check all patterns with python. Very crazy code.
# C
N, M, X = list(map(int, input().split()))
CAs = [list(map(int, input().split())) for i in range(N)]
import numpy as np
Cs = np.array([ca[0] for ca in CAs])
As = [np.array(ca[1:]) for ca in CAs]
import itertools
states = list(itertools.product([True, False], repeat=N))
def compute(state):
    arr = np.array([0] * M)
    for i in range(N):
        arr += As[i] * state[i]
    price = (Cs * state).sum() if ((arr >= X).sum() == M) else -1
    return price
res = np.array(list(map(compute, states)))
res = res[res > 0]
print(min(res) if len(res) > 0 else -1)
D There is always a loop, so check the size of the loop and subtract the appropriate amount from the number of times you use the teleporter $ K $. This proper deduction took a long time and I couldn't get into the E problem. .. ..
# D
N, K = list(map(int, input().split()))
As = list(map(int, input().split()))
def compute(city, K):
    for i in range(K):
        city = As[city-1]
    print(city)
cities_visited = [False] * N
i = 0
city = 1
city_visited_time = {}
while not cities_visited[city - 1]:
    cities_visited[city - 1] = True
    city_visited_time[city-1] = i
    city = As[city-1]
    i += 1
loop_start = city_visited_time[city-1]
loop_end = i
div = (K - loop_start) // (loop_end - loop_start)
if K - loop_start < 0 or div == 0:
    compute(1, K)
else:
    compute(city, K - loop_start - div * (loop_end - loop_start))
E The objective function is as follows.
\sum_{n=0}^K M (M-1) ^{N-1-n} {}_{N-1} C _n
$ n $ is the number of adjacent pairs of blocks of the same color. For example, if $ n = 0 $, the colors of all adjacent blocks will be different. In that case, the leftmost block can be freely selected according to $ M $, and the right one must be selected differently from the left, so $ M-1 $ can be selected. When it is 0 $, it becomes $ M (M-1) ^ {N-1} $. When $ n = 1 $, if one set is considered together, it can be calculated in the same way as when $ n = 0 $ is considered for $ N-1 $ blocks.
If the objective function is calculated as it is, it becomes TLE or MLE. Therefore, I use Fermat's little theorem as a technique (reference link).
# E
N, M, K = list(map(int, input().split()))
p = 998244353
comb = 1
res = 0
for n in range(K+1):
    res = (res + comb * M * pow(M-1, N-1-n, p)) % p
    comb = (comb * (N -1 -n) * pow(n + 1, p - 2, p)) % p
    
print(res)
F As the explanation is as it is, please watch the explanation video. .. .. Replace (,) with each ± number and record the sum and minimum of each query. What to think about
--Whether the sum of all positive sums is equal to the sum of all negative sums -Whether it does not exceed 0 on the way
The latter is, for example, ())) ((A query like (the sum is +1 but the minimum value is -2", so you can't create a parenthesis string without two or more (to the left).
# F
N = int(input())
list_plus = []
list_minus = []
for i in range(N):
    S = input()
    state = 0
    min_state = 0
    for s in S:
        if s == '(':
            state += 1
        else:
            state -= 1
        min_state = min(min_state, state)
    
    if state > 0:
        list_plus.append((min_state, state))
    else:
        list_minus.append((min_state - state, -state))
def compute(arr):
    total_state = 0
    for min_state, state in arr[::-1]:
        if total_state + min_state < 0:
            print('No')
            exit()
        total_state += state
    return total_state
list_plus.sort()
total_state_plus = compute(list_plus)
list_minus.sort()
total_state_minus = compute(list_minus)
print('Yes' if total_state_plus == total_state_minus else 'No')
In the code of the F problem, append ((min_state, state)) was used, but append ([min_state, state]) resulted in TLE. There was a description of this in Chapter 3 of High Performance Python, but it was a good opportunity to realize that the speed will change considerably.
Recommended Posts