Solve AtCoder 167 with python

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')

Impressions

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

Solve AtCoder 167 with python
Solve AtCoder ABC 186 with Python
Solve AtCoder Problems Recommendation with python (20200517-0523)
Solve Sudoku with Python
Solve POJ 2386 with python
[AtCoder] Solve ABC1 ~ 100 A problem with Python
Solve AtCoder ABC168 with python (A ~ D)
[Python] Solve equations with sympy
[AtCoder] Solve A problem of ABC101 ~ 169 with Python
Light blue with AtCoder @Python
atCoder 173 Python
Solve "AtCoder version! Ant book (beginner)" with Python!
solver> Link> Solve Excel Solver with python
Solve ABC166 A ~ D with Python
Solve Atcoder ABC169 A-D in Python
Solve ABC168 A ~ C with Python
Solved AtCoder ABC 114 C-755 with Python3
Solve ABC162 A ~ C with Python
Solve ABC167 A ~ C with Python
Solve ABC158 A ~ C with Python
Statistics with python
Scraping with Python
AtCoder ABC 174 Python
Python with Go
Solve with Ruby and Python AtCoder ABC133 D Cumulative sum
Twilio with Python
AtCoder ABC187 Python
Integrate with Python
Play with 2016-Python
AES256 with python
Tested with Python
AtCoder ABC188 Python
python starts with ()
with syntax (Python)
Bingo with python
Zundokokiyoshi with python
AtCoder ABC 175 Python
Solve AtCoder Problems Boot camp for Beginners (Medium 100) with python
Excel with Python
Microcomputer with Python
Cast with python
I wanted to solve ABC160 with Python
[Python] Solve 10 past elite problems of Atcoder
AtCoder Green tried to solve with Go
Solve Lake Counting (POJ NO.2386) with Python3
I wanted to solve ABC172 with Python
Solve with Ruby, Python and Java AtCoder ARC104 B Cumulative sum
Daily AtCoder # 2 in Python
Zip, unzip with python
Django 1.11 started with Python3.6
Primality test with Python
Python with eclipse + PyDev.
Daily AtCoder # 6 in Python
Daily AtCoder # 18 in Python
Scraping with Python (preparation)
Python hand play (let's get started with AtCoder?)
Daily AtCoder # 53 in Python
Try scraping with Python.
Daily AtCoder # 33 in Python
Solve ABC168D in Python
Learning Python with ChemTHEATER 03