Review of AtCoder Beginner Contest 166, 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.

Cause of this defeat

Oh, ** Did you do it on Sunday too? ** **

I didn't know the existence of the contest. I didn't see the schedule. That's why I'm solving the problem after the contest is over.

A - A?C

Atcoder seems to hold a contest every Saturday. Next time, I will ask whether ABC or ARC will be held.

If the input is'ABC', output'ARC'. If not, output'ABC'.

S = input()
if S == 'ABC':
  print('ARC')
else:
  print('ABC')

B - Trick or Treat

Each of the K types of sweets will be distributed to children with the number $ \ boldsymbol {A_k} $. The question is how many children have not received the sweets.

I think you should combine the elements of each array, remove the duplication, check the "number of people who were given sweets", and subtract from the total number of people.

N, K = map(int, input().split())
A = []
for _ in range(K):
  d = input()
  A += list(map(int, input().split()))
print(N - len(set(A)))

↓ is a pattern that I tried to thrust into the set type every time. This one was faster.

N, K = map(int, input().split())
A = set()
for _ in range(K):
  d = input()
  A = A.union(set(map(int, input().split())))
print(N - len(A))

C - Peaks

The question is how many observatories meet the condition "higher than all observatories adjacent to you".

It looks like a graph problem ... but you don't have to think about it that hard. False if the adjacent observatory is taller than you. True if you don't have it. Finally, I checked the number of True and output it.

Note that both observatories with "same height" are False.

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

good = [True] * N

for _ in range(M):
  A, B = map(int, input().split())
  A -= 1
  B -= 1
  if H[A] >= H[B]:
    good[B] = False
  if H[A] <= H[B]:
    good[A] = False
print(sum(good))

D - I hate Factorization

A^5 - B^5 = X

It is a problem to output one set of integers (A, B) that satisfies.

How much should the $ A and B $ ranges to meet the $ X \ leq 10 ^ 9 $ limit? What is the number that exceeds $ 10 ^ 9 $ when multiplied by 5?

n = 1
while n**5 < 10**9:
  n += 1
print(n)
# 64

I will try to search the whole area by taking about twice this range.

import itertools

X = int(input())

for a, b in itertools.product(range(128), range(-128, 128)):
  if(a ** 5 - b ** 5 == X):
    print(a, b)
    break

I went through.

When I saw the commentary,

n^5 - (n-1)^5 \geq 10^9

It seems correct to think of searching up to $ n $, which satisfies $ n = 120 $. The result is okay, but let's think about it properly. After that, as long as this calculation passes, it is also a good idea to search as wide a range as possible.

E - This Message Will Self-Destruct in 5s

"This message disappears automatically after 5 seconds."

The question is to answer the number that satisfies $ A_i + A_j = j-i $.

ʻItertools.combinations_with_replacement ()` covers everything. It became TLE.

import itertools

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

count = sum([j-i == A[i-1]+A[j-1] for i, j in itertools.combinations_with_replacement(range(1, N+1), 2)])

print(count)

I don't know and give up. See the commentary. Is it possible to escape from $ O (n ^ 2) $?

A_i + A_j = j-i
A_i + i = j-A_j

It's done. You just have to calculate an independent status for each value.

For the value $ A_i $, calculate the value $ L_i = i + A_i $, $ R_i = i --A_i $. You can calculate the number of combinations where $ L_i = R_j $ by counting how many values are contained in $ R $ and $ L $ respectively.

That's why I implemented it with the following code.

import collections

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

L = [i + A[i] for i in range(N)]
R = [i - A[i] for i in range(N)]

countL = collections.Counter(L)
countR = collections.Counter(R)

print(sum([countL[n] * countR[n] for n in countL.keys()]))

That's all for this article.

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 160, 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
AtCoder Beginner Contest 053 Review of past questions
AtCoder Beginner Contest 094 Review of past questions