728x90
반응형
11723번: 집합
첫째 줄에 수행해야 하는 연산의 수 M (1 ≤ M ≤ 3,000,000)이 주어진다. 둘째 줄부터 M개의 줄에 수행해야 하는 연산이 한 줄에 하나씩 주어진다.
www.acmicpc.net
👩🏻💻 코드
import sys
M = int(sys.stdin.readline().rstrip())
S = set()
for i in range(M):
line = sys.stdin.readline().rstrip().split()
command = line[0]
if command == 'all':
S = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20}
elif command == 'empty':
S = set()
else:
x = int(line[1])
if command == 'add':
if x not in S:
S.add(x)
elif command == 'remove':
if x in S:
S.remove(x)
elif command == 'check':
if x in S:
print(1)
else:
print(0)
elif command == 'toggle':
if x in S:
S.remove(x)
else:
S.add(x)
📝 정리
python 자료형 중 set을 사용하면 쉽게 풀 수 있는 문제였다. PyPy3가 아니라 Python3로 제출했더니 메모리 초과가 나지 않았다.
728x90
반응형