์ƒˆ์†Œ์‹

๐Ÿงฎ PS

๋ฐฑ์ค€ (boj) ํŒŒ์ด์ฌ - 2108 ํ†ต๊ณ„ํ•™

  • -

๋ฌธ์ œ ๋งํฌ : https://www.acmicpc.net/problem/2108

 

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

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

www.acmicpc.net

 

 

 

์‚ฐ์ˆ ํ‰๊ท , ์ค‘์•™๊ฐ’, ๋ฒ”์œ„๋Š” ์‰ฌ์› ์ง€๋งŒ ์ตœ๋นˆ๊ฐ’์ด ์กฐ๊ธˆ ์–ด๋ ค์› ๋‹ค. ์ตœ๋นˆ๊ฐ’์„ ๊ตฌํ•ด์ฃผ๋Š” ๋ชจ๋“ˆ์ด ์žˆ์—ˆ๋Š”๋ฐ

์ฝœ๋ ‰์…˜ ๋ชจ๋“ˆ์˜ Counter์˜€๋‹ค.

https://infinitt.tistory.com/183

 

ํŒŒ์ด์ฌ Collections ๋ชจ๋“ˆ - counter , most_common

collections ๋ชจ๋“ˆ์€ ๊ธฐ๋ณธ์ ์œผ๋กœ ํŒŒ์ด์ฌ์— ๋‚ด์žฅ๋˜์–ด์žˆ๋Š” ๋‚ด์žฅํ•จ์ˆ˜์ž…๋‹ˆ๋‹ค. (๋”ฐ๋กœ ์„ค์น˜๊ฐ€ ํ•„์š” ์—†..) ๋ฆฌ์ŠคํŠธ๋‚˜, ๋ฌธ์ž์—ด์˜ ์š”์†Œ์— ๋Œ€ํ•œ ๊ฐœ์ˆ˜๋ฅผ ๊ตฌํ• ๋•Œ ๋ฐ˜๋ณต๋ฌธ์œผ๋กœ๋„ ๊ตฌํ•  ์ˆ˜ ์žˆ์ง€๋งŒ, counter ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ํŽธ๋ฆฌํ•ฉ๋‹ˆ..

infinitt.tistory.com

 

 

ํŒŒ์ด์ฌ ์†Œ์Šค
import sys
input = sys.stdin.readline
N=int(input())
arr = []
for _ in range(N):
    arr.append(int(input()))

#1 ์‚ฐ์ˆ  ํ‰๊ท 
print("%.f"%(sum(arr)/N))

#2 ์ค‘์•™๊ฐ’
arr.sort()
print(arr[N//2])

#3 ์ตœ๋นˆ๊ฐ’
from collections import Counter
k=Counter(arr).most_common()


if len(arr) > 1:  #๋งŒ์•ฝ ์ž…๋ ฅ๊ฐ’์ด ํ•˜๋‚˜๋ฉด , ๊ทธ๊ฒŒ ์ตœ๋นˆ๊ฐ’์ด ๋˜๋ฏ€๋กœ ์˜ˆ์™ธ์ฒ˜๋ฆฌ
    if k[0][1] == k[1][1]:print(k[1][0]) 
    # ์ตœ๋นˆ๊ฐ’์˜ ๋นˆ๋„์ˆ˜๋ฅผ ๋น„๊ตํ•˜์—ฌ, 2๊ฐœ์ด์ƒ์˜ ์ตœ๋นˆ๊ฐ’์ด ์žˆ์œผ๋ฉด ๋‘๋ฒˆ์งธ๋กœ ์ž‘์€๊ฒƒ์„ ์ถœ๋ ฅ
    else : print(k[0][0]) 
else : print(arr[0]) 

#4 ๋ฒ”์œ„
print(arr[-1] -  arr[0])

    
Contents

ํฌ์ŠคํŒ… ์ฃผ์†Œ๋ฅผ ๋ณต์‚ฌํ–ˆ์Šต๋‹ˆ๋‹ค

์ด ๊ธ€์ด ๋„์›€์ด ๋˜์—ˆ๋‹ค๋ฉด ๊ณต๊ฐ ๋ถ€ํƒ๋“œ๋ฆฝ๋‹ˆ๋‹ค.