๊ฐœ๋ฐœ/๐Ÿค– ์•Œ๊ณ ๋ฆฌ์ฆ˜

baekjoon. 1071 ์†ŒํŠธ [Platinum V][python]

ttoance 2023. 6. 22. 09:00

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

 

1071๋ฒˆ: ์†ŒํŠธ

N๊ฐœ์˜ ์ •์ˆ˜๊ฐ€ ์ฃผ์–ด์ง€๋ฉด, ์ด๊ฒƒ์„ ์—ฐ์†๋œ ๋‘ ์ˆ˜๊ฐ€ ์—ฐ์†๋œ ๊ฐ’์ด ์•„๋‹ˆ๊ฒŒ ์ •๋ ฌ(A[i] + 1 ≠ A[i+1])ํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜์‹œ์˜ค. ๊ฐ€๋Šฅํ•œ ๊ฒƒ์ด ์—ฌ๋Ÿฌ ๊ฐ€์ง€๋ผ๋ฉด ์‚ฌ์ „์ˆœ์œผ๋กœ ๊ฐ€์žฅ ์•ž์„œ๋Š” ๊ฒƒ์„ ์ถœ๋ ฅํ•œ๋‹ค.

www.acmicpc.net

 

์ตœ์ข…์ ์ธ ์•Œ๊ณ ๋ฆฌ์ฆ˜์œผ๋กœ๋Š” ์ด ๋ถ„์˜ ๋ธ”๋กœ๊ทธ๋ฅผ ์ฐธ๊ณ ํ–ˆ๋‹ค. 

https://hoji25.tistory.com/4

 

[๋ฐฑ์ค€ ์•Œ๊ณ ๋ฆฌ์ฆ˜] 1071๋ฒˆ ์†ŒํŠธ c++

a[i]+1 ์ด a[i+1]์ด ์•„๋‹Œ ์ •๋ ฌ ์ค‘ ์‚ฌ์ „ ์ˆœ์ด ๊ฐ€์žฅ ๋น ๋ฅธ ๊ฒฐ๊ณผ๋ฅผ ์ถœ๋ ฅํ•˜๋Š” ๋ฌธ์ œ์ด๋‹ค. ์‚ฌ์ „ ์ˆœ์ด ๊ฐ€์žฅ ๋นจ๋ผ์•ผ ํ•˜๋ฏ€๋กœ ์˜ค๋ฆ„์ฐจ์ˆœ ์ •๋ ฌ์„ ์ง„ํ–‰ํ•œ๋‹ค. v[i]+1์ด v[i+1]์ผ ๋•Œ, 2๊ฐ€์ง€์˜ ๊ฒฝ์šฐ๋ฅผ ํ™•์ธํ•˜๋ฉด ๋œ๋‹ค. 1. v[i+

hoji25.tistory.com

 

1. ์‚ฌ์ „์ˆœ์œผ๋กœ ๊ฐ€์žฅ ์•ž์„  ๊ฒƒ์„ ์ถœ๋ ฅํ•ด์•ผ ํ•˜๋ฏ€๋กœ ๋จผ์ € ์˜ค๋ฆ„์ฐจ์ˆœ ์ •๋ ฌ์„ ํ•œ๋‹ค. 

2. [i] + 1 == [i +1]์„ ์ฐพ์œผ๋ฉด, 

2-1. [i + 1]๋ณด๋‹ค ํฐ ์ˆซ์ž๊ฐ€ ์—†๋‹ค๋ฉด, [i]์™€ [i+1]์˜ ์ˆœ์„œ๋ฅผ ๊ต์ฒดํ•œ๋‹ค. 11112222 -> 22221111

2-2. [i + 1]๋ณด๋‹ค ํฐ ์ˆซ์ž p๊ฐ€ ์žˆ๋‹ค๋ฉด, [i+1]์™€p๋ฅผ ๊ต์ฒดํ•œ๋‹ค. 111122223 -> 111132222

N = int(input())
numList = list(map(int,input().split()))

# ์˜ค๋ฆ„์ฐจ์ˆœ์œผ๋กœ ์ •๋ ฌ
numList.sort()

for i in range(len(numList) - 1):
    # print(numList[i], ' :: ', numList)
    if (i > 0 and numList[i] == numList[i - 1]):
        count = count +1
    else:
        count = 0
    
    if (numList[i] + 1) == numList[i + 1]:
        # i + 1 ์ด ์ตœ์†Ÿ๊ฐ’์ด๋ผ๋ฉด 
        if (numList[-1] == numList[i + 1]):
            temp = numList[-1]
            del numList[-1]
            numList.insert(i - count, temp)
            # print('>', numList)
        else:
            # i + 1 ๋‹ค์Œ์œผ๋กœ ์ตœ์†Ÿ๊ฐ’์„ ์ฐพ์•„์„œ ๋‹ค์Œ ๊ฐ’์œผ๋กœ ๋„ฃ์–ด์คŒ 
            for nextIndex in range(i +2, len(numList)):
                if numList[nextIndex] > numList[i + 1]:
                    temp = numList[nextIndex]
                    del numList[nextIndex]
                    numList.insert(i + 1, temp)
                    break
                
        
print(*numList)

 

1% ๋ฐ˜๋ก€

์ฐธ๊ณ  : https://www.acmicpc.net/board/view/117883

5
0 1 0 1 3
>> 0 0 3 1 1

 

2% ๋ฐ˜๋ก€ 

์ฐธ๊ณ  : https://www.acmicpc.net/board/view/111422

7
1 1 2 3 3 4 5
>> 1 1 3 2 4 3 5

 

9% ๋ฐ˜๋ก€ 

9
1 1 1 1 2 2 2 2 2
>> 2 2 2 2 2 1 1 1 1

 

 


 

โ‘ ์ฐจ ํ’€์ด 

1. ๋จผ์ € ์ œ์ผ ์ž‘์€ ์ˆ˜๋ฅผ 0๋ฒˆ์— ๋„ฃ๋Š”๋‹ค. 

2. ๊ทธ๋ฆฌ๊ณ , 1๋ฒˆ๋ถ€ํ„ฐ i = i +1์„ ๋งŒ์กฑํ•˜์ง€ ์•Š์€ ์ˆซ์ž ์ค‘์—์„œ ์ตœ์†Ÿ๊ฐ’์„ ๊ฐ€์ ธ์™€์„œ ๋‹ค์Œ์— ๋„ฃ์–ด์ค€๋‹ค. 

>> ์ด๋ ‡๊ฒŒ ํ•˜๋ฉด,  12 ๊ฐ™์€ ๊ฒฝ์šฐ 21์„ ํ•ด์•ผ ํ•˜๋Š”๋ฐ ๊ทธ ์กฐ๊ฑด์„ ๋งŒ์กฑํ•˜์ง€ ๋ชปํ•˜๊ฒŒ ๋œ๋‹ค. 

N = int(input())
numList = list(map(int,input().split()))

# ์˜ค๋ฆ„์ฐจ์ˆœ์œผ๋กœ ์ •๋ ฌ
numList.sort()

# https://stackoverflow.com/questions/6142689/initialising-an-array-of-fixed-size-in-python
usedList = [False] * len(numList)

sortedList = [None] * len(numList)

# ์ฒ˜์Œ ์ˆซ์ž๋Š” ์ œ์ผ ์ž‘์€ ์ˆซ์ž ?
sortedList[0] = numList[0]
usedList[0] = True
sortedIndex = 0

# print(len(numList))
# print(numList)
while(sortedIndex < (len(numList) -1) ):
    print(sortedIndex, '>', sortedList)
    print('ใ„ด ', usedList)
    
    possibleIndex = 0
    for i in range(len(numList)):
        # print(i, '>>', usedList[i], '>>', (sortedList[sortedIndex] + 1), '!=', numList[i])
        if usedList[i] == False and (sortedList[sortedIndex] + 1) != numList[i]:
            # print('match ? ')
            possibleIndex = i
            
            # ์—ฌ๊ธฐ ๊ฐ’์„ ๋‹ค์‹œ ๋ชป์ฐพ์œผ๋ฉด reset ... 
            break;
    
    sortedIndex = sortedIndex + 1
    usedList[possibleIndex] = True
    sortedList[sortedIndex] = numList[possibleIndex]
      
    

print(*sortedList)

 

โ‘ก์ฐจ ํ’€์ด 

1. ์‚ฌ์ „์ˆœ์œผ๋กœ ๊ฐ€์žฅ ์•ž์„  ๊ฒƒ์„ ์ถœ๋ ฅํ•ด์•ผ ํ•˜๋ฏ€๋กœ ๋จผ์ € ์˜ค๋ฆ„์ฐจ์ˆœ ์ •๋ ฌ์„ ํ•œ๋‹ค.

2. [i] + 1 == [i +1]์„ ์ฐพ์œผ๋ฉด, 

2-1. [i + 1]๋ณด๋‹ค ํฐ ์ˆซ์ž p๊ฐ€ ์žˆ๋‹ค๋ฉด, [i + 1] ์ „์ฒด์™€ p์ „์ฒด๋ฅผ ๊ตํ™˜ํ•œ๋‹ค. 1111222233 -> 11113322

2-2. [i + 1]๋ณด๋‹ค ํฐ ์ˆซ์ž๊ฐ€ ์—†๋‹ค๋ฉด, [i]์™€ [i+1]์˜ ์ˆœ์„œ๋ฅผ ๊ต์ฒดํ•œ๋‹ค. 11112222 -> 22221111

>> ์ด๋ ‡๊ฒŒํ•˜๋ฉด, 1123345 ๊ฐ€ ์žˆ๋Š”๊ฒฝ์šฐ 33์ด ๋ฌถ์—ฌ์„œ 2์™€ ๋ฐ”๊พธ๊ฒŒ ๋จ์œผ๋กœ์จ, 1123354๊ฐ€ ๋‚˜์˜จ๋‹ค. (๋‹ต์€ 1132435์ด๋‹ค) 

 

N = int(input())
numList = list(map(int,input().split()))

# ์˜ค๋ฆ„์ฐจ์ˆœ์œผ๋กœ ์ •๋ ฌ
numList.sort()

for i in range(len(numList) - 1):
    print(numList[i], ' :: ', numList)
    
    if (i > 0 and numList[i] == numList[i - 1]):
        count = count +1
    else:
        count = 0
        
    # print(count)
    
    if (numList[i] + 1) == numList[i + 1]:
        nextIndex = i + 2
        nextFound = False
        while (nextIndex < len(numList)):
            if numList[nextIndex] > numList[i + 1]:
                next = numList[nextIndex]
                nextFound = True
                break
            nextIndex = nextIndex + 1
        
        nextIndex = i + 2
        if (nextFound):
             while(numList[nextIndex] == next) :
                print('> ',numList[nextIndex],'=', numList)
                next = numList[nextIndex]
                del numList[nextIndex]
                print(numList)
                numList.insert(i + count + 1, next)
                print(numList)
                nextIndex = nextIndex + 1
                
                # ๋งˆ์ง€๋ง‰์ด๋ฉด ์ข…๋ฃŒ 
                if (nextIndex == len(numList)):
                    break
        
            
        tempIndex = i + 1
        if (not nextFound and tempIndex < len(numList)):
            temp = numList[tempIndex]
            
            while(numList[tempIndex] == temp) :
                # print('>> ',numList[tempIndex],'=', numList)
                del numList[tempIndex]
                numList.insert(i - count, temp)
                tempIndex = tempIndex + 1
                
                # ๋งˆ์ง€๋ง‰์ด๋ฉด ์ข…๋ฃŒ 
                if (tempIndex == len(numList)):
                    break
        
print(*numList)

 

 

๋ฐ˜์‘ํ˜•