๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๐Ÿงฉps/๐Ÿ”ฅNormal

[๋ฐฑ์ค€ 2108๋ฒˆ] ํ†ต๊ณ„ํ•™

by goguma.dev 2024. 9. 28.

๐Ÿ“–๋ฌธ์ œ:

 

๐Ÿ“™ํ’€์ด:

์ž…๋ ฅ๋ฐ›๋Š” ์ˆซ์ž๋“ค์„ ๋ฆฌ์ŠคํŠธ๊ฐ€ ์•„๋‹Œ ๋”•์…”๋„ˆ๋ฆฌ ํ˜•ํƒœ๋กœ ์ €์žฅํ•˜์—ฌ ๋นˆ๋„ ์ˆ˜๋ฅผ ํ•จ๊ป˜ ์ €์žฅํ–ˆ๋‹ค.

๋”•์…”๋„ˆ๋ฆฌ๋ฅผ ์ •๋ ฌํ•˜๋Š” ๋ฐฉ๋ฒ•์ด ๊ธฐ์–ต์ด ์•ˆ๋‚˜์„œ ๊ตฌ๊ธ€๋ง์„ ํ–ˆ๋‹ค...

 

โœ๏ธ์ฝ”๋“œ:

'''
https://www.acmicpc.net/problem/2108
๋ฌธ์ œ: ํ†ต๊ณ„ํ•™
๋‚œ์ด๋„: ์‹ค๋ฒ„3
'''
import sys

def myround(num):
    a = int(num)
    b = num - a
    if b >= 0.5:
        return a + 1
    elif b <= -0.5:
        return a - 1
    else:
        return a

index = int(input())
box = {}
for i in range(index):
    num = int(sys.stdin.readline())
    if num not in box:
        box[num] = 1
    else:
        box[num] += 1

sorted_by_key = dict(sorted(box.items(), key=lambda item: item[0])) # ์ˆซ์ž ํฌ๊ธฐ ์ˆœ์œผ๋กœ ์ •๋ ฌ
sorted_list = list(sorted_by_key.keys())

sorted_by_value = dict(sorted(box.items(), key=lambda item: (-item[1], item[0]))) # ๋นˆ๋„ ์ˆœ์œผ๋กœ ์ •๋ ฌ
freq_list = list(sorted_by_value.items())

# ์‚ฐ์ˆ ํ‰๊ท  ์ถœ๋ ฅ
sum = 0
for m, n in sorted_by_key.items():
    sum += m*n
print(myround(sum/index))

# ์ค‘๊ฐ„๊ฐ’ ์ถœ๋ ฅ
n = 0
cur_index = 0
middle = 0
while cur_index <= int(index/2):
    middle = sorted_list[n]
    cur_index += sorted_by_key[middle]
    n+=1
print(middle)

# ์ตœ๋นˆ๊ฐ’ ์ถœ๋ ฅ
if index == 1: # ์š”์†Œ๊ฐ€ ํ•˜๋‚˜ ์ผ๋•Œ
    print(freq_list[0][0])
else:
    # ์ฒซ ๋ฒˆ์งธ ์š”์†Œ์˜ ํ‚ค์™€ ๊ฐ’
    first_key, first_value = freq_list[0]
    # ๋‘ ๋ฒˆ์งธ ์š”์†Œ์˜ ํ‚ค์™€ ๊ฐ’
    second_key, second_value = freq_list[1]
    if first_value == second_value:
        print(second_key)
    else:
        print(first_key)

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

 

๐Ÿ”—๋งํฌ:

https://www.acmicpc.net/problem/2108