๐ ๋ฌธ์ ๋งํฌ
29714๋ฒ: ๋ธ์ค์ด์ ๊ตฌ์ฌ ์์ด์คํฌ๋ฆผ
๐ ๋ฌธ์ ์์ฝ
๋ธ์ค์ด๋ ์์ํ๊ฒ ๊ตฌ์ฌ ์์ด์คํฌ๋ฆผ์ ๋จน๋๋ค. ์ฒซ ์ค์ ์ด๊ธฐ ์์ด์คํฌ๋ฆผ ๊ตฌ์ฌ์ ๊ฐฏ์ N์ด ์ฃผ์ด์ง๊ณ , ๋์งธ ์ค์ N๊ฐ์ ๊ตฌ์ฌ ์ซ์๋ค์ด ๊ณต๋ฐฑ์ผ๋ก ๋ถ๋ฆฌ๋์ด ์ฃผ์ด์ง๋ค.
์ ์งธ ์ค์ ์ ๋ ฅ๋๋ Q๋ฒ์ ๊ตฌ์ฌ ์์ด์คํฌ๋ฆผ์ ๋จน๊ณ ๋ณด์ถฉํ๊ฑฐ๋, ์๋ฌด๊ฒ๋ ํ์ง ์๋ ๊ณผ์ ์ ๊ฑฐ์ณ ๊ตฌ์ฌ ์์ด์คํฌ๋ฆผ์ ๋จน๋๋ค. (์ ๋ง ์์ํ๋ค.) Q๋ฒ ๋ ๊ฐ์ ์ค์ด ์ธํธ๋ก ์ ๋ ฅ๋๋ค. ์ฒซ ๋ฒ์งธ ์ค์, ๋จน์ผ๋ ค๋ ์์ด์คํฌ๋ฆผ ๊ตฌ์ฌ์ ๊ฐฏ์์ ๊ทธ ๊ฐฏ์๋งํผ ๊ตฌ์ฌ์ ์ซ์๊ฐ ์ฃผ์ด์ง๋ค. ๋ ๋ฒ์งธ ์ค์, ๋ณด์ถฉํ๋ ค๋ ์์ด์คํฌ๋ฆผ ๊ตฌ์ฌ์ ๊ฐฏ์์ ๊ทธ ๊ฐฏ์๋งํผ ๊ตฌ์ฌ์ ์ซ์๊ฐ ์ฃผ์ด์ง๋ค.
๋ง์ฝ, ๋จน์ผ๋ ค๋ ์์ด์คํฌ๋ฆผ ๊ตฌ์ฌ ์ค, ๊ฐ์ง๊ณ ์๋ ์์ด์คํฌ๋ฆผ์ ์๋ ๊ฒ์ด ์๋ค๋ฉด ๋จน๊ฑฐ๋ ๋ณด์ถฉํ์ง ์๊ณ ๋์ด๊ฐ๋ค.
๊ตฌ์ฌ ์์ด์คํฌ๋ฆผ์ ๋ค ๋จน์ ํ, ๋จ์์๋ ์์ด์คํฌ๋ฆผ ๊ตฌ์ฌ์ ๊ฐฏ์์, ๊ตฌ์ฌ์ ์ซ์๋ค์ ๊ณต๋ฐฑ์ผ๋ก ๊ตฌ๋ถํ์ฌ ์ถ๋ ฅํ๋ผ.
๐ฟ ํ์ด ์ค๋ช
์ ๋ง ์์ํ๊ณ ๊ธฐ์ดํ ๋ฌธ์ ๋ด์ฉ์ผ๋ก, ์ดํดํ๋ ๋ฐ์ ๋ค์ ์ด๋ ค์์ด ์์ ์ ์๋ ๋ฌธ์ ์๋ค.
์ ๋ต ์ฝ๋
import sys
input=sys.stdin.readline
N=int(input().rstrip())
marbles_list=list(map(int, input().rstrip().split()))
marbles={}
for i in range(N):
if marbles_list[i] in marbles.keys():
marbles[marbles_list[i]]+=1
else:
marbles[marbles_list[i]]=1
Q=int(input().rstrip())
continue_std=True
for _ in range(Q):
eatable=True
ai, *Ai=map(int, input().rstrip().split())
bi, *Bi=map(int, input().rstrip().split())
a_dict={}
for a in Ai:
if a not in a_dict.keys():
a_dict[a]=1
else:
a_dict[a]+=1
for a in a_dict.keys():
if a not in marbles.keys() or marbles[a]<a_dict[a]:
eatable=False
break
if eatable:
for a in a_dict.keys():
marbles[a]-=a_dict[a]
for b in Bi:
if b not in marbles.keys():
marbles[b]=1
else:
marbles[b]+=1
print(sum(marbles.values()))
for key, value in marbles.items():
for _ in range(value):
print(key, end=" ")
1. dictionary๋ฅผ ์ฌ์ฉํด์ผ ํ๋ ์ด์
์ฐ์ ๋ ์ฌ๋ ค์ผ ํ๋ ๊ฐ๋ ์ ์์ด์คํฌ๋ฆผ์ ๊ตฌ์ฌ์ ๋ด์ ๋ ์ฌ๋ฌ ์๋ฃ ๊ตฌ์กฐ ์ค, dictionary๋ฅผ ์ฌ์ฉํด์ผ ํ๋ค๋ ๊ฒ์ด๋ค.
list.pop(index)๋ list.insert(index, value)๋ ์๊ฐ ๋ณต์ก๋๊ฐ O(N)์ด๊ธฐ๋ ํ๊ณ , ๊ตฌ์ฌ ์ซ์์ ์ ํ ์กฐ๊ฑด์ด ์์ด์ list๋ฅผ ์ฌ์ฉํ๋ฉด ๋ฉ๋ชจ๋ฆฌ, ์๊ฐ์ ์ผ๋ก ๋งค์ฐ ๋นํจ์จ์ ์ด๋ค.
๋ฐ๋ผ์, {marble_num: marble_count} ํํ๋ก ๊ตฌ์ฌ ์ ๋ณด๋ฅผ ์ ์ฅํ๊ณ ๊ตฌ์ฌ ์์ด์คํฌ๋ฆผ์ ๋จน๊ณ ๋ณด์ถฉํ ๋์ ๊ฐ๊ฐ์ ๊ตฌ์ฌ ์ซ์์ ๋์ํ๋ key์ value๋ฅผ ๋ํ๊ณ ๋นผ๋ฉฐ ์ ๋ณด๋ฅผ ์ ๋ฐ์ดํธํ๋ ๊ฒ์ด ์ค์ํ๋ค. ์ด ๋, key๋ฅผ ํตํด ํด๋นํ๋ value ๊ฐ์ ์กฐํํ๊ฑฐ๋ ์ ๋ฐ์ดํธํ๋ ๊ฒ์ ์๊ฐ ๋ณต์ก๋๋ O(1)์ด๋ค.
์ด์ฒ๋ผ dictionary๊ฐ ์ ์ ์๊ฐ ๋ณต์ก๋๋ฅผ ๊ฐ์ง ์ ์๋ ์ด์ ๋ dictionary๊ฐ hash ๊ตฌ์กฐ์ด๊ธฐ ๋๋ฌธ์ด๋ค.
๊ฐ๊ฐ์ key๊ฐ์ ํด์ฌ ํจ์๋ฅผ ํตํด ํน์ ํ index ๊ฐ์ผ๋ก mapping๋๊ณ , ์ด๋ฅผ ํตํด ๊ณต๊ฐ ํจ์จ์ฑ์ ๋ค์ ํฌ๊ธฐํ๊ณ ์๊ฐ ํจ์จ์ฑ์ ํฌ๊ฒ ๋์ธ๋ค.
(์ฐธ๊ณ ํฌ์คํ : ํ์ด์ฌ์ ๋์ ๋๋ฆฌ๋ ์ด๋ป๊ฒ ๊ตฌํ๋์ด ์์๊น? -ํด์-)
2. ๊ตฌํํ ์๊ณ ๋ฆฌ์ฆ์ ์๊ฐ ํจ์จ์ฑ ๊ฐ์ : ์๊ฐ ๋ณต์ก๋ ์ถ์ ํ๊ธฐ
์ด๊ธฐ ์์ฑํ๋ ์ฝ๋๋ ์๊ฐ ์ด๊ณผ๋ก ๊ณ์ํด์ ์ค๋ต ์ฒ๋ฆฌ๊ฐ ๋์๋ค.
dict.keys()๋ฅผ ๋ฐ๋ก list๋ก ๊ด๋ฆฌํด๋ณด๋ ๊ฒ๊ณผ ๊ฐ์ด ํฌ๊ฒ ์๋ฏธ์๋ ์ฝ๋ ์์ ์ ํ๋ค๊ฐ, ์๋์ ์ ๋ต ํฌ์คํ ์ ํตํด ์ฝ๋์ ๋ฌธ์ ์ ์ ๊นจ๋ซ๊ณ ๊ฐ์ ํ ์ ์์๋ค.
์ค๋ต ์ฝ๋ (์๊ฐ์ด๊ณผ)
import sys
input=sys.stdin.readline
N=int(input().rstrip())
marbles_list=list(map(int, input().rstrip().split()))
marbles={}
for i in range(N):
if marbles_list[i] in marbles.keys():
marbles[marbles_list[i]]+=1
else:
marbles[marbles_list[i]]=1
Q=int(input().rstrip())
continue_std=True
for _ in range(Q):
if continue_std:
temp=marbles.copy()
continue_std=False
ai, *Ai=map(int, input().rstrip().split())
bi, *Bi=map(int, input().rstrip().split())
for i in range(ai):
if Ai[i] in temp.keys():
temp[Ai[i]]-=1
if temp[Ai[i]]<0:
continue_std=True
break
else:
continue_std=True
break
if continue_std:
continue
for i in range(bi):
if Bi[i] in temp.keys():
temp[Bi[i]]+=1
else:
temp[Bi[i]]=1
marbles=temp.copy()
print(sum(marbles.values()))
for key, value in marbles.items():
for _ in range(value):
print(key, end=" ")
์ค๋ต ์ฝ๋์์ ๊ฐ์ ํด์ผํ ๋ถ๋ถ์ ์๋์ ๊ฐ๋ค.
๊ธฐ์กด์๋ ํ์ฌ ๊ฐ์ง๊ณ ์๋ ๊ตฌ์ฌ์์ Ai์ ๊ตฌ์ฌ์ ํ๋์ฉ ๋นผ๋ณด๊ณ , ํ์ฌ ๊ฐ์ง๊ณ ์๋ ๊ตฌ์ฌ์ ๊ฐฏ์๊ฐ 0๋ณด๋ค ์์์ง๋ฉด ๋์ด๊ฐ๋ ๋ฐฉ์์ธ๋ฐ, ์ด ์ฝ๋์๋ ํ ๊ฐ์ง ๋ฌธ์ ์ ์ด ์๋ค.
Ai๊ฐ ์งํ ์กฐ๊ฑด์ ๋ง๋์ง ํ์ธํ๊ธฐ ์ํด marble์ copyํ temp dict์ด ํ์ํ์ฌ, ์ด๋ฅผ ๋ง๋ค๊ธฐ ์ํด copy()๋ฅผ ๋ฐ๋ณต์ ์ผ๋ก ์ฌ์ฉํ๋ค๋ ๊ฒ์ด๋ค.
copy()๋ ์๊ฐ ๋ณต์ก๋๊ฐ O(N)์ด๊ณ , ํน์ ํ ๊ฒฝ์ฐ์์๋ง copy()ํ๊ฒ๋ ์กฐ๊ฑด์ ์ถ๊ฐํ์์๋ ์๊ฐ ์ด๊ณผ ๋ฌธ์ ๊ฐ ์ง์๋์๋ค.
์ด๋ฅผ ์ฐ์ธก๊ณผ ๊ฐ์ ์ฝ๋๋ก ์์ ํ์ฌ ๋๋ต, N^2์์ 3N ์์ค์ ์๊ฐ ๊ฐ์๊ฐ ๋ฐ์ํ ๊ฒ์ผ๋ก ๋ณด์ธ๋ค.
์๊ฐ์ ๋ง์ด ์ฌ์ฉํ๋ ํจ์๋ค์ ๋ฐ๋ณต์ ์ผ๋ก ์ฌ์ฉํ๋ ๊ฒ์ ์ฃผ์ํ์.