[PYTHON] [Professional competition practice] I tried At Coder Beginner Selection

I started AtCoder just a week ago, and at that time I tried AtCoder Beginners Selection. Since I was new to competitive programming, I started by learning how to input. I have summarized the answers and what I thought about myself.

PracticeA - Welcome to AtCoder

a = int(input())
b, c = map(int, input().split())
s = input()

print('{} {}'.format(a+b+c,s))

I / O related tests int(input()) map(int, input().split()) Later, as I solved other contests, I learned that I would use these two a lot.

ABC086A - Product

strings = ['Even','Odd']
a, b = map(int, input().split())

print(strings[(a*b)%2])

The question of answering a strange situation.

ABC081A - Placing Marbles

#Placing Marbles
S = list(input())
print(S.count('1'))

The problem of finding the number of digits that is 1 from the input numerical value. I entered it as a character string and judged the number.

ABC081B - Shift only

#Shift only
import numpy as np
N = int(input())
A = np.array(list(map(int, input().split())))
count = 0
check = A%2
while np.count_nonzero(check)==0 :
    count += 1
    A = A/2
    check = A%2

print(count)

The question of how many times the input array is divisible by 2. In numpy array, operators are applied to each element, so numpy receives the input and answers. When I think about it now, I don't think I had to bother to receive a variable called check ...

ABC087B - Coins

#Coins
A = int(input())
B = int(input())
C = int(input())
X = int(input())
count=0
for i in range(A+1):
    yo = X-i*500
    if(yo>=0):
        for j in range(B+1):
            yoi = yo-j*100
            if(yoi>=0):
                for k in range(C+1):
                    yoii = yoi-k*50
                    if(yoii==0):
                        count +=1

print(count)

The number of combinations that make X yen from A 500-yen coins, B 100-yen coins, and C 50-yen coins.

The answer policy is A 100-yen coin loop with a 500-yen coin fixed to 0. In the 100-yen coin loop, the 50-yen coin loop is fixed to 0 100-yen coins. If X yen can be generated with C or less 50-yen coins, return to the higher 100-yen coin loop and use one 100-yen coin as one ... Since the for statement is multiplexed, this was the limit while I wanted to make it easier.

ABC083B - Some Sums

#Some Sums
N, A, B = list(map(int, input().split()))
count = 0
for j in range(N):
    S = str(j+1)
    numN = 0
    for i in range(len(S)):
        numN += int(S[i])
    if A <= numN <= B:
        count += j+1
print(count)

The sum of all integers x where 1 <= x <= N and A <= sum of each digit <= B. I changed the number to str, received each digit as an int, and calculated the sum with a for statement.

Reflections It doesn't need to be a list because it receives 3 inputs with 3 variables. I was able to calculate N from 1 with range (1, N + 1). I was able to utilize the sum method (?)

ABC088B - Card Game for Two

#Card Game for Two
N = int(input())
a = sorted(map(int, input().split()))
count = 0
for i in range(N):
    count += a[-i-1] * (-1)**i
print(count)

Alice and Bob alternately acquire N cards with arbitrary numbers written on them so that the maximum score is achieved. Finally, the problem of finding the score difference between Alice and Bob.

Sort the N cards in descending order, passing the odd numbers to Alice and the even numbers to Bob. ↓ In order to finally obtain the score difference, it is advisable to add the odd numbers and subtract the even numbers. I thought and answered in this way.

ABC085B - Kagami Mochi

import numpy as np
N = int(input())
d = np.zeros(N)
for i in range(N):
    d[i] = int(input())
sorted_d = np.unique(d)
print(len(sorted_d))

Given the diameter of N disks, the question of how many stages can be stacked when they are stacked in descending order from top to bottom.

Disks of the same diameter cannot be stacked. ↓ You can get a unique array by using a function that returns a unique array of Numpy. Find the number of elements. I finally thought and answered.

ABC085C - Otoshidama

#Otoshidama
N, Y = list(map(int, input().split()))
y_man = Y
flag = False
out = [-1,-1,-1]
for i in range(N+1):
    y_gsen = y_man
    for j in range(N-i+1):
        n_sen = int(y_gsen/1000)
        if N-i-j == n_sen:
            out = [i,j,n_sen]
            flag = True
            break
        y_gsen -= 5000
    if flag:
        break
    y_man -= 10000
print('{} {} {}'.format(out[0],out[1],out[2]))
# print(out[0]*10000+out[1]*5000+out[2]*1000)

Whether 10,000-yen bills, 5,000-yen bills, and 1,000-yen bills can be combined into N and Y yen. Answer the combination if possible (-1 -1 -1 if not possible)

I found a way to get rid of the lower for statement when I got out of the lower for statement, but it was difficult to understand and I replaced it by setting a variable called flag.

ABC049C --Daydream

#Hakuchumu
S = input()
m = ['dream','dreamer','erase','eraser']
for i in range(len(m)):
    m[i] = m[i][::-1]
lenS = len(S)
revS = S[::-1]
Header = 0
flag = True
Ans = 'YES'

while Header < lenS-1 :
    flag = True
    if Header + 6 < lenS:
        # print('6t')
        if revS[Header:Header+7]==m[1]:
            Header += 7
            flag = False
            # print('6tt')
    
    if Header + 5 < lenS:
        # print('5t')
        if revS[Header:Header+6]==m[3]:
            Header += 6
            flag = False
            # print('5tt')
    
    if Header + 4 < lenS:
        # print('4t')
        if revS[Header:Header+5]==m[0] or revS[Header:Header+5]==m[2]:
            Header += 5
            flag = False
            # print('4tt')
    if flag:
        Ans = 'NO'
        # print('out')
        break

print(Ans)

The problem of determining whether the character string S can be completed by arranging 4 of'dream',' dreamer',' erase', and'eraser' arbitrarily.

I thought of a method of judging from the beginning of the character string and moving the Header by the number of characters if they match, but I was defeated by the conditional branching of the character string, which is complicated to divide such as dreamerase. The answer is based on the hint that if you invert a string, it will never match.

ABC086C - Traveling

#Traveling
import numpy as np
N = int(input())
Plan = np.zeros((N+1,3))
for i in range(N):
    Plan[i+1] = list(map(int, input().split()))

able = 'Yes'
for j in range(N):
    t, x, y = Plan[j]
    t_next, x_next, y_next = Plan[j+1]
    distance = abs(x-x_next) + abs(y-y_next)
    delta = t_next - t
    amari = delta - distance
    # print(distance,delta,amari)
    if amari < 0 or amari%2 == 1:
        able = 'No'
        break

print(able)

The question is whether it is feasible to have a combination of x_n and y_n at time t_n when moving to a grid point of any one of the top, bottom, left, and right at one time.

First, determine that the next x, y distance is less than t. And, considering that it always moves, it is feasible when too many times are even. If it is an odd number, it is determined that it is unreachable because it must move.

Summary

Looking back now, there were various improvements. Dedicated to the next contest ...

Recommended Posts

[Professional competition practice] I tried At Coder Beginner Selection
[Professional competition] I tried At Coder Beginner Contest 175 (A ~ C)
[Professional competition practice] I tried AtCoder Beginner Contest 171
At Coder (2020/09/08)
[At Coder] Beginner Contest 175 ABCD python Solution introduction
I tried using PyCaret at the fastest speed
I tried to implement selection sort in python