atcoder Review of Panasonic Programming Contest 2020, up to question E (Python)

This is a review article for beginners of competition professionals. Click here for the contests you participated in. https://atcoder.jp/contests/panasonic2020

The code I write here is written while looking at other people's answers and explanations. It was not actually submitted.

A - Kth Term The problem is to output the nth term of the input given from a predetermined sequence.

li = [1, 1, 1, 2, 1, 2, 1, 5, 2, 2, 1, 5, 1, 2, 1, 14, 1, 5, 1, 5, 2, 2, 1, 15, 2, 2, 5, 4, 1, 4, 1, 51]
i = int(input())
print(li[i-1])

Note that the item numbers are counted from 1.

B - Bishop $ H \ times W $ It is a question to answer the range where the corner can move on the board of the square.

Note that the corner cannot move even one step when there is only one horizontal or vertical size (one loss).

The code I submitted looks like this.

import math
H, W = map(int, input().split())
if H == 1 or W == 1:
    print(1)
else:
    w_odd = math.ceil(H/2)
    w_even = H // 2
    N_odd = math.ceil(W/2)
    N_even = W // 2
    print(w_odd * N_odd + w_even * N_even)

The number of squares that can be moved differs between the even-numbered and odd-numbered squares counting from the left. The number of cells in these two cases and the number of columns corresponding to each were calculated separately.

You can actually write it more concisely.

Simply half of all the squares are movable. If you divide by 2 and there is a remainder, the lower right cell that is the remainder is always in the (odd, odd) position. Since it is a movable position, it will increase by one more square.

H, W = map(int, input().split())
if H == 1 or W == 1:
    print(1)
else:
    print((H * W + 1) // 2)

Also, there is no need to import math into the round-up process.

C - Sqrt Inequality For input A B C

\sqrt{A} + \sqrt{B} < \sqrt C \tag{1}

The problem of determining if.

I submitted the following, thinking that it would be useless.

a, b, c = map(int, input().split())
if a**0.5 + b**0.5 < c**0.5:
    print("Yes")
else:
    print("No")

It was bad. You can't even use math. Let's square both sides of equation (1). $A + 2\sqrt{AB} +B < C \tag{2}$ This is also useless. I gave up. I'm wondering if there will be some error in the calculation of the square root, but I don't have that kind of knowledge.

I saw the commentary. (2) is further transformed.

2\sqrt{AB} < C - A - B

When both sides are positive, square both sides $ 4AB < (C - A - B)^2 $ Is established. Now the square root calculation is gone.

So the answer. This should have been noticed as it shouldn't be difficult.

a, b, c = map(int, input().split())
if 0 < c - a - b and 4 * a * b < (c - a - b)**2:
    print("Yes")
else:
    print("No")

D - String Equivalence

The problem is to arrange all the character string patterns that exist with the number of characters N (≤10).

I didn't know how to do it, so I tried to solve it by arranging all the patterns once and then erasing them. Actually, I was stuck in the middle of writing and could not submit it, but I wrote the following code.

import itertools
N = int(input())
alp = [chr(ord('a') + i) for i in range(N)]
allS = list(itertools.product(alp, repeat=N))
answers = []
PatternList = []
for s in allS:
    pattern = []
    letters = []
    for l in s:
        if not l in letters:
            letters.append(l)
        pattern.append(letters.index(l))
    if not pattern in PatternList:
        PatternList.append(pattern)
        answers.append(s)
for answer in answers:
    print(''.join(answer))

I thought it would be possible to reach N = 10, but TLE came out in the second half.

I saw the commentary. The standard form condition is replaced with the following formula. $ s_1 = a $ $ s_k \leq \max[ s_1, \cdots, s_{k - 1}\] + 1$

It seems that you should search with DFS so as to satisfy these two conditions. The maximum value information is stored in the variable mx that holds the maximum value information, and it is passed by a recursive function.

The following is made according to the sample.

n = int(input())

def dfs(s, mx):
    if len(s) == n:
        print(s)
    else:
        for i in range(0, mx + 1):
            c = chr(ord('a') + i)
            if i == mx: dfs(s+c, mx+1)
            else: dfs(s+c, mx)

dfs('', 0)

E - Three Substrings Given the three substrings a, b, and c taken from the string s, this is the problem of guessing the original string s.

I prepared an empty array with the length of a + b + c as the character string s, and started writing with the policy of applying the three of a, b, and c from one end, but there are some elements that get stuck. I gave up because I saw TLE coming out.

I saw the commentary. Get the "relative positions" that can exist for each of a and b, b and c, and a and c as an array. Store this as ab [] `` `, ac [] , `` `bc [] . From there, if the positional relationship between a and b is represented by i and the positional relationship between a and c is represented by `` j```, the positional relationship between b and c is obtained by `ji```. I can do it.

This turns i and `j```, and ```ab [i] and ```ac [j] ```, `` bc [ji] ] `` `It can be obtained by calculating the number of characters when the three conditions are met. Note that a, b and c can be up to $ \ pm 4000 $ respectively.

I just rewrote most of the explanation to Python, but the following code passed. Even with Pypy3, this algorithm is very quick, and the time is just 1991 ms. For example, just put `True``` and False``` in ```ab [] `` instead of 1 and 0 to get a TLE. There seems to be a faster method (I haven't seen it yet), as some people are getting a few times faster calculation time and some are doing this in Python.

a = input()
b = input()
c = input()
A, B, C = len(a), len(b), len(c)
ab, bc, ac = [1] * 16000, [1] * 16000, [1] * 16000

for i in range(A):
    if a[i] == '?':
        continue
    for j in range(B):
        if b[j] != '?' and a[i] != b[j]:
            ab[i-j+8000]= 0
for i in range(B):
    if b[i] == '?':
        continue
    for j in range(C):
        if c[j] != '?' and b[i] != c[j]:
            bc[i-j+8000]= 0
for i in range(A):
    if a[i] == '?':
        continue
    for j in range(C):
        if c[j] != '?' and a[i] != c[j]:
            ac[i-j+8000]= 0
ans = 6000

for i in range(-4000, 4000):
    for j in range(-4000, 4000):
        if ab[i+8000] and bc[j-i+8000] and ac[j+8000]:
            L = min(0, min(i, j))
            R = max(A, max(B+i, C+j))
            ans = min(ans, R-L)
print(ans)

That's all for this article. If possible, I would like to add question F later.

Recommended Posts

atcoder Review of Panasonic Programming Contest 2020, up to question E (Python)
Review of AtCoder Beginner Contest 159, up to question E (Python)
Review of AtCoder Beginner Contest 163, up to question E (Python)
Review of AtCoder Beginner Contest 164, up to question E (Python)
Review of AtCoder Beginner Contest 162, up to question E (Python)
Review of AtCoder Beginner Contest 154, up to question E (Python)
Review of AtCoder Beginner Contest 153, up to question E (Python)
Review of AtCoder Beginner Contest 160, up to question E (Python)
Review of AtCoder Beginner Contest 167, up to question E (Python)
Review of AtCoder Beginner Contest 157, up to question E (Python)
Review of AtCoder Beginner Contest 161, up to question E (Python)
Review of AtCoder Beginner Contest 155, up to question E (Python)
Review of AtCoder Beginner Contest 156, up to question E (Python)
Review of AtCoder Beginner Contest 166, up to question E (Python)
Review of AtCoder Beginner Contest 165, up to question E (Python)
Review of atcoder ABC158, up to question E (Python)
Atcoder Acing Programming Contest Python
I wanted to solve the Panasonic Programming Contest 2020 with Python
AtCoder Panasonic Programming Contest 2020 Participation Report
AtCoder Beginner Contest 102 Review of past questions
AtCoder Beginner Contest 072 Review of past questions
AtCoder Beginner Contest 085 Review of past questions
AtCoder Beginner Contest 062 Review of past questions
AtCoder Beginner Contest 113 Review of past questions
AtCoder Beginner Contest 074 Review of past questions
AtCoder Beginner Contest 051 Review of past questions
AtCoder Beginner Contest 127 Review of past questions
AtCoder Beginner Contest 119 Review of past questions
AtCoder Beginner Contest 151 Review of past questions
AtCoder Beginner Contest 075 Review of past questions
AtCoder Beginner Contest 054 Review of past questions
AtCoder Beginner Contest 110 Review of past questions
AtCoder Beginner Contest 117 Review of past questions
AtCoder Beginner Contest 070 Review of past questions
AtCoder Beginner Contest 105 Review of past questions
AtCoder Beginner Contest 112 Review of past questions
AtCoder Beginner Contest 076 Review of past questions
AtCoder Beginner Contest 089 Review of past questions
AtCoder Beginner Contest 069 Review of past questions
AtCoder Beginner Contest 079 Review of past questions
AtCoder Beginner Contest 056 Review of past questions
AtCoder Beginner Contest 087 Review of past questions
AtCoder Beginner Contest 067 Review of past questions
AtCoder Beginner Contest 093 Review of past questions
AtCoder Beginner Contest 046 Review of past questions
AtCoder Beginner Contest 123 Review of past questions
AtCoder Beginner Contest 049 Review of past questions
AtCoder Beginner Contest 078 Review of past questions
AtCoder Beginner Contest 081 Review of past questions
AtCoder Beginner Contest 047 Review of past questions
AtCoder Beginner Contest 060 Review of past questions
AtCoder Beginner Contest 104 Review of past questions
AtCoder Beginner Contest 057 Review of past questions
AtCoder Beginner Contest 121 Review of past questions
AtCoder Beginner Contest 126 Review of past questions
AtCoder Beginner Contest 090 Review of past questions
AtCoder Beginner Contest 103 Review of past questions
AtCoder Beginner Contest 061 Review of past questions
AtCoder Beginner Contest 059 Review of past questions
AtCoder Beginner Contest 044 Review of past questions
AtCoder Beginner Contest 083 Review of past questions