[AtCoder explanation] Control ABC164 A, B, C problems with Python!

I will explain how to solve ** A, B, C problem ** of ** AtCoder Beginners Contest 164 ** with ** Python 3 ** as carefully as possible.

I am aiming to explain a solution that satisfies the following three points, not just a method that can be solved.

--Simple: You don't have to think about anything extra --Easy to implement: I'm glad that mistakes and bugs are reduced ――It doesn't take long: The performance goes up and the time left for the D problem increases.

ABC164

Date: 2020-04-26 (Sun) 21:00 ~ 2020-04-26 (Sun) 22:40 (100 minutes) A Number of people submitting questions: 11236

Performance AC time Ranking Guideline
400 ABC- 29 minutes 7198th Tea performance
600 ABC- 18 minutes 5854th Brown rate at 8 times
800 ABC- 12 minutes 4425th Green performance
1200 ABC- 5 minutes 2081 Water performance

(Reference) I: Performance 1625 ABCD-- 23:03 80 5th place </ font>

A problem (11148 people AC)
Just do as you write. The sample is also kind.
B problem (10415 AC)
It's easier not to get confused if the variable name is easy to understand.
C problem (9533 people AC)
There are various ways, but if you use the set type, it will be one shot.
D problem (1926 AC) [not explained in this article]
This is the difficulty level of the usual E problem level. It can't be helped if you can't solve it.

The D problem was quite difficult, so just solving the C problem quickly gave me a water performance. I think you felt the importance of doing it in a fast and easy way.

ABC158A 『Sheep and Wolves』

** Problem page **: A --Sheep and Wolves ** Difficulty **: ☆☆☆☆☆ ** Point **: Handling of if statements, handling of boundary values

It's a simple matter of doing exactly as you write it.

Problem statement

Problem statement (folding)
![abc164a.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/595910/833c91bd-e833-edce-d1e8-2e9f532e4984.png)

How to solve

  1. Decipher the problem statement

Step 1: Read the question

'Unsafe' when the number of wolves is ** "greater than or equal to" **, otherwise it is 'safe'.

When judging by ʻif, be careful about handling the boundary value (the number of the boundary where the judgment changes). ** "More than" **, so w> = s. If the number of wolves is ** "more" ** than the number of sheep, then w> s`.

Kindly, sample 3 is 'unsafe' with $ W = S $, so if this passes, you can submit with confidence.

code

Just do it. Be careful not to reverse ʻunsafe and safe`, or make typos or spelling mistakes. One mistake can result in a penalty of 5 minutes, so even if it takes some time, be sure to check that it fits all the samples before submitting.

s, w = list(map(int, input().split()))

if w >= s:
    print("unsafe")
else:
    print("safe")

If you want to write in one line, you can use "ternary operator". I don't use it because it seems to cause mistakes.

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

ABC164B『Battle』

** Problem page **: B --Battle ** Difficulty **: ★ ☆☆☆☆ ** Point **: Handling while, devising variable names

It is a simple game that you can reduce the opponent's HP by the same amount as your own attack power when you attack. Takahashi is the first player and Aoki is the second player.

Problem statement

Problem statement (folding)
![abc164b.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/595910/6d9fd7d6-4213-6b43-6fc9-b55fefbea2f6.png)

How to solve

  1. Think about what to do

Step 1: Think about what to do

Since $ A, B, C, D $ are all 100 or less, it can be solved by simulating as it is. If it is a very huge number such as 5000 trillion (dead language), it will be calculated by arithmetic. (It doesn't really matter, but there is actually a problem with 5000 trillion in the problem statement)

If the variable name is changed to ʻa, b, c, d` as it is, it will be difficult to understand which variable is what and it will be a source of confusion. I was confused during the contest and ended up reading the question sentence about 5 times.

Let's say tkhp, tkat, ʻaohp, ʻaoat.

code

Just write this too. For such a simulation, use while True to make an infinite loop, and if the conditions are met, use break to exit.

tkhp, tkat, aohp, aoat = list(map(int, input().split()))

while True:
    aohp -= tkat
    if aohp <= 0:
        print("Yes")
        break

    tkhp -= aoat
    if tkhp <= 0:
        print("No")
        break

By the way, if you leave the variable name as the problem statement, it will be the code below. It's a code that seems to induce mistakes. You can see the importance of variable names! (Self-discipline)

while True:
    c -= b
    if c <= 0:
        print("Yes")
        break

    a -= d
    if a <= 0:
        print("No")
        break

ABC164C『gacha』

** Problem page **: C --gacha ** Difficulty **: ★★ ☆☆☆ ** Type **: Utilization of set type

You can easily understand the meaning of the problem statement. The fun depends on how you implement it.

Problem statement

Problem statement (folding)
![abc164c.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/595910/6709cc42-fa2f-4b76-fe49-27305eb75547.png)

things to do

  1. Think about how to ask

Step 1: Think about how to ask

There are various ways to find the number of types, but the set type is the easiest.

If you normally receive the input as a list type and then make it a set type, the duplicate elements (double) will disappear. Since the number of elements of set type is the number of types itself, you can find the answer by using the len () function.

I don't think you can understand it even if it is said in words. There is nothing difficult if you look at what will happen.

>>> s = ['apple', 'orange', 'apple']
>>> s_set = set(s)
>>> s_set
{'orange', 'apple'} #There are two'apple'Became one
>>> len(s_set)
2 # 'orange'When'apple'There are two types.
>>> s = ['grape', 'grape', 'grape', 'grape', 'grape']
>>> s_set = set(s)
>>> s_set
{'grape'} #There are 5'grape'Became one
>>> len(s_set)
1 # 'grape'It is one type of.

code

n = int(input())
s = [input() for _ in range(n)] #I'm receiving input in a list comprehension that looks neat. The same is true with the for loop.

ss = set(s) #Create a set type of s. This is a bad variable name, but it's a short code so I think it's fine.
ans = len(ss) #Now you can see the number of types.
print(ans)

Excluding input, there are only 3 lines. If you use print (len (set (s))), it will be one line, but if you are not used to it, try to do only one line per line.

The technique of converting to set type and finding the number of types with len is really often used, so be sure to remember it.

Bonus (about Counter)

It's not relevant to this issue, but you often want to know how often each element appears.

In such a case, it is easier to write from collections import Counter and use the Counter type. I use it as often as the set type.

I will not explain how to use it this time, so if you want to know it, please check it out. To briefly explain what it looks like, it is a convenient counter with various functions added to the dict type.

Recommended Posts

[AtCoder explanation] Control ABC180 A, B, C problems with Python!
[AtCoder explanation] Control ABC188 A, B, C problems with Python!
[AtCoder explanation] Control ABC158 A, B, C problems with Python!
[AtCoder explanation] Control ABC164 A, B, C problems with Python!
[AtCoder explanation] Control ABC168 A, B, C problems with Python!
[AtCoder explanation] Control the A, B, C problems of ABC182 with Python!
[AtCoder explanation] Control the A, B, C problems of ABC187 with Python!
[AtCoder explanation] Control the A, B, C problems of ABC184 with Python!
[AtCoder explanation] Control the A, B, (C), D problems of ABC165 with Python!
[AtCoder explanation] Control the A, B, C, D problems of ABC181 with Python!
ABC127 A, B, C Explanation (python)
ABC126 A, B, C Explanation (python)
Challenge AtCoder (ABC) 164 with Python! A ~ C problem
Solve Atcoder ABC176 (A, B, C, E) in Python
Solve ABC163 A ~ C with Python
Solve ABC168 A ~ C with Python
Solve ABC162 A ~ C with Python
Solve ABC167 A ~ C with Python
ABC128 A, B, C commentary (python)
Solve ABC158 A ~ C with Python
Solve ABC175 A, B, C in Python
[AtCoder] Solve ABC1 ~ 100 A problem with Python
Solve AtCoder ABC168 with python (A ~ D)
[AtCoder] Solve A problem of ABC101 ~ 169 with Python
AtCoder ABC 177 Python (A ~ E)
Solve AtCoder ABC166 with python
AtCoder ABC 178 Python (A ~ E)
ABC129 A, B, C commentary
AtCoder ABC 176 Python (A ~ E)
AtCoder ABC 182 Python (A ~ D)
ABC188 C problem with python3
ABC187 C problem with python
AtCoder Beginner Contest 166 A Explanation of Problem "A? C" (Python3, C ++, Java)
AtCoder Beginner Contest 174 B Problem "Distance" Explanation (C ++, Python, Java)
AtCoder Beginner Contest 177 B Problem "Substring" Explanation (Python3, C ++, Java)
Solve ABC166 A ~ D with Python
AtCoder Beginner Contest 167 A Problem "Registration" Explanation (Python3, C ++, Java)
ABC166 in Python A ~ C problem
Solve ABC036 A ~ C in Python
Solving with Ruby and Python AtCoder ABC011 C Dynamic programming
Template AtCoder ABC 179 Python (A ~ E)
Solve ABC037 A ~ C in Python
AtCoder Beginner Contest 169 B Problem "Multiplication 2" Explanation (Python3, C ++, Java)
AtCoder Beginner Contest 170 A Problem "Five Variables" Explanation (C ++, Python, Java)
[AtCoder commentary] Win the ABC165 C problem "Many Requirements" with Python!
AtCoder Beginner Contest 169 A Explanation of Problem "Multiplication 1" (Python3, C ++, Java)
AtCoder Beginner Contest 176 A Explanation of problem "Takoyaki" (Python3, C ++, Java)
AtCoder Beginner Contest 175 B Problem "Making Triangle" Explanation (C ++, Python3, Java)
AtCoder Beginner Contest 175 A Problem "Rainy Season" Explanation (C ++, Python3, Java)
Solving with Ruby, Python and numpy AtCoder ABC054 B Matrix operation
Solving with Ruby, Perl, Java, and Python AtCoder ABC 065 C factorial
AtCoder Beginner Contest 176 B Problem "Multiple of 9" Explanation (Python3, C ++, Java)
AtCoder Beginner Contest 174 A Problem "Air Conditioner" Explanation (C ++, Python, Java)
[Python] [Explanation] AtCoder Typical DP Contest: A Contest
python> keyword arguments> hoge (** {'a': 1,'b': 2,'c': 3})
Solve ABC165 A, B, D in Python
AtCoder ABC187 Python
AtCoder ABC188 Python
AtCoder ABC 175 Python
AtCoder Beginner Contest 177 A Problem "Don't be late" Explanation (Python3, C ++, Java)
Solving with Ruby, Perl, Java and Python AtCoder ABC 107 B String Manipulation