ํ‹ฐ์Šคํ† ๋ฆฌ ๋ทฐ

๐Ÿ‘ฉ๐Ÿป‍๐Ÿ’ป ๋ฌธ์ œ

 

20301๋ฒˆ: ๋ฐ˜์ „ ์š”์„ธํ‘ธ์Šค

์ฒซ์งธ ์ค„์— ์ •์ˆ˜ $N$, $K$, $M$์ด ์ฃผ์–ด์ง„๋‹ค. ($1 \leq N \leq 5\ 000$, $1 \leq K, M \leq N$)

www.acmicpc.net


โœ๐Ÿป ํ’€์ด

๐ŸŽจ Go

// https://www.acmicpc.net/problem/20301
package main

import (
	"bufio"
	"fmt"
	"os"
)

func main() {
	reader := bufio.NewReader(os.Stdin)
	writer := bufio.NewWriter(os.Stdout)
	defer writer.Flush()

	var n, k, m int
	fmt.Fscanln(reader, &n, &k, &m)
	queue := []int{}
	for i := 0; i < n; i++ {
		queue = append(queue, i+1)
	}
	selected := 0
	removed := []int{}
	turnRight := false
	for len(queue) > 0 {
		if len(removed)%m == 0 {
			turnRight = !turnRight
		}
		if turnRight {
			selected = (selected + k - 1) % len(queue)
		} else {
			selected = (selected - k) % len(queue)
			for selected < 0 {
				selected += len(queue)
			}
		}
		removed = append(removed, queue[selected])
		queue = append(queue[:selected], queue[selected+1:]...)
	}
	for i := 0; i < n; i++ {
		fmt.Fprintln(writer, removed[i])
	}
}

๐ŸŽจ Python3

# https://www.acmicpc.net/problem/20301
import sys

if __name__ == "__main__":
    n, k, m = list(map(int, sys.stdin.readline().split()))
    queue = [i+1 for i in range(n)]
    selected = 0
    removed = []
    turn_right = False
    while len(queue) > 0:
        if len(removed) % m == 0:
            turn_right = not turn_right
        if turn_right:
            selected = (selected + k - 1) % len(queue)
        else:
            selected = (selected - k) % len(queue)
            while selected < 0:
                selected += len(queue)
        removed.append(queue[selected])
        queue.pop(selected)
    for i in range(n):
        print(removed[i])
728x90
๋Œ“๊ธ€