[PYTHON] AtCoderBeginnerContest164 Review & Summary (first half)

AtCoder ABC164 This is a summary of the AtCoder Beginner Contest 164 questions that were held on 2020-04-26 (Sun), starting with question A and taking into consideration the considerations. The first half deals with problems up to ABC. The problem is quoted, but please check the contest page for details. Click here for the contest page Official commentary PDF

Problem A Sheep and Wolves

Problem statement There are $ S $ sheep and $ W $ wolves. When the number of wolves is greater than or equal to the number of sheep, the sheep will be attacked by the wolves. Output "unsafe" if the sheep is attacked by the wolf, "safe" if not.

If you use the if statement, you're done. I don't write much else, but if I dare to write a little, the condition that is originally attacked is written using "** or more **", so I can write the code as it is, but in the past I wrote a comparison operator From the experience of writing "= <" and having an accident, I often consciously avoid the above and below. The correct answer is "<=". I don't know if I made a problem like a human wolf or if I liked it from the beginning because human wolf etc. are popular among new students on SNS without starting university in Corona, but "Yes" or "No" I personally like the problem (I really care about misspellings, case letters, etc.).

abc164a.py


s, w = map(int, input().split())
if s > w:
    print("safe")
else:
    print("unsafe")

B problem Battle

Problem statement Takahashi and Aoki fight monsters. Takahashi-kun's monster has $ A $ health and $ B $ attack power. Aoki's monster has $ C $ health and $ D $ attack power. Attack in the order of Takahashi-kun → Aoki-kun → Takahashi-kun → Aoki-kun → ... Attack means reducing the health value of the opponent's monster by the amount of the attack power of your monster. If you continue to do this until one of the monsters has 0 or less health, the one who has 0 or less health first loses, and the other one wins. Output "Yes" if Takahashi wins and "No" if he loses.

For the time being, I will post the code I submitted first.

Addendum: Python does not require () for while, so you can write while True:, but I often write it with () attached due to the habit of other languages (sweat).

abc164b.py


a, b, c, d = map(int, input().split())
while(True):
    c = c - b
    if c <= 0:
        print("Yes")
        break
    a = a - d
    if a <= 0:
        print("No")
        break

Of course, if the physical strength of $ A $ and $ C $ is extremely large, I can't do a loop, but since the constraint of this problem was an integer of 100 or less, I wrote the code exactly as the problem sentence without hesitation. It was. I've been told a lot on Twitter, but I don't regret it (think of mistakes as WA). Of course, the following code ran through my head, so I rewrote it after the contest was over.

abc164b.py


a, b, c, d = map(int, input().split())
if (c + b - 1) // b <= ( a + d - 1) // d:
    print("Yes")
else:
    print("No")

It's a little troublesome because it is necessary to round up after the decimal point, so the calculation formula will be a little clumsy, but ~~ If you write in production, it is safer to use ceil in the math module ~~.

Postscript: As commented by @DaikiSuyama, it seems safer not to use the ceil function because it seems that errors may occur. I wrote "** safe **" because I didn't want the formula to be complicated, but it wasn't safe. I have confirmed that this problem will pass, but I was told that ABC046C did not pass due to an error. These comments are really helpful. Thank you, @DaikiSuyama. I also have to solve the past questions properly.

abc164b.py


import math
a, b, c, d = map(int, input().split())
if math.ceil(c / b) <= math.ceil(a / d):
    print("Yes")
else:
    print("No")

C problem gacha

Problem statement I did a lottery $ N $ times, and at the $ i $ time I got a free gift whose type is represented by the string $ S_i $. How many kinds of prizes did you get?

If you use the dictionary type, you're done. I was lucky because I used it all up in natural language processing. If you use len, python is really convenient because it returns not only the list but also the number of keys in the dict. When you usually count the frequency of occurrence of words

python


word_dict = {}
for word in word_list:
    if word in word_dict
        word_dict[word] += 1
    else:
        word_dict[word] = 1

However, I didn't use this question many times, so I could write it easily.

Postscript: I am using dict this time, but as the official explanation says, it can also be implemented with set and map.

abc161c.py


n = int(input())
dict_s = {}
for i in range(0, n):
    s = input()
    dict_s[s] = 1
print(len(dict_s))

This is the end of the first half. The second half will explain the DEF problem. After solving ABC, I got stuck in this contest and tried and errored, but in the end it ended without "AC", and I regret that it was a comparison of the speed of sprinting to the C problem. After all, I want to get out of the foot race by asking about one difficult problem. Thank you for reading to the end of the first half for the time being. Continued in the second half.

Recommended Posts

AtCoderBeginnerContest175 Review & Summary (first half)
AtCoderBeginnerContest164 Review & Summary (first half)
AtCoderBeginnerContest169 Review & Summary (first half)
AtCoderBeginnerContest174 Review & Summary (first half)
AtCoderBeginnerContest173 Review & Summary (First Half)
AtCoderBeginnerContest165 Review & Summary (first half)
AtCoderBeginnerContest170 Review & Summary (first half)
AtCoderBeginnerContest167 Review & Summary (first half)
AtCoderBeginnerContest177 Review & Summary (first half)
AtCoderBeginnerContest168 Review & Summary (first half)
AtCoderBeginnerContest178 Review & Summary (first half)
AtCoderBeginnerContest171 Review & Summary (first half)
AtCoderBeginnerContest166 Review & Summary (first half)
AtCoderBeginnerContest161 Review & Summary (first half)
AtCoderBeginnerContest172 Review & Summary (first half)
AtCoderBeginnerContest176 Review & Summary (first half)
AtCoderBeginnerContest178 Review & Summary (second half)
AtCoderBeginnerContest161 Review & Summary (second half)
AtCoderBeginnerContest164 Review & Summary (second half)
AtCoderBeginnerContest176 Review & Summary (second half)
AtCoderBeginnerContest168 Review & Summary (second half)
AtCoderBeginnerContest169 Review & Summary (second half)
AtCoderBeginnerContest166 Review & Summary (second half)
AtCoderBeginnerContest171 Review & Summary (second half)
AtCoderBeginnerContest174 Review & Summary (second half)
AtCoderBeginnerContest173 Review & Summary (second half)
AtCoderBeginnerContest177 Review & Summary (second half)
AtCoderBeginnerContest180 Review & Summary
AtCoderBeginnerContest181 Review & Summary
AtCoderBeginnerContest182 Review & Summary
AtCoderBeginnerContest183 Review & Summary
AtCoderBeginnerContest179 Review & Summary
Django Girls Tutorial Summary First Half
AtCoder Review of past questions (first half of 12 / 8,9)