Review of AtCoder Beginner Contest 155, 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 - Poor

The question is to answer if there are two equal numbers out of the three given numbers.

You can simply compare them. This problem can be rephrased as being asked "Is there two types of elements?", So let's investigate that. It seems convenient to use a set type object to remove duplicate elements in an array.

A = list(map(int, input().split()))
if len(set(A)) == 2:
  print('Yes')
else: print('No')

B - Papers, Please

The question is to find out if the elements of a given array meet the condition that "if even, it is divisible by 3 or 5".

Examine all the elements and reject if they are even but not divisible by either.

N = int(input())
A = list(map(int, input().split()))

for a in A:
  if a%2 == 0 and not (a%3 == 0 or a%5 == 0):
    print('DENIED')
    break
else:
  print('APPROVED')

C - Poll

Since some character strings are given, it is a problem to output the character string with the highest number of occurrences (if the number is the same, all in dictionary order).

Store strings in an array. Aggregate using collections.Counter. Take the maximum value with max (), and put only the character string with the maximum number of occurrences into one array. I rearranged this with sorted () and output it.

import collections
N = int(input())
S = [input() for _ in range(N)]
count = collections.Counter(S)
maxV = max(count.values())
c = [k for k, v in count.items() if v == maxV]
print('\n'.join(sorted(c)))

D - Pairs

When all the products of pairs made from a given array are arranged in order, the question is what is the K-th smallest value.

I do not know. I gave up. Refer to other answers.

The following code is written to make it easier for you to understand, referring to most of the other answers. I have explained as much as possible in the comments, but honestly there are many parts that I do not understand well. I'm not sure if the comment is correct (especially the part with?).

import numpy as np
N, K = map(int, input().split())
A = np.array(list(map(int, input().split())))
A = np.sort(A)

G = A[A > 0]
Z = A[A == 0]
L = A[A < 0]

l, r = 10**18, -10**18

while l-r > 1:
  #Check "the number of pairs whose product is m or less".
  m = (l+r) // 2

  # A[A > 0]Number of items that meet the conditions?
  Pk = np.searchsorted(A, m//G, side="right").sum()

  # A[A < 0]Number of items that meet the conditions?
  Nk = (N - np.searchsorted(A, (-m-1)//(-L), side="right")).sum()

  # A[0]Number of items that meet the conditions?
  Zk = 0
  if m >= 0:
    Zk += len(Z) * N

  #Since the product of the same elements cannot be selected, reduce it if the conditions are met.
  duplicate = np.count_nonzero(A*A <= m)

  #Match the number of elements that meet the conditions
  k = Pk + Nk + Zk - duplicate
  
  #All elements are doubled. Remove duplicate elements
  k //= 2

  #If the number of elements k that satisfy the condition is K or more, m is lowered, and if it is less than K, m is raised.
  if k >= K:
    l = m
  else:
    r = m
#If l and r match, m is uniquely determined
print(l)

I passed by this.

I don't understand the following part at all. I was wondering why I could find the number that meets the conditions, but I gave up.

  # A[A > 0]Number of items that meet the conditions?
  Pk = np.searchsorted(A, m//G, side="right").sum()

  # A[A < 0]Number of items that meet the conditions?
  Nk = (N - np.searchsorted(A, (-m-1)//(-L), side="right")).sum()

E - Payment

It is a problem to think about how to pay the money so that the "total of the number of sheets to be put out and the number of changes" is the smallest. However, the cash in this world is only in the $ 10 ^ n $ unit.

Let's count from the bottom. If the number of sheets to be paid is 5 or less, it is more efficient to pay as it is, and if it is 6 or more, it is more efficient to move up to the next digit and receive the change.

Bad guy.py


N = list(map(int, input()))
N = N[::-1] + [0]

count = 0

for i, n in enumerate(N):
  if n <= 5:
    count += n
  elif n > 5:
    count += 10 - n
    N[i+1] += 1
    
print(count)

This is WA. With the above code, there are some omissions. For example, if you want to pay $ 95 $ yen, it is more efficient to pay $ 100 $ yen and receive the change (6 cards in total). At this rate, I paid $ 105 $ for a total of 7 cards.

The condition branches further only when 5 is issued. If the next digit is 5 or more, you can reduce the change by one by moving up.

The one who passed.py


N = list(map(int, input()))
N = N[::-1] + [0]

count = 0

for i, n in enumerate(N):
  if n < 5:
    count += n
  elif n > 5:
    count += 10 - n
    N[i+1] += 1
  elif n == 5:
    if N[i+1] >= 5:
      N[i+1] += 1
    count += 5
    
print(count)

Looking at the explanation, the solution was different.

We will check the number of sheets in order from the top. You can calculate the carry-up by asking for the amount of money "when you pay exactly" and "when you pay one more".

I will write according to the explanation.py


N = list(map(int, input()))

m = 0 #Minimum number of sheets
m_ = 1 #Secure the number of sheets at the time of carry-up, always thinking that n is one more

for n in N:
  m, m_ = min(m + n, m_ + 10-n), min(m + (n+1), m_ + 10-(n+1))
print(m)

How simple is it to write?

That's all for this article.

Recommended Posts

Review of AtCoder Beginner Contest 159, 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 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 072 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 127 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 123 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 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
AtCoder Beginner Contest 053 Review of past questions
AtCoder Beginner Contest 063 Review of past questions
AtCoder Beginner Contest 107 Review of past questions