LoginSignup
0
0

More than 1 year has passed since last update.

【AtCoder】ABC206をPython3で解説

Last updated at Posted at 2021-06-20

ABC206の解説。

A - Maxi-Buying

解説

Nの消費税込みの値段を、206円と比較してどうなるかという問題。

Nにそのまま1.08をかけると、0以下の数字が表示されてしまうため、p = N * 108 // 100として、表示させないようにすれば良い。

あとはifで場合分けをすると完成。

コード

N = int(input())

p = N * 108 // 100

if p < 206:
    print('Yay!')
elif p == 206:
    print('so-so')
else:
    print(':(')

B - Savings

解説

i日目の朝にi円を入れるという問題。

whileを用いて、daytotalに足していく。足していく中でtotalN以上となれば、whileループを抜け、dayを出力する。

また、while True:として、breakを用いる方法もある。

コード1

# while 条件文:
N = int(input())

day = 0
total = 0

while total < N:
    day += 1
    total += day

print(day)

コード2

# while True:
N = int(input())
day = 0
total = 0

while True:
    day += 1
    total += day

    if total >= N:
        print(day)
        break

C - Swappable

解説

N個の数列からなる配列Aのうち、条件を満たす$(i, j)$の数を求めるという問題。

今回の条件では、「条件を満たすものを数える」よりも、「条件を満たさないものを数えて、全体から引く」という操作のほうが、計算量を少なく計算できる。

また、重複した値を数えなければならないので、dict()を使う。

今回、A = [1, 2, 1, 3, 2]の場合を考える。

まず、for i in range(N):Aの要素数分、ループを回す。
if A[i] not in cnt.keys():で、設定した辞書cnt = {}の中に値がなければ、値を数えるために、0を追加しておく必要がある。

そして、現時点のcntに含まれている数であるiから、重複のある$i = j$の数を引いてあげることで、現時点での今回の条件を満たす数を求めることができる。

これをansに加えることによって、最終的な条件を満たす整数組の数を求めることができる。
cnt[A[i]] += 1は、同じ数が何個あるのか数える操作である。

コード

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

cnt = {}
ans = 0

for i in range(N):
    if A[i] not in cnt.keys():
        cnt[A[i]] = 0

    ans += i - cnt[A[i]]
    cnt[A[i]] += 1

print(ans)

編集後記

もう少しC問題を頭良く解けたらなーと感じた今日このごろ。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0