ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • Kotlin ๐Ÿฌ ๋ฐฑ์ค€ 6๋‹จ๊ณ„ :: 10812 ๋ฒˆ
    2023. 4. 26. 00:45
    ๋ฐ˜์‘ํ˜•

    ๋ฐ”๊ตฌ๋‹ˆ ์ˆœ์„œ ๋ฐ”๊พธ๊ธฐ

    ๋ฌธ์ œ   |

      ๋„ํ˜„์ด๋Š” ๋ฐ”๊ตฌ๋‹ˆ๋ฅผ ์ด N๊ฐœ ๊ฐ€์ง€๊ณ  ์žˆ๊ณ , ๊ฐ๊ฐ์˜ ๋ฐ”๊ตฌ๋‹ˆ์—๋Š” 1๋ฒˆ๋ถ€ํ„ฐ N๋ฒˆ๊นŒ์ง€ ๋ฒˆํ˜ธ๊ฐ€ ์ˆœ์„œ๋Œ€๋กœ ์ ํ˜€์ ธ ์žˆ๋‹ค. ๋ฐ”๊ตฌ๋‹ˆ๋Š” ์ผ๋ ฌ๋กœ ๋†“์—ฌ์ ธ ์žˆ๊ณ , ๊ฐ€์žฅ ์™ผ์ชฝ ๋ฐ”๊ตฌ๋‹ˆ๋ฅผ 1๋ฒˆ์งธ ๋ฐ”๊ตฌ๋‹ˆ, ๊ทธ ๋‹ค์Œ ๋ฐ”๊ตฌ๋‹ˆ๋ฅผ 2๋ฒˆ์งธ ๋ฐ”๊ตฌ๋‹ˆ, ..., ๊ฐ€์žฅ ์˜ค๋ฅธ์ชฝ ๋ฐ”๊ตฌ๋‹ˆ๋ฅผ N๋ฒˆ์งธ ๋ฐ”๊ตฌ๋‹ˆ๋ผ๊ณ  ๋ถ€๋ฅธ๋‹ค.

      ๋„ํ˜„์ด๋Š” ์•ž์œผ๋กœ M๋ฒˆ ๋ฐ”๊ตฌ๋‹ˆ์˜ ์ˆœ์„œ๋ฅผ ํšŒ์ „์‹œํ‚ค๋ ค๊ณ  ๋งŒ๋“ค๋ ค๊ณ  ํ•œ๋‹ค. ๋„ํ˜„์ด๋Š” ๋ฐ”๊ตฌ๋‹ˆ์˜ ์ˆœ์„œ๋ฅผ ํšŒ์ „์‹œํ‚ฌ ๋•Œ, ์ˆœ์„œ๋ฅผ ํšŒ์ „์‹œํ‚ฌ ๋ฒ”์œ„๋ฅผ ์ •ํ•˜๊ณ , ๊ทธ ๋ฒ”์œ„ ์•ˆ์—์„œ ๊ธฐ์ค€์ด ๋  ๋ฐ”๊ตฌ๋‹ˆ๋ฅผ ์„ ํƒํ•œ๋‹ค. ๋„ํ˜„์ด๊ฐ€ ์„ ํƒํ•œ ๋ฐ”๊ตฌ๋‹ˆ์˜ ๋ฒ”์œ„๊ฐ€ begin, end์ด๊ณ , ๊ธฐ์ค€์ด ๋˜๋Š” ๋ฐ”๊ตฌ๋‹ˆ๋ฅผ mid๋ผ๊ณ  ํ–ˆ์„ ๋•Œ, begin, begin+1, ..., mid-1, mid, mid+1, ..., end-1, end ์ˆœ์„œ๋กœ ๋˜์–ด์žˆ๋Š” ๋ฐ”๊ตฌ๋‹ˆ์˜ ์ˆœ์„œ๋ฅผ mid, mid+1, ..., end-1, end, begin, begin+1, ..., mid-1๋กœ ๋ฐ”๊พธ๊ฒŒ ๋œ๋‹ค.

      ๋ฐ”๊ตฌ๋‹ˆ์˜ ์ˆœ์„œ๋ฅผ ์–ด๋–ป๊ฒŒ ํšŒ์ „์‹œํ‚ฌ์ง€ ์ฃผ์–ด์กŒ์„ ๋•Œ, M๋ฒˆ ๋ฐ”๊ตฌ๋‹ˆ์˜ ์ˆœ์„œ๋ฅผ ํšŒ์ „์‹œํ‚จ ๋‹ค์Œ, ๋ฐ”๊ตฌ๋‹ˆ์— ์ ํ˜€์žˆ๋Š” ๋ฒˆํ˜ธ๋ฅผ ๊ฐ€์žฅ ์™ผ์ชฝ ๋ฐ”๊ตฌ๋‹ˆ๋ถ€ํ„ฐ ์ถœ๋ ฅํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜์‹œ์˜ค.

     

    ์ž…๋ ฅ   |

      ์ฒซ์งธ ์ค„์— N (1 ≤ N ≤ 100)๊ณผ M (1 ≤ M ≤ 100)์ด ์ฃผ์–ด์ง„๋‹ค.

      ๋‘˜์งธ ์ค„๋ถ€ํ„ฐ M๊ฐœ์˜ ์ค„์—๋Š” ๋ฐ”๊ตฌ๋‹ˆ์˜ ์ˆœ์„œ๋ฅผ ๋ฐ”๊พธ๋Š” ๋งŒ๋“œ๋Š” ๋ฐฉ๋ฒ•์ด ์ฃผ์–ด์ง„๋‹ค. ๋ฐฉ๋ฒ•์€ i, j, k๋กœ ๋‚˜ํƒ€๋‚ด๊ณ , ์™ผ์ชฝ์œผ๋กœ๋ถ€ํ„ฐ i๋ฒˆ์งธ ๋ฐ”๊ตฌ๋‹ˆ๋ถ€ํ„ฐ j๋ฒˆ์งธ ๋ฐ”๊ตฌ๋‹ˆ์˜ ์ˆœ์„œ๋ฅผ ํšŒ์ „์‹œํ‚ค๋Š”๋ฐ, ๊ทธ ๋•Œ ๊ธฐ์ค€ ๋ฐ”๊ตฌ๋‹ˆ๋Š” k๋ฒˆ์งธ ๋ฐ”๊ตฌ๋‹ˆ๋ผ๋Š” ๋œป์ด๋‹ค. (1 ≤ i ≤ k ≤ j ≤ N)

      ๋„ํ˜„์ด๋Š” ์ž…๋ ฅ์œผ๋กœ ์ฃผ์–ด์ง„ ์ˆœ์„œ๋Œ€๋กœ ๋ฐ”๊ตฌ๋‹ˆ์˜ ์ˆœ์„œ๋ฅผ ํšŒ์ „์‹œํ‚จ๋‹ค.

     

    ์ถœ๋ ฅ   |

      ๋ชจ๋“  ์ˆœ์„œ๋ฅผ ํšŒ์ „์‹œํ‚จ ๋‹ค์Œ์—, ๊ฐ€์žฅ ์™ผ์ชฝ์— ์žˆ๋Š” ๋ฐ”๊ตฌ๋‹ˆ๋ถ€ํ„ฐ ๋ฐ”๊ตฌ๋‹ˆ์— ์ ํ˜€์žˆ๋Š” ์ˆœ์„œ๋ฅผ ๊ณต๋ฐฑ์œผ๋กœ ๊ตฌ๋ถ„ํ•ด ์ถœ๋ ฅํ•œ๋‹ค.

     

     

    ํ’€์ด  |

      k (mid)๋ฅผ ๊ธฐ์ค€์œผ๋กœ i (begin) ๋ถ€ํ„ฐ k - 1, k + 1 ๋ถ€ํ„ฐ j (end)๋ฅผ ๋ฐ”๊ฟ”์ค€๋‹ค.

      ์ฆ‰, j - k + 1 (== k - i + 1) ๋ฐ˜๋ณตํ•˜์—ฌ ๋งจ ๋’ค์— ์œ„์น˜ํ•œ ์š”์†Œ๋ฅผ ์•ž์œผ๋กœ ์ด๋™์‹œ์ผœ์ค€๋‹ค.

      ๊ฐ„๊ฒฐํ•œ ์ฝ”๋“œ๋ฅผ ์œ„ํ•ด ์œ„์น˜๋ฅผ ๋ฐ”๊ฟ”์ฃผ๋Š” ํ•จ์ˆ˜ swap()์„ array์˜ ํ™•์žฅํ•จ์ˆ˜๋กœ ์ƒ์„ฑํ•ด ๊ตฌํ˜„ํ•ด์ฃผ์—ˆ๋‹ค.

     

     

    ๋‹ต์•ˆ  |

    import java.util.Scanner
    
    fun main() = with(Scanner(System.`in`)) {
        val N = nextInt()
        val M = nextInt()
        var buckets = Array(N) { it + 1 }
        
        repeat(M) {
            val i = nextInt()
            val j = nextInt()
            val k = nextInt()
            
            repeat(j - k + 1){
                for (num in j - 1 downTo i){
                    buckets.swap(num, num - 1)
                }
            }
        }
        
        buckets.forEach { print("${it} ") }
    }
    
    fun Array<Int>.swap(n: Int, m: Int){
        val temp = this[n]
        this[n] = this[m]
        this[m] = temp
    }
    ๋ฐ˜์‘ํ˜•

    ๋Œ“๊ธ€

Designed by Tistory.