๋ฐฑ์ค€ - ์Šค์œ„์น˜ ์ผœ๊ณ  ๋„๊ธฐ(Swift)

2022. 11. 10. 14:30ใ†Algorithm

๋ฐฑ์ค€ - ์Šค์œ„์น˜ ์ผœ๊ณ  ๋„๊ธฐ(Swift)

 

 

 ๋ฌธ์ œ ์„ค๋ช…

 

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

 

1244๋ฒˆ: ์Šค์œ„์น˜ ์ผœ๊ณ  ๋„๊ธฐ

์ฒซ์งธ ์ค„์—๋Š” ์Šค์œ„์น˜ ๊ฐœ์ˆ˜๊ฐ€ ์ฃผ์–ด์ง„๋‹ค. ์Šค์œ„์น˜ ๊ฐœ์ˆ˜๋Š” 100 ์ดํ•˜์ธ ์–‘์˜ ์ •์ˆ˜์ด๋‹ค. ๋‘˜์งธ ์ค„์—๋Š” ๊ฐ ์Šค์œ„์น˜์˜ ์ƒํƒœ๊ฐ€ ์ฃผ์–ด์ง„๋‹ค. ์ผœ์ ธ ์žˆ์œผ๋ฉด 1, ๊บผ์ ธ์žˆ์œผ๋ฉด 0์ด๋ผ๊ณ  ํ‘œ์‹œํ•˜๊ณ  ์‚ฌ์ด์— ๋นˆ์นธ์ด ํ•˜๋‚˜์”ฉ

www.acmicpc.net

 

 ๋‚˜์˜ ํ’€์ด

 

์Šค์œ„์น˜์˜ ๊ฐœ์ˆ˜์™€ ํ•™์ƒ์˜ ์ˆ˜๊ฐ€ 100์ดํ•˜ ์ด๋ฏ€๋กœ ์™„์ „ํƒ์ƒ‰์„ ํ•  ์ˆ˜ ์žˆ๊ณ  ์ด๋กœ์ธํ•ด ๊ตฌํ˜„๋ฌธ์ œ์ธ ๊ฒƒ์„ ์•Œ ์ˆ˜ ์žˆ๋‹ค.

๋ชจ๋“  ๊ตฌํ˜„๋ฌธ์ œ๊ฐ€ ๊ทธ๋ ‡๋“ฏ์ด ํ•ญ์ƒ ํ•œ ๋ฌธ์žฅ์„ ์ œ๋Œ€๋กœ ์ฝ์ง€ ์•Š์•„์„œ ํ‹€๋ฆฌ๋Š” ๊ฒฝ์šฐ๊ฐ€ ๋งŽ๋‹ค.

๋‚˜์˜ ๊ฒฝ์šฐ์—๋Š” ๊ตฌ๊ฐ„์— ์†ํ•œ ์Šค์œ„์น˜์˜ ๊ฐœ์ˆ˜๋Š” ํ™€์ˆ˜๋ผ๋Š” ๋ฌธ์žฅ์„ ๊ฐ„๊ณผํ–ˆ์—ˆ๋Š”๋ฐ, ์—ฌ์ž๊ฐ€ 3์„ ๋ฝ‘์•˜๋‹ค๋ฉด 3 ๋˜ํ•œ ๋’ค์ง‘์–ด์ค˜์•ผํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์ด๋ฅผ ๊ณ ๋ คํ–ˆ์–ด์•ผ ํ–ˆ๋‹ค.

 

import Foundation

let n = Int(readLine()!)!
var states = readLine()!.components(separatedBy:" ").map{Int($0)!} // ์Šค์œ„์น˜ ์ƒํƒœ
let total = states.count
states.insert(0, at: 0) // 1๋ฒˆ ์ธ๋ฑ์Šค๋ถ€ํ„ฐ ํŽธํ•˜๊ฒŒ ํ•˜๊ธฐ์œ„ํ•ด ์‚ฝ์ž…
let num = Int(readLine()!)! // ํ•™์ƒ ์ˆ˜

for _ in 0..<num {
    let input = readLine()!.components(separatedBy:" ").map{Int($0)!}
    let type = input[0], card = input[1] // ์„ฑ๋ณ„, ๋ฐ›์€ ์ˆซ์ž
    var ran = 1
    switch type {
    case 1: // ๋‚จ์ž์ผ ๋•Œ
        let share = total / card // ๋ฐฐ์ˆ˜๊ฐ€ ๋˜๋Š” ๊ฐœ์ˆ˜
        for i in 1..<share+1 {
            let idx = card*i
            toggle(idx)
        }
    case 2: // ์—ฌ์ž์ผ ๋•Œ
        while true {
            if check(card, ran) { // ๋ฒ”์œ„์•ˆ ๊ฐ’๋“ค์ด ๊ฐ™๋‹ค๋ฉด toggleํ›„ ๋” ๊นŠ๊ฒŒ ํƒ์ƒ‰
                toggle(card-ran)
                toggle(card+ran)
            } else {
                toggle(card)
                break
            }
            ran += 1
        }
    default:
        break
    }
}

states.removeFirst() // ์ฒ˜์Œ ์‚ฝ์ž…ํ•œ ๊ฒƒ์„ ์ง€์šด๋‹ค.
if states.count < 21 {
    print(states.map{String($0)}.joined(separator:" "))
} else {
    let p = total / 20
    let rest = total % 20
    var idx = -20
    for _ in 0..<p {
        idx += 20
        for j in 0..<20 {
            print(states[idx+j], terminator: " ")
        }
        print()
    }
    if rest != 0 {
        idx += 20
        for j in 0..<rest {
            print(states[idx+j], terminator: " ")
        }
    }
    
}


// states[idx]์˜ ์ƒํƒœ๋ฅผ ๋ฐ”๊ฟ”์ค€๋‹ค.
func toggle(_ idx: Int) {
    if states[idx] == 0 {
        states[idx] = 1
    } else {
        states[idx] = 0
    }
}

// ์—ฌ์ž์ผ ๋•Œ ์ธ๋ฑ์Šค ๋ฒ”์œ„๋ฅผ ํ™•์ธํ•ด์„œ ๊ฐ€๋Šฅํ•˜๋ฉด true ์•„๋‹ˆ๋ฉด false
func check(_ idx: Int, _ range: Int) -> Bool {
    if idx-range < 1 || idx+range > total { // ์ธ๋ฑ์Šค์˜ ๋ฒ”์œ„๊ฐ€ ๋„˜์–ด๊ฐ€๋ฉด false
        return false
    }
    return states[idx-range] == states[idx+range] ? true : false
}

 

 ๋‹ค๋ฅธ ์‚ฌ๋žŒ์˜ ํ’€์ด

 

20๊ฐœ์”ฉ ๋Š์–ด์•ผ ํ•˜๋Š” ๋ถ€๋ถ„์„ ๋ชซ๊ณผ ๋‚˜๋จธ์ง€๋ฅผ ๊ตฌํ•ด์„œ ์ฒ˜๋ฆฌํ–ˆ์—ˆ๋Š”๋ฐ ๋‹ค๋ฅธ ์‚ฌ๋žŒ์˜ ํ’€์ด๋ฅผ ๋ณด๋‹ˆ ๋”์šฑ ๊ฐ„ํŽธํ•œ ๋ฐฉ๋ฒ•์ด ์žˆ์—ˆ๋‹ค.

์ธ๋ฑ์Šค๊ฐ€ 20๋ฒˆ์งธ์ผ๋•Œ๋Š” ์ถœ๋ ฅํ•˜๊ณ  ์ค„๋„˜๊น€๋งŒ ํ•˜๋ฉด 20๋ณด๋‹ค ์ž‘์„๋•Œ๋„ ์ฒ˜๋ฆฌํ•  ์ˆ˜ ์žˆ๊ฒŒ ๋œ๋‹ค.

 

for (i, x) in states.enumerated() {
    if (i+1) % 20 == 0 {
        print(x, terminator: "\n")
    } else { print(x, terminator: " ")}
}