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

Friends times.

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 - Serval vs Monster

It is a question to answer the number of attacks required to reduce the physical strength H with the attack power A.

The answer is to round up H / A. I used math.ceil () for the round-up process.

import math
H, A = map(int, input().split())
print(math.ceil(H/A))

By the way, I heard that math.ceil () can cause an error when the number is too large. It's okay to write something like (H + A-1) // A, but I didn't like it, so I looked it up lightly.

The following writing method introduced in this article may be smart.

H, A = map(int, input().split())
print(-(-H//A))

How to write -(-H // A). Truncation processing with a negative number seems to be the same processing as rounding up, such as -0.1 -1. By the way, I've seen it in other answers. Should I use this as much as possible in the future?

B - Common Raccoon vs Monster

It is a question to answer whether you can cut off your physical strength $ H $ by making full use of the power $ A_i $ technique.

If the total value of the special move is greater than or equal to the physical strength,'Yes' is output.

H, N = map(int, input().split())
A = list(map(int, input().split()))
if H <= sum(A):
  print('Yes')
else:
  print('No')

C - Fennec vs Monster

When defeating monsters by making full use of the techniques of "doing 1 damage" (unlimited number of times) and "killing instantly" (up to K times), it is a problem to answer the number of normal attacks.

Arrange the HP of monsters in order, and exclude K monsters in descending order. The answer is the total number of remaining HP.

Note that K may be more frequent than N (one loss).

N, K = map(int, input().split())
H = list(map(int, input().split()))

H.sort()

print(sum(H[:max(N-K, 0)]))

Postscript: Taking a negative value for the array index in python will behave specially.

a = [0, 1]
print(a[2:]) #The protruding index is passed through
# []
print(a[-1:]) #Overhanging indexes are counted in the opposite direction
# [1]

I added the processing of max to avoid this specification, but if you write it as follows, you don't need to compare N and K.

N, K = map(int, input().split())
H = list(map(int, input().split()))

H.sort(reverse=True)

print(sum(H[K:]))

D - Caracal vs Monster

It is a problem to answer the number of attacks required for a monster that splits (truncated) by halving HP when attacking.

Replace numbers

First, HP always truncates, so even if the value $ H $ given is converted to $ 2 ^ {k-1} $ (but $ 2 ^ {k-1} \ leq H <2 ^ k $) It will be the same number of times.

Think about the number of attacks

So let's count the number of attacks against monsters with $ 2 ^ {k-1} $ HP.

First, attack a monster with HP of $ 2 ^ {k-1} $. This is once.

Next, two monsters with HP of $ 2 ^ {k-2} $. This is twice.

Next is a $ 2 ^ 2 $ monster with $ 2 ^ {k-3} $, this is $ 2 ^ 2 $ times.

...

This is repeated, and when you finally hit a $ 2 ^ {k-1} $ monster with HP1 for $ 2 ^ {k-1} $ times, you win.

Let's make it a mathematical formula.

x = 1 + 2 + 2^2 + \cdots + 2^{k-1}
= 2^k - 1

If you want to follow the conversion here properly, please use the formula of the sum of geometric progressions.

so

You can get the answer by "replace the value with $ 2 ^ {k-1} $" and "output $ 2 ^ {k} -1 $". I wrote the code below.

H = int(input())

k = 0
while H:
  H //= 2
  k += 1
print(2**k-1)

E - Crested Ibis vs Monster

The magic power $ A_i $ and the consumption magic power $ B_i $ are passed. It is a question to answer as little magic power as possible to scrape off the given H.

I submitted it in a simple DP and it passed. Just store the minimum magic power consumed for each HP value in the array and update the minimum value in order. I'm getting used to dynamic programming.

H, N = map(int, input().split())

magic = [tuple(map(int, input().split())) for _ in range(N)]

DP = [0] + [10**9]*H

for h in range(H):
  for damage, mp in magic:
    DP[min(h + damage, H)] = min(DP[min(h + damage, H)], DP[h] + mp)
print(DP[H])

However, only PyPy3 passed this, and TLE came out in Python3. Looking at what happened with python, there was an answer that speeded up by using comprehension notation. I will rewrite it.

H, N = map(int, input().split())

magic = [tuple(map(int, input().split())) for _ in range(N)]

DP = [0] * (H+10**4)

for h in range(1, H+1):
  DP[h] = min(DP[h - damage] + mp for damage, mp in magic)
print(DP[H])

Note that the meaning of h, which is turned by for, is different from the previous code. Now it passed in python.

That's all for this article. Even though it was a past contest, I am happy that it was the first time I was able to solve the E question on my own.

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 072 Review of past questions
AtCoder Beginner Contest 085 Review of past questions
AtCoder Beginner Contest 062 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 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 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 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
AtCoder Beginner Contest 092 Review of past questions
AtCoder Beginner Contest 099 Review of past questions
AtCoder Beginner Contest 065 Review of past questions