๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ

๐Ÿ’ปAlgorithm/PS

[BOJ/๋ฐฑ์ค€] 11582๋ฒˆ ํŒŒ์ด์ฌ - ์น˜ํ‚จ TOP N

๋ฌธ์ œ

๋ฐฑ์ค€ 11582 ์น˜ํ‚จ TOP N

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

 

11582๋ฒˆ: ์น˜ํ‚จ TOP N

์ธํ•˜๋Œ€ ์ฃผ๋ณ€ ์น˜ํ‚จ์นฉ์˜ ๋ง›์˜ ์ •๋„๋ฅผ ์ธก์ •ํ•ด ์ˆ˜์น˜ํ™”ํ•˜๋Š” ๋™์•„๋ฆฌ C.T.P(Chicken Tastes Perfect)์˜ ํšŒ์žฅ ๋ฏผํ˜ธ๋Š” ์น˜ํ‚จ์ง‘์˜ ๋ง›์˜ ์ˆ˜์น˜๋ฅผ ๊ฐ์†Œํ•˜์ง€ ์•Š๋Š” ์ˆœ์œผ๋กœ ์ •๋ ฌ์„ ํ•˜๊ณ  ์‹ถ์—ˆ๋‹ค. ํ•˜์ง€๋งŒ ์น˜ํ‚จ์ง‘์ด ๋„ˆ๋ฌด ๋งŽ

www.acmicpc.net

 

๋ฌธ์ œํ’€์ด

merge sort ์˜ ์ค‘๊ฐ„ ๊ณผ์ •์„ ์ถœ๋ ฅํ•˜๋Š” ๋ฌธ์ œ ; k๋ช…์ด ์ •๋ ฌํ•˜๋Š” ๋‹จ๊ณ„๊ฐ€ ์™„๋ฃŒ๋œ ์ƒํƒœ ์ถœ๋ ฅ
merge ํ•จ์ˆ˜๋ฅผ ๋งŒ๋“ค์–ด์„œ merge sort ์ง„ํ–‰ํ•˜๊ธฐ
check ํ•จ์ˆ˜๋ฅผ ๋งŒ๋“ค์–ด์„œ k๋ช… ๋ฏธ๋งŒ์ด ์ •๋ ฌํ•˜๋ฉด ์ •๋ ฌ ์ค‘์ง€์‹œํ‚ค๊ธฐ

์ฝ”๋“œ

import sys
input=sys.stdin.readline

def check(s,e):
    if((e-s)>(n/k)):  ##์ •๋ ฌ ์ •์ง€ ์กฐ๊ฑด
        return
    mid=int((s+e)/2);
    idx1=s; idx2=mid+1; idx3=0;
    
    while(idx1<=mid and idx2<=e):
        if(num[idx1]<=num[idx2]):
            tmp[idx3]=num[idx1]
            idx3+=1; idx1+=1
        else:
            tmp[idx3]=num[idx2]
            idx3+=1; idx2+=1
    while(idx1<=mid):
        tmp[idx3]=num[idx1]
        idx3+=1; idx1+=1
    while(idx2<=e):
        tmp[idx3]=num[idx2]
        idx3+=1; idx2+=1
    for i in range(s,e+1):
        num[i]=tmp[i-s]
        
def merge(s,e):
    if s==e:
        return
    mid=int((s+e)/2)
    merge(s,mid)
    merge(mid+1,e)
    check(s,e)
    

n=int(input())
num=list(map(int,input().split()))
tmp=[0]*n
k=int(input())
merge(0,n-1)
print(*num)

์—ฌ๊ธฐ์„œ

  • if(e-s>n/k): return
  • e-s = ์ •๋ ฌ์ค‘์ธ ๋ฆฌ์ŠคํŠธ์˜ ํฌ๊ธฐ , (n/k) = k๋ช… ๋‹น ์ •๋ ฌํ•ด์•ผํ•˜๋Š” ๋ฐฐ์—ด์˜ ํฌ๊ธฐ

์„ ์˜๋ฏธ!

 

์–ด๋ ค์› ๋˜ ์ 

์ฝ”๋“œ ์งœ๋Š”๊ฑฐ ์ž์ฒด๋Š” ํฌ๊ฒŒ ์–ด๋ ค์šด ๊ฒŒ ์•„๋‹ˆ์—ˆ๋Š”๋ฐ ์ •๋ ฌ ์ •์ง€ ์กฐ๊ฑด์„ ์ƒ๊ฐํ•˜๋Š” ๊ฒƒ์ด ์ข€ ์–ด๋ ค์› ๋‹ค

๊ทธ ๋‹จ๊ณ„๊ฐ€ ์™„๋ฃŒ๋œ ์ดํ›„์˜ ์ •๋ ฌ ๊ฐ’์„ ์ถœ๋ ฅํ•˜๋Š” ๊ฒƒ์ด๋ผ์„œ ๋” ํ—ท๊ฐˆ๋ ธ์›€ ใ…œใ…œ