Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weโ€™ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

14 ljedd2 #170

Open
wants to merge 13 commits into
base: main
Choose a base branch
from
Open

14 ljedd2 #170

wants to merge 13 commits into from

Conversation

LJEDD2
Copy link
Collaborator

@LJEDD2 LJEDD2 commented Sep 28, 2024

๐Ÿ”— ๋ฌธ์ œ ๋งํฌ

๋ฐฑ์ค€ ํ | BOJ 2161 ์นด๋“œ1

๋ฌธ์ œ ์„ค๋ช…
N์žฅ์˜ ์นด๋“œ๊ฐ€ ์žˆ๋‹ค. ๊ฐ๊ฐ์˜ ์นด๋“œ๋Š” ์ฐจ๋ก€๋กœ 1๋ถ€ํ„ฐ N๊นŒ์ง€์˜ ๋ฒˆํ˜ธ๊ฐ€ ๋ถ™์–ด ์žˆ์œผ๋ฉฐ, 1๋ฒˆ ์นด๋“œ๊ฐ€ ์ œ์ผ ์œ„์—, N๋ฒˆ ์นด๋“œ๊ฐ€ ์ œ์ผ ์•„๋ž˜์ธ ์ƒํƒœ๋กœ ์ˆœ์„œ๋Œ€๋กœ ์นด๋“œ๊ฐ€ ๋†“์—ฌ ์žˆ๋‹ค.

์ด์ œ ๋‹ค์Œ๊ณผ ๊ฐ™์€ ๋™์ž‘์„ ์นด๋“œ๊ฐ€ ํ•œ ์žฅ ๋‚จ์„ ๋•Œ๊นŒ์ง€ ๋ฐ˜๋ณตํ•˜๊ฒŒ ๋œ๋‹ค.

  1. ์ œ์ผ ์œ„์— ์žˆ๋Š” ์นด๋“œ๋ฅผ ๋ฐ”๋‹ฅ์— ๋ฒ„๋ฆฐ๋‹ค.
  2. ์ œ์ผ ์œ„์— ์žˆ๋Š” ์นด๋“œ๋ฅผ ์ œ์ผ ์•„๋ž˜์— ์žˆ๋Š” ์นด๋“œ ๋ฐ‘์œผ๋กœ ์˜ฎ๊ธด๋‹ค.

์˜ˆ๋ฅผ ๋“ค์–ด N=4์ธ ๊ฒฝ์šฐ๋ฅผ ์ƒ๊ฐํ•ด ๋ณด์ž.
์นด๋“œ๋Š” ์ œ์ผ ์œ„์—์„œ๋ถ€ํ„ฐ 1234 ์˜ ์ˆœ์„œ๋กœ ๋†“์—ฌ์žˆ๋‹ค. 1์„ ๋ฒ„๋ฆฌ๋ฉด 234๊ฐ€ ๋‚จ๋Š”๋‹ค.
์—ฌ๊ธฐ์„œ 2๋ฅผ ์ œ์ผ ์•„๋ž˜๋กœ ์˜ฎ๊ธฐ๋ฉด 342๊ฐ€ ๋œ๋‹ค.
3์„ ๋ฒ„๋ฆฌ๋ฉด 42๊ฐ€ ๋˜๊ณ , 4๋ฅผ ๋ฐ‘์œผ๋กœ ์˜ฎ๊ธฐ๋ฉด 24๊ฐ€ ๋œ๋‹ค.
๋งˆ์ง€๋ง‰์œผ๋กœ 2๋ฅผ ๋ฒ„๋ฆฌ๊ณ  ๋‚˜๋ฉด,

๋ฒ„๋ฆฐ ์นด๋“œ๋“ค์€ ์ˆœ์„œ๋Œ€๋กœ 1 3 2๊ฐ€ ๋˜๊ณ , ๋‚จ๋Š” ์นด๋“œ๋Š” 4๊ฐ€ ๋œ๋‹ค.

N์ด ์ฃผ์–ด์กŒ์„ ๋•Œ, ๋ฒ„๋ฆฐ ์นด๋“œ๋“ค์„ ์ˆœ์„œ๋Œ€๋กœ ์ถœ๋ ฅํ•˜๊ณ , ๋งˆ์ง€๋ง‰์— ๋‚จ๊ฒŒ ๋˜๋Š” ์นด๋“œ๋ฅผ ์ถœ๋ ฅํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜์‹œ์˜ค.

์ œํ•œ ์กฐ๊ฑด

  • ์ฒซ์งธ ์ค„์— ์ •์ˆ˜ N(1 โ‰ค N โ‰ค 1,000)์ด ์ฃผ์–ด์ง

โœ”๏ธ ์†Œ์š”๋œ ์‹œ๊ฐ„

40๋ถ„ +@

โœจ ์ˆ˜๋„ ์ฝ”๋“œ

์š”์„ธํ‘ธ์Šค(Josephus) ๋ฌธ์ œ์™€ ๋น„์Šทํ•œ ๋ฌธ์ œ์ž…๋‹ˆ๋‹ค.
์ƒ๊ฐ๋ณด๋‹ค ๋ฌธ์ œ ์ง€๋ฌธ์„ ๊ทธ๋Œ€๋กœ ๊ตฌํ˜„ํ•˜๋ฉด ๋˜๋Š” ๋ฌธ์ œ๋ผ ์„ค๋ช…ํ•  ๊ฒƒ์ด ๋งŽ์ด ์—†๋„ค์š” ...

collections ๋ชจ๋“ˆ์—์„œ deque๋ฅผ ์ž„ํฌํŠธํ•˜์—ฌ ํšจ์œจ์ ์ธ ํ ์—ฐ์‚ฐ ๋ฐฉ์‹์„ ์‚ฌ์šฉํ•˜์˜€์Šต๋‹ˆ๋‹ค. ์–‘ ๋์—์„œ์˜ ์‚ฝ์ž…๊ณผ ์‚ญ์ œ๋ฅผ O(1) ์‹œ๊ฐ„์— ์ˆ˜ํ–‰ํ•  ์ˆ˜ ์žˆ๋Š” ์žฅ์ ์ด ์žˆ์Šต๋‹ˆ๋‹ค.
queue.popleft() , queue.append() ๋ฅผ ์‚ฌ์šฉํ•˜์˜€์Šต๋‹ˆ๋‹ค.

์นด๋“œ ๋ฐฐ์—ด์„ ๋งŒ๋“  ๋’ค ์•ž์นด๋“œ๋ฅผ ๋ฒ„๋ฆฌ๊ณ  ๋‘๋ฒˆ์จฐ ์นด๋“œ๋ฅผ ๋’ค์— ์ถ”๊ฐ€ํ•˜๋Š” ๋™์ž‘์„ ๋ฐ˜๋ณตํ•ด์ฃผ๋ฉด ๋ฉ๋‹ˆ๋‹ค.

์ตœ์ข… ์ฝ”๋“œ

import sys
from collections import deque
input = sys.stdin.readline

n = int(input().strip())
queue = deque([i for i in range(1, n+1)])
result = []

while len(queue) > 1: # ์นด๋“œ ๋‘์žฅ ์ด์ƒ์ผ ๋•Œ, 
    result.append(queue.popleft()) #๋งจ ์•ž ์นด๋“œ ๋ฒ„๋ฆผ  
    queue.append(queue.popleft()) # ๋ฒ„๋ฆฐ ํ›„ ๋งจ ์•ž์นด๋“œ ๋’ค๋กœ ์˜ฎ๊น€ 
else:
    result.append(queue.popleft()) # ์ดˆ๋ฐ˜์— ํ•œ ์žฅ ๋” ์ถ”๊ฐ€ 

print(" ".join(map(str, result))) # ๊ฒฐ๊ณผ ์ถ”๊ฐ€ 

๐Ÿ“š ์ƒˆ๋กญ๊ฒŒ ์•Œ๊ฒŒ๋œ ๋‚ด์šฉ

Copy link
Member

@jung0115 jung0115 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ํšจ์œจ์ ์ธ ๋ฐฉ๋ฒ•์„ ๊ณ ๋ฏผํ•œ ๊ฒŒ ๋Œ€๋‹จํ•˜์‹  ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค! ์ €๋„ ์ฐธ๊ณ ํ•ด์„œ ๋น„์Šทํ•˜๊ฒŒ ํ•ด๊ฒฐํ•  ์ˆ˜ ์žˆ์—ˆ์–ด์š” โ˜บ๏ธ ์ด๋ฒˆ ์ฐจ์‹œ๋„ ์ˆ˜๊ณ ํ•˜์…จ์–ด์š”!

import java.util.LinkedList
import java.util.Queue

fun main() {
    val N = readLine()!!.toInt()
    
    val queue: Queue<Int> = LinkedList()
    val sb = StringBuilder()
    
    // 1๋ถ€ํ„ฐ N๊นŒ์ง€์˜ ์นด๋“œ๋ฅผ ํ์— ์ถ”๊ฐ€
    for (i in 1..N) {
        queue.add(i)
    }
    
    // ์นด๋“œ๊ฐ€ ํ•œ ์žฅ ๋‚จ์„ ๋•Œ๊นŒ์ง€ ๋ฐ˜๋ณต
    while (queue.size > 1) {
        // ์ œ์ผ ์œ„์— ์žˆ๋Š” ์นด๋“œ๋ฅผ ๋ฒ„๋ฆผ
        sb.append(queue.poll()).append(" ")
        
        // ๊ทธ ๋‹ค์Œ ์นด๋“œ๋ฅผ ์ œ์ผ ์•„๋ž˜๋กœ ์ด๋™
        queue.add(queue.poll())
    }
    
    // ๋งˆ์ง€๋ง‰์œผ๋กœ ๋‚จ์€ ์นด๋“œ ์ถ”๊ฐ€
    sb.append(queue.poll())
    
    // ๋ชจ๋“  ๊ฒฐ๊ณผ๋ฅผ ํ•œ ๋ฒˆ์— ์ถœ๋ ฅ
    println(sb.toString())
}

Copy link
Member

@janghw0126 janghw0126 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

์ €๋Š” ๋ฌธ์ œ๋ฅผ ๋ณด๊ณ  ์นด๋“œ๋ฅผ ๋ฒ„๋ฆฌ๊ณ  ๋‚œ ํ›„์— ์นด๋“œ๊ฐ€ ์—†์œผ๋ฉด ๋ฐ˜๋ณต์„ ๋ฉˆ์ถœ ์ˆ˜ ์žˆ๋„๋ก ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•˜์˜€์Šต๋‹ˆ๋‹ค.

import sys
input=sys.stdin.readline

n = int(input())
card = [i for i in range(1, n + 1)]
result = []

while True:
    result.append(card.pop(0))
    if not card: 
        break
    card.append(card.pop(0)) 

print(*result)

์ •์€๋‹˜์˜ ์ˆ˜๋„ ์ฝ”๋“œ๋ฅผ ๋ณด๊ณ  ์š”์„ธํ‘ธ์Šค ๋ฌธ์ œ์™€ ๋น„์Šทํ•œ ๋ฌธ์ œ์ž„์„ ๊นจ๋‹ซ๊ณ  ๊ฐ‘๋‹ˆ๋‹ค!๐Ÿ‘

result = []

while len(queue) > 1:
result.append(queue.popleft())
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ํ๋ฅผ ์ด์šฉํ•ด์„œ ํšจ์œจ์ ์ธ ์—ฐ์‚ฐ์„ ์ด์šฉํ•  ์ˆ˜ ์žˆ์—ˆ๊ตฐ์š”! ์งฑ์ด์‹ญ๋‹ˆ๋‹ค๐Ÿฅน๐Ÿฅน

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants