Review of AtCoder Beginner Contest 159, up to question E (Python)

This is a review article for beginners of competition professionals.

The solution I write here is written while looking at the commentary and other people's submissions. It may not be what you actually submitted.

A - The Number of Even Pairs The question is to answer the number of combinations of N balls with even numbers and M balls with odd numbers, for which the total is even.

The answer is simply to combine the even-numbered combination $ N (N-1) / 2 $ and the odd-numbered combination $ M (M-1) / 2 $.


N, M = map(int, input().split())
print((N*(N-1))//2 + (M*(M-1))//2)

B - String Palindrome

It is a problem to judge whether the given character string is a "palindrome that combines two palindromes".

Whether the four character strings of the first half on the left side, the second half on the left side, the first half on the right side, and the second half on the right side match is determined by turning up to 1/4 of the character string N.

s = input()
n = len(s)
for i in range((n-1+3)//4):# +3 is for rounding up
    if not(s[i] == s[(n-1)//2-i-1] and s[(n+3)//2 + i-1] ==  s[-i-1] and s[i] == s[-i-1]):
        print('No')
        exit()
print('Yes')

When I looked at other answers and explanations, there was a cleaner way to write.

s = input()
n = len(s)
sl = s[:n//2]
sr = s[n//2+1:]
if sl == sr and sr ==  sr[::-1]:
    print('Yes')
else:
    print('No')

Divide the character string into left and right, and if the right `sr``` is a palindrome, the sl that matches it is also a palindrome. Since `` `sl and sr are matching palindromes, it is a palindrome even if the two are combined. Only two conditions were required.

C - Maximum Volume The total height, width, and height is the problem of answering the maximum volume of a rectangular parallelepiped of L.

When height = width = height, the volume is the maximum, so the length of each is $ L / 3 $. Just cube this.

L = int(input())
print((L/3)**3)

I couldn't explain why it is the maximum when the lengths of the sides match, so I will write a mathematical explanation for the time being. The following is the formula for the additive geometric mean with three variables. $ {a+b+c\over 3}\geq (abc)^{1/3}$ Considering that the height is $ a $ and the width is $ b $ and $ c $, the volume of the rectangular parallelepiped can be calculated by $ abc . $ ({a+b+c\over 3})^3\geq abc$$ When $ a = b = c $, the equal sign of this formula holds, so it is the maximum volume.

D - Banned K

It is a problem to think about how many combinations to select the same number from N balls with numbers written on them, excluding the balls.

First, find the total number of combinations $ S $ from all the balls. From there, when there are $ m $ balls with the number $ n $, if you take out one ball with the number $ n $, the total $ S $ will be reduced by $ m-1 $. The number $ n $ is applied to each ball to be taken out and output.

import collections
N = int(input())
li = list(map(int, input().split()))
cn = collections.Counter(li)
sumC = sum([n*(n-1)//2 for n in cn.values()])
for k in range(N):
    print(sumC-cn[li[k]] + 1)

E - Dividing Chocolate

It is a question of answering the number of times to divide so that the amount of white chocolate in the chocolate bar is below a certain level.

I gave up because I didn't understand either "the idea of exploration" or "how to count the number of white chocolates in the split".

I saw the commentary. Since there is a narrow range specification of $ H \ leq 10 $, the horizontal division can be straightforward to search all $ 2 ^ {H-1} $ streets. For a full search, use ʻitertools.product ()` to create a complete array of length H-1 containing 0s or 1. Search is OK with this.

Chocolates that are split horizontally are stored in the two-dimensional array SW as a one-dimensional array that counts the number of white chocolates vertically, and are counted from the left. If the number of chocolates in any column exceeds the specified number K, divide the chocolates vertically and return the count to 0. You can also count chocolates with this. However, if the value of the one-dimensional array exceeds K, it is impossible to divide the chocolate many times, so the situation should be redone from the sideways division.

So the code looks like this:

import itertools

H, W, K = map(int, input().split())
S = [input() for _ in range(H)]
ans = 1e4
for t in itertools.product([0, 1], repeat=H-1):
    cnt = t.count(1)
    SW = []
    tmp = [int(s) for s in S[0]]
    for i, c in enumerate(t):
        if c:
            SW.append(tmp)
            tmp = [int(s) for s in S[i+1]]
        else:
            tmp = [tmp[j] + int(S[i+1][j]) for j in range(W)]
    SW.append(tmp)
    H_ = len(SW)
    sums = [0] * H_
    if max(itertools.chain.from_iterable(SW)) > K:
        continue
    for w in range(W):
        sumtmp = [sums[i] + SW[i][w] for i in range(H_)]
        if max(sumtmp) > K:
            cnt += 1
            sums = [SW[i][w] for i in range(H_)]
        else:
            sums = sumtmp
    ans = min(ans, cnt)
print(ans)        

This article ends with question E for the time being.

Recommended Posts

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)
atcoder Review of Panasonic Programming Contest 2020, up to question E (Python)
Review of atcoder ABC158, up to question E (Python)
AtCoder Beginner Contest 102 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 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
AtCoder Beginner Contest 048 Review of past questions
AtCoder Beginner Contest 124 Review of past questions
AtCoder Beginner Contest 116 Review of past questions
AtCoder Beginner Contest 097 Review of past questions
AtCoder Beginner Contest 088 Review of past questions