์ด ๋ฌธ์ ๋ Finite Array Swaps์ ๊ตต์ ๊ธ์จ๋ก ์ ํ ๋ถ๋ถ๊ณผ ์ ์ถ๋ ฅ๋ง ๋ค๋ฆ ๋๋ค.
๋์ฐ๋ ๊ธธ์ด๊ฐ ์ธ ๋ ๋ฐฐ์ด ๊ณผ
์ ๊ฐ์ง๊ณ ์๋ค.
๋์ฐ๋ ๋ ๋ฐฐ์ด์ ๋ค์ ๋ ์ํ์ ๊ฐ๊ฐ ์ํ๋ ๋งํผ ํ ์ ์๋ค. ๊ตํํ๋ ๋ ์์์ ์ธ๋ฑ์ค๋ ๋ฌ๋ผ์ผ ํ๋ค.
์ด ์ฐ์ฐ์ ํตํด ์ป์ ์ต์ข ์ํ์์์ ๋ ๋ฐฐ์ด์ ๊ณผ ์ด๋ผ ํ ๋, ๋์ฐ๋ ๋ฅผ ๋ง์กฑํ๋ ์์ ๊ฐ์๋ฅผ ์ต๋ํํ๋ ค๊ณ ํ๋ค. ๋ ๋ฐฐ์ด์ด ์ฃผ์ด์ง ๋, ์ด๋ฅผ ์ต๋๋ก ํ๋ ๊ตํ์ ์ฐพ์๋ณด์.
์ฒซ ๋ฒ์งธ ์ค์ ๋ ๋ฐฐ์ด์ ๊ธธ์ด ์ด ์ฃผ์ด์ง๋ค.
๋ ๋ฒ์งธ ์ค์ ๋ฐฐ์ด ์ ์์ ์ด ๊ณต๋ฐฑ์ผ๋ก ๊ตฌ๋ถ๋์ด ์ฃผ์ด์ง๋ค.
์ธ ๋ฒ์งธ ์ค์ ๋ฐฐ์ด ์ ์์ ์ด ๊ณต๋ฐฑ์ผ๋ก ๊ตฌ๋ถ๋์ด ์ฃผ์ด์ง๋ค.
์ฃผ์ด์ง๋ ์ ๋ ฅ์ ๋ชจ๋ ์ ์์ด๋ค.
GUN์ด ์ด ๊ณก์ ๊ตฌ๊ฐ์ ์ด๋น ๋ฐ์ ๋ณํ๋์ ํฉ์ ์ ๋ ฅ ์์๋๋ก Q ์ค์ ๊ฑธ์ณ ์ถ๋ ฅํ๋ค.
6
2 4 3 3 2 4
3 1 6 8 2 2
3
4 3 2 4 3 2
1 6 2 8 3 2
import sys
import collections
'''
๊ทธ๋ฅ ๊ฐ์๊ฑฐ ์ฐพ์ผ๋ฉด ์๋จ?
1์ด์ 10**5
๊ทผ๋ฐ for ํ๋๋ก ํด์ผ ํ๋ค
'''
def find_same_numbers(a,b):
count_a = collections.Counter(a)
count_b = collections.Counter(b)
combinations_number = []
not_combinations_a = []
not_combinations_b = []
for i in set(a+b):
if i in count_a and i in count_b:
common = min(ca := count_a[i],cb := count_b[i])
combinations_number.extend([i] * common)
if (cnt_a := ca - common) > 0:
not_combinations_a.extend([i] * cnt_a)
if (cnt_b := cb - common) > 0:
not_combinations_b.extend([i] * cnt_b)
elif i in count_a:
not_combinations_a.extend([i] * count_a[i])
else:
not_combinations_b.extend([i] * count_b[i])
return combinations_number, not_combinations_a,not_combinations_b
def main():
inputs = sys.stdin.read().split()
n = int(inputs[0])
combinations_number,not_combinations_a,not_combinations_b =(
find_same_numbers(inputs[1:n+1],inputs[n+1:]))
sys.stdout.write(str(len(combinations_number))+'\n')
sys.stdout.write(" ".join(combinations_number + not_combinations_a) + '\n')
sys.stdout.write(" ".join(combinations_number + not_combinations_b))
if __name__ == '__main__':
main()
๋ฌธ์ ๋ฅผ ์๋ชป ์ฝ์ด์ ์๊ฐ์ ๋ง์ด ์๋นํ ๋ฌธ์ . ๋จ์ ๊ตฌํ ๋ฌธ์ ๋ผ๊ณ ์๊ฐํ๋, ์ข ์ด์ ์๋์ฝ๋๋ฅผ ์ ์ด์ ์ฃ์ง ์ผ์ด์ค๋ฅผ ๋ฐฉ์งํ๋๊ฒ ์ค์ํ๋ค๊ณ ์๊ฐํ๋ค. ๊ทธ๋ฆฌ๊ณ sys.stdout.write ์ค์๋ก 2๋ฒ ํ๋ ค์ ๋ฉํ์ด ์ฝ๊ฐ ํ๋ค๋ ธ๋ค. ์์ผ๋ก๋, print๋ฅผ ์ฐ๊ณ ์ต์ ํ ํ ๋ ๋ณ๊ฒฝํ๋ ค๊ณ ํ๋ค.
2๋ฑ. 1๋ฑ์ ๊ฒฝ์ฐ dic.get์ ์ด์ฉํจ. ๋๋ Counter๋ฅผ ์ฌ์ฉ. ์ผ์ด์ค ์ฐจ์ด๋ผ๊ณ ์๊ฐ.
import sys
import collections
def match_elements(array_a, array_b):
"""
๋ ๋ฐฐ์ด์์ ๊ฐ ์์์ ๋ฑ์ฅ ๋น๋๋ฅผ ๊ณ์ฐํ ํ,
- matched: ๋ ๋ฐฐ์ด ๋ชจ๋์์ ๋งค์นญ ๊ฐ๋ฅํ ์์(์ต์ ๋น๋๋งํผ) ๋ฆฌ์คํธ
- remaining_a: array_a์์ ๋งค์นญ ํ ๋จ์ ์์ ๋ฆฌ์คํธ
- remaining_b: array_b์์ ๋งค์นญ ํ ๋จ์ ์์ ๋ฆฌ์คํธ
๋ฅผ ๋ฐํํฉ๋๋ค.
"""
count_a = collections.Counter(array_a)
count_b = collections.Counter(array_b)
matched = [] # ๋ ๋ฐฐ์ด์์ ๋งค์นญ๋ ์์๋ค์ ์ ์ฅ
remaining_a = [] # array_a์๋ง ๋จ๋ ์์๋ค์ ์ ์ฅ
remaining_b = [] # array_b์๋ง ๋จ๋ ์์๋ค์ ์ ์ฅ
# ๋ ๋ฐฐ์ด์ ๋ฑ์ฅํ๋ ๋ชจ๋ ๊ณ ์ ์์์ ๋ํด ์ฒ๋ฆฌ
for element in set(array_a + array_b):
# element๊ฐ ๋ ๋ฐฐ์ด ๋ชจ๋์ ์์ ๊ฒฝ์ฐ
if element in count_a and element in count_b:
common = min(count_a[element], count_b[element])
matched.extend([element] * common)
# array_a์ ๋จ๋ ๊ฐ์๊ฐ ์๋ค๋ฉด ์ถ๊ฐ
if (leftover_a := count_a[element] - common) > 0:
remaining_a.extend([element] * leftover_a)
# array_b์ ๋จ๋ ๊ฐ์๊ฐ ์๋ค๋ฉด ์ถ๊ฐ
if (leftover_b := count_b[element] - common) > 0:
remaining_b.extend([element] * leftover_b)
# element๊ฐ array_a์๋ง ์์ ๊ฒฝ์ฐ
elif element in count_a:
remaining_a.extend([element] * count_a[element])
# element๊ฐ array_b์๋ง ์์ ๊ฒฝ์ฐ
else:
remaining_b.extend([element] * count_b[element])
return matched, remaining_a, remaining_b
def main():
data = sys.stdin.read().split()
n = int(data[0])
# ๋ฐฐ์ด A์ B๋ฅผ ๊ฐ๊ฐ n๊ฐ์ ์์๋ก ๋ถ๋ฆฌ
array_a = data[1:n+1]
array_b = data[n+1:2*n+1]
matched, leftover_a, leftover_b = match_elements(array_a, array_b)
# ์ต์ข
๋ฐฐ์ด์ ๋งค์นญ๋ ์์๋ฅผ ์๋ถ๋ถ์ ๋๊ณ ๋๋จธ์ง ์์๋ฅผ ๋ค์ ์ด์ด๋ถ์
๋๋ค.
final_array_a = matched + leftover_a
final_array_b = matched + leftover_b
sys.stdout.write(str(len(matched)) + '\n')
sys.stdout.write(" ".join(final_array_a) + '\n')
sys.stdout.write(" ".join(final_array_b))
if __name__ == '__main__':
main()