Algorithm/BOJ

๋ฐฑ์ค€(BOJ) 1158๋ฒˆ ์š”์„ธํ‘ธ์Šค [์•Œ๊ณ ๋ฆฌ์ฆ˜/ํŒŒ์ด์ฌ(python)/์—ฐ๊ฒฐ๋ฆฌ์ŠคํŠธ]

DAHLIA CHOI 2021. 7. 7. 20:49

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

 

1158๋ฒˆ: ์š”์„ธํ‘ธ์Šค ๋ฌธ์ œ

์ฒซ์งธ ์ค„์— N๊ณผ K๊ฐ€ ๋นˆ ์นธ์„ ์‚ฌ์ด์— ๋‘๊ณ  ์ˆœ์„œ๋Œ€๋กœ ์ฃผ์–ด์ง„๋‹ค. (1 ≤ K ≤ N ≤ 5,000)

www.acmicpc.net

 

 

 

[๋ฌธ์ œ]

์š”์„ธํ‘ธ์Šค ๋ฌธ์ œ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค.

1๋ฒˆ๋ถ€ํ„ฐ N๋ฒˆ๊นŒ์ง€ N๋ช…์˜ ์‚ฌ๋žŒ์ด ์›์„ ์ด๋ฃจ๋ฉด์„œ ์•‰์•„์žˆ๊ณ , ์–‘์˜ ์ •์ˆ˜ K(≤ N)๊ฐ€ ์ฃผ์–ด์ง„๋‹ค. ์ด์ œ ์ˆœ์„œ๋Œ€๋กœ K๋ฒˆ์งธ ์‚ฌ๋žŒ์„ ์ œ๊ฑฐํ•œ๋‹ค. ํ•œ ์‚ฌ๋žŒ์ด ์ œ๊ฑฐ๋˜๋ฉด ๋‚จ์€ ์‚ฌ๋žŒ๋“ค๋กœ ์ด๋ฃจ์–ด์ง„ ์›์„ ๋”ฐ๋ผ ์ด ๊ณผ์ •์„ ๊ณ„์†ํ•ด ๋‚˜๊ฐ„๋‹ค. ์ด ๊ณผ์ •์€ N๋ช…์˜ ์‚ฌ๋žŒ์ด ๋ชจ๋‘ ์ œ๊ฑฐ๋  ๋•Œ๊นŒ์ง€ ๊ณ„์†๋œ๋‹ค. ์›์—์„œ ์‚ฌ๋žŒ๋“ค์ด ์ œ๊ฑฐ๋˜๋Š” ์ˆœ์„œ๋ฅผ (N, K)-์š”์„ธํ‘ธ์Šค ์ˆœ์—ด์ด๋ผ๊ณ  ํ•œ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด (7, 3)-์š”์„ธํ‘ธ์Šค ์ˆœ์—ด์€ <3, 6, 2, 7, 5, 1, 4>์ด๋‹ค.

N๊ณผ K๊ฐ€ ์ฃผ์–ด์ง€๋ฉด (N, K)-์š”์„ธํ‘ธ์Šค ์ˆœ์—ด์„ ๊ตฌํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜์‹œ์˜ค.

 

[์ž…๋ ฅ]

์ฒซ์งธ ์ค„์— N๊ณผ K๊ฐ€ ๋นˆ ์นธ์„ ์‚ฌ์ด์— ๋‘๊ณ  ์ˆœ์„œ๋Œ€๋กœ ์ฃผ์–ด์ง„๋‹ค. (1 ≤ K ≤ N ≤ 5,000)

 

[์ถœ๋ ฅ]

์˜ˆ์ œ์™€ ๊ฐ™์ด ์š”์„ธํ‘ธ์Šค ์ˆœ์—ด์„ ์ถœ๋ ฅํ•œ๋‹ค.

 

 

class Node:
    def __init__(self, key = None):
        self.key = key
        self.next = self.prev = self

    def __str__(self):
        return str(self.key)

class DoubleLinkedList:
    def __init__(self):
        self.head = Node()
        self.size = 0

    def __len__(self):
        return self.size

    def splice(self, a, b, x):
        if a == None or b == None or x == None: return

        a.prev.next = b.next
        b.next.prev = a.prev
        x.next.prev = b
        b.next = x.next
        a.prev = x
        x.next = a

    def moveBefore(self, a, x):
        self.splice(a, a, x.prev)
        
    def insertBefore(self, x, key):
        self.moveBefore(Node(key), x)

    def pushBack(self, key):
        self.insertBefore(self.head, key)
        self.size += 1

    def delete(self, x):
        if x == None or x == self.head: return

        x.prev.next, x.next.prev = x.next, x.prev
        self.size -= 1
        return x.key

    def search(self, key):
        v = self.head.next
        while v != self.head:
            if v.key == key: return v
            v = v.next
        return None

N,K = map(int, input().split())
L = DoubleLinkedList()
Answer = []

for i in range(1, N+1):
    L.pushBack(i)
    
v = L.head
while L.__len__() != 0:
    for i in range(1, K):
        if v.key == L.head.key: v = v.next
        x = L.search(v.key)
        L.pushBack(L.delete(x))
        v = v.next
    if v.key == L.head.key: v = v.next
    x = L.search(v.key)
    L.delete(x)
    Answer.append(v.key)
    v = v.next

print("<", end="")
print(", ".join(str(v) for v in Answer), end="")
print(">")

 

ํ๋ฅผ ์‚ฌ์šฉํ•ด์„œ ์‰ฝ๊ฒŒ ํ’€ ์ˆ˜ ์žˆ์ง€๋งŒ ์›ํ˜•์—ฐ๊ฒฐ๋ฆฌ์ŠคํŠธ๋ฅผ ์‚ฌ์šฉํ•ด์„œ ํ’€์–ด๋ดค๋‹ค!

๋ฐฑ์ค€์—์„œ python3๋กœ ์ œ์ถœํ•˜๋ฉด ์ž๊พธ ์‹œ๊ฐ„์ดˆ๊ณผ ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ•ด์„œ ๋” ๋น ๋ฅธ pypy๋กœ ์ œ์ถœํ–ˆ๋Š”๋ฐ ํ†ต๊ณผ๋๋‹ค. ์ด๋ ‡๊ฒŒ ํ•ด๋„ ๋˜๋Š”๊ฒƒ์ธ๊ฐ€,,ใ…Žใ…Ž