0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

[ABC461] ABC 461(Atcoder Beginner Contest)のA~C(A,B,C)問題をPythonで解説(復習)

0
Posted at

[ABC461] ABC 461(Atcoder Beginner Contest)のA~C(A,B,C)問題をPythonで解説(復習)

A問題

  • 不等号
A.py
"""
<方針>
- 不等号
"""
A, D = map(int, input().split())
if(A<=D):
  print("Yes")
else:
  print("No")

B問題

  • A の値を Bindex で参照すれば良さそう.
B.py
"""
<方針>
- `A` の値を `B` の `index` で参照すれば良さそう.
"""
N = int(input())
A = list(map(int, input().split()))
B = list(map(int, input().split()))

for i, a in enumerate(A):
  if(B[a-1] != (i+1)):
    print("No")
    exit()

# 全部OK
print("Yes")

C問題

方針

  • 種類ごとにまとめて,M 個種類をコンプするようにそれぞれの種類で一番高いやつを選ぶ.
  • あとは残ったやつから高いやつを選べば良い.

前提

  • C問題あたりで,TLEになる人は,制約条件を見る癖をつけよう.
  • AB問題では,基本的に制約条件を見ずにやっても解ける.
  • しかし,C問題以降では,制約条件を見ないと必ずTLEすると思っても良い.
  • 詳しい話は私の352回の記事C問題の解説に記したので,是非参照してほしい.
C.py
"""
<方針>
- 種類ごとにまとめて,`M` 個種類をコンプするようにそれぞれの種類で一番高いやつを選ぶ.
- あとは残ったやつから高いやつを選べば良い.
"""
from collections import defaultdict
# 入力
N, K, M = map(int, input().split())
CVV = defaultdict(list)
for _ in range(N):
  C, V = map(int, input().split())
  CVV[C].append(V)

# 種類ごとに最大価値をheadに入れて,残りをtailに入れる
head = []
tail = []
for _, VV in CVV.items():
  VV.sort(reverse=True)
  head.append(VV[0])
  tail.extend(VV[1:])

# M種類だけheadに残して,残りはtailに移す
head.sort(reverse=True)
tail.extend(head[M:])
tail.sort(reverse=True)

# M種類の高いやつと残りで埋める
ans = sum(head[:M] + tail[:K-M])

# 出力
print(ans)

補足

関係するリンク(参考文献など)

筆者について

その他

  • 間違いを含んでいる可能性があります.
  • 方針と言いつつ,方針ではない感想などを書いている可能性があります.
  • A問題から解説がだんだん省略されます.
  • コードに書かれている解説の文言と,その上に書いてある解説の文言を変えている場合があります.

最後に一言

  • claude code 入門します
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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?