-
[๋ฐฑ์ค] 1713 ํ๋ณด ์ถ์ฒํ๊ธฐ with PythonPS 2021. 10. 18. 20:30728x90๋ฐ์ํ
๐ BOJ 1713 ํ๋ณด ์ถ์ฒํ๊ธฐ
๐ก ์กฐ๊ฑด
- ์ฌ์งํ์ ๊ฐ์ N์ด ์ฃผ์ด์ง๋ค.
(1 โค N โค 20)
์ด ์ถ์ฒ ํ์๋1,000๋ฒ ์ดํ
์ด๋ฉฐ ํ์์ ๋ํ๋ด๋ ๋ฒํธ๋1๋ถํฐ 100๊น์ง์ ์์ฐ์
- ์ฌ์งํ์ ๊ฐ์์ ์ ์ฒด ํ์์ ์ถ์ฒ ๊ฒฐ๊ณผ๊ฐ ์ถ์ฒ๋ฐ์ ์์๋๋ก ์ฃผ์ด์ก์ ๋, ์ต์ข ํ๋ณด๊ฐ ๋๊ตฌ์ธ์ง ๊ฒฐ์
- ๊ตฌํ & ์๋ฎฌ๋ ์ด์ ์ ํ์ ๋ฌธ์
๐ฅ ์์ค ์ฝ๋
from sys import stdin import heapq def solution(n): student = {} data = list(map(int, stdin.readline().split())) for s in data: if s not in student: if len(student) >= n: # dict๋ฅผ heapq ๋ชจ๋์ ์ฌ์ฉํด ์ต์๊ฐ์ ๋ฝ์๋ a = heapq.nsmallest(min(student), student, key=student.get) student.pop(a[0]) student[s] = 1 else: student[s] += 1 return sorted(student.keys()) n = int(stdin.readline().rstrip()) r = int(stdin.readline().rstrip()) print(*solution(n))
๐ ์์ ๋ฐ ์คํ๊ฒฐ๊ณผ
์์
3 9 2 1 4 3 5 6 2 7 2
์คํ๊ฒฐ๊ณผ
2 6 7
โจ๏ธ ๋ฌธ์ ํ์ด
์ถ์ฒ๋ฐ์ ์์๋๋ก ์ฃผ์ด์ง ๋ฆฌ์คํธ๋ฅผ ์ํํ๋ฉฐ student (dict) ์ ์์ผ๋ฉด ์๋ก ์ถ๊ฐํ๊ณ ,
student (dict) ์ ์์ผ๋ฉด + 1์ ํด์ค๋ค.
๋ง์ฝ, ์๋ก ์ถ๊ฐ๋ฅผ ํด์ผํ๋๋ฐ student ๊ธธ์ด๊ฐ ์ฌ์งํ์ ๊ฐ์๋ณด๋ค ๊ฐ๊ฑฐ๋ ํด ๊ฒฝ์ฐ, ๊ฐ์ฅ ์ ๊ฒ ์ถ์ฒ ๋ฐ์ ํ์ ๋๋ ๊ฐ์ฅ ์ค๋๋ ์ฌ์ง์ ๋บ๋ค.student์ ๋จ์์๋ key๋ง ๋ฝ์ ์ ๋ ฌํ ๋ค ๋ฐํํ๋ค.
๐พ ๋๋์
- heapq ์ ๋ ๋ค์ํ ๊ธฐ๋ฅ์ด ์๋ค๋ ๊ฒ์ ๊ฒ์์ ํตํด ์์๋ค.
- ์ผ๋ฐ ๋ฐ๋ชฉ + ์กฐ๊ฑด๋ฌธ์ผ๋ก ์ฝ๋๋ฅผ ์์ฑํ ๊ฒ๋ณด๋ค ํจ์ฌ๋ ๋ณด๊ธฐ๊ฐ ์ข์๋ค.
๋ฐ์ํ'PS' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[๋ฐฑ์ค] 2343 ๊ธฐํ ๋ ์จ with Python (0) 2021.10.19 [๋ฐฑ์ค] 1743 ์์๋ฌผ ํผํ๊ธฐ with Python (0) 2021.10.18 [๋ฐฑ์ค] 1244 ์ค์์น ์ผ๊ณ ๋๊ธฐ with Python (0) 2021.10.17 [Programmers] ํ ํธ์ง with Python (0) 2021.10.17 [Programmers] ๊ด๊ณ ์ฝ์ with Python (0) 2021.10.17 - ์ฌ์งํ์ ๊ฐ์ N์ด ์ฃผ์ด์ง๋ค.