LoginSignup
0
2

More than 3 years have passed since last update.

【Python】ARC006A(集合(set)最強説)【AtCoder】

Last updated at Posted at 2020-03-21

集合setが最強だなと思ったのでメモ。

ARC006A

Difficulty:238
問題自体は簡単。
あとはいかに早くスマートに記述できるかが競プロで高パフォーマンスを出すための鍵!
※スマートに記述できた方が、バグもコード量も減ると思う!

別にsetなんか使わなくても解けるんだけど、今回はsetを使って解いてみる。

集合setの予備知識

高校数学のベン図みたいなことができる!
A|B:和集合
A&B:積集合
A^B:排他的論理和(どっちか一方のみtrue ビット演算のXOR)
A-B:差集合(Aのみにあるもの)

具体的には・・・

test.py
A={1,2,3,5,6}
B={2,4,5,7}
print(A|B)
print(A&B)
print(A^B)
print(A-B)

出力結果
スクリーンショット 2020-03-21 14.03.22.png

set強い(確信)
いろいろ使いどころありそう

実際にARC006Aをsetを使ってとくとこんな感じ

test.py
def I(): return int(input())
def LI(): return list(map(int,input().split()))
E = set(LI()) #集合として受け取る!
B = I()
L = set(LI()) #集合として受け取る!
ans = 0
count = len(E&L) #積集合!!!
if count==5 and B in L:
    ans = 2
else:
    ans = {6:1,5:3,4:4,3:5}[count] if count>=3 else 0
print(ans)

この集合の知識が頭にはいっていればこの問題は3分以内でコード描けるはず!
こういうの知らなくても解けるけど、知ってると早く解ける=強い人になれるとなんとなく思ってる。

おわり!

0
2
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
2