์•Œ๊ณ ๋ฆฌ์ฆ˜ ๋ฌธ์ œ/BOJ_Python

[BOJ/Step12] 2108 : ํ†ต๊ณ„ํ•™ (Python)

NaNaRin๐Ÿ™ƒ 2021. 2. 19. 16:48

www.acmicpc.net/problem/2108

 

2108๋ฒˆ: ํ†ต๊ณ„ํ•™

์ฒซ์งธ ์ค„์— ์ˆ˜์˜ ๊ฐœ์ˆ˜ N(1 ≤ N ≤ 500,000)์ด ์ฃผ์–ด์ง„๋‹ค. ๊ทธ ๋‹ค์Œ N๊ฐœ์˜ ์ค„์—๋Š” ์ •์ˆ˜๋“ค์ด ์ฃผ์–ด์ง„๋‹ค. ์ž…๋ ฅ๋˜๋Š” ์ •์ˆ˜์˜ ์ ˆ๋Œ“๊ฐ’์€ 4,000์„ ๋„˜์ง€ ์•Š๋Š”๋‹ค.

www.acmicpc.net


์‚ฐ์ˆ ํ‰๊ท ๊ณผ ์ค‘์•™๊ฐ’, ์ตœ๋นˆ๊ฐ’, ๋ฒ”์œ„๋ฅผ ๊ตฌํ•˜๋Š” ๋ฌธ์ œ

์‚ฐ์ˆ ํ‰๊ท ์€ ์ž…๋ ฅ๋ฐ›์€ ๊ฐ’์„ ๋ชจ๋‘ ๋”ํ•ด ๊ฐœ์ˆ˜๋กœ ๋‚˜๋ˆ„์–ด ๋ฐ˜์˜ฌ๋ฆผ ํ•˜๋ฉด ๋˜๊ณ 

์ค‘์•™๊ฐ’์€ ์ •๋ ฌ ํ›„ n//2 ์ž๋ฆฌ์˜ ๊ฐ’์ด๋ฉฐ

๋ฒ”์œ„๋Š” ์ •๋ ฌ ํ›„ ๋งˆ์ง€๋ง‰ ๊ฐ’ - ์ฒซ๋ฒˆ์งธ ๊ฐ’์ด๋‹ค.

์ตœ๋นˆ๊ฐ’์ด ๊ฐ€์žฅ ๊นŒ๋‹ค๋กญ๋‹ค. ์ตœ๋นˆ๊ฐ’์ด ์—ฌ๋Ÿฌ ๊ฐœ ์ผ ๋•Œ ๋‘ ๋ฒˆ์งธ๋กœ ์ž‘์€ ๊ฐ’์„ ์ถœ๋ ฅํ•ด์•ผ ํ•˜๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค.

=> collection ๋ชจ๋“ˆ์˜ Counter() ํ•จ์ˆ˜, most_common() ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค.

 

# 2108.py

import collections
import sys

n = int(sys.stdin.readline())
a = [int(sys.stdin.readline()) for _ in range(n)]
a.sort()

modea = collections.Counter(a).most_common()
maxinum = modea[0][0]
try:
    if modea[1][1] == modea[0][1]:
        maxinum = modea[1][0]
except:
    pass

print(round(sum(a)/n))
print(a[n//2])
print(maxinum)
print(a[n-1] - a[0])

( ๋‹ค ํ–ˆ๋Š”๋ฐ๋„ ๊ณ„์† ์‹œ๊ฐ„์ดˆ๊ณผ๊ฐ€ ๋– ์„œ ๋ฏธ์ณ๋ฒ„๋ฆฌ๋Š” ์ค„ ์•Œ์•˜๋Š”๋ฐ input() ๋•Œ๋ฌธ์ด์—ˆ๋‹ค..... ์ด์ œ input() ์•ˆ์“ด๋‹ค ํ›„ )