๋ฐฑ์ค€ - ๋ฉ€ํ‹ฐํƒญ ์Šค์ผ€์ค„๋ง(Swift)

2022. 10. 31. 10:36ใ†Algorithm

๋ฐฑ์ค€ - ๋ฉ€ํ‹ฐํƒญ ์Šค์ผ€์ค„๋ง(Swift)

 

 

 ๋ฌธ์ œ ์„ค๋ช…

 

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

 

1700๋ฒˆ: ๋ฉ€ํ‹ฐํƒญ ์Šค์ผ€์ค„๋ง

๊ธฐ์ˆ™์‚ฌ์—์„œ ์‚ด๊ณ  ์žˆ๋Š” ์ค€๊ทœ๋Š” ํ•œ ๊ฐœ์˜ ๋ฉ€ํ‹ฐํƒญ์„ ์ด์šฉํ•˜๊ณ  ์žˆ๋‹ค. ์ค€๊ทœ๋Š” ํ‚ค๋ณด๋“œ, ํ—ค์–ด๋“œ๋ผ์ด๊ธฐ, ํ•ธ๋“œํฐ ์ถฉ์ „๊ธฐ, ๋””์ง€ํ„ธ ์นด๋ฉ”๋ผ ์ถฉ์ „๊ธฐ ๋“ฑ ์—ฌ๋Ÿฌ ๊ฐœ์˜ ์ „๊ธฐ์šฉํ’ˆ์„ ์‚ฌ์šฉํ•˜๋ฉด์„œ ์–ด์ฉ” ์ˆ˜ ์—†์ด ๊ฐ์ข… ์ „

www.acmicpc.net

 

 

 

 

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

 

 

๋ฌธ์ œ์˜ ํ•ต์‹ฌ์„ ์ฐพ๋Š”๊ฒŒ ๋ฒ„๊ฑฐ์› ๋˜ ๋ฌธ์ œ์ด๋‹ค.

์ฒ˜์Œ์—๋Š” ๋‹จ์ˆœํžˆ ๋’ค์— ์žˆ๋Š” ๋ฆฌ์ŠคํŠธ ์ค‘์—์„œ ๊ฐ€์žฅ ์ ๊ฒŒ ์‚ฌ์šฉํ•œ ๊ฒƒ์„ ๋นผ์ž๊ณ  ์ƒ๊ฐํ–ˆ๋Š”๋ฐ

2 3 2 3 1 1 5 5 6 6 10 ... 2 2 2 ์˜ ๊ฒฝ์šฐ์— ๊ฐ€์žฅ ๋‚˜์ค‘์— ๋‚˜์˜ค๋Š” 2๋ฅผ ๋นผ์•ผํ•˜๋Š”๋ฐ ์ „์ฒด์ ์œผ๋กœ ๋ดค์„๋•Œ 2์˜ ์ˆ˜๊ฐ€ ๊ฐ€์žฅ ๋งŽ๊ธฐ ๋•Œ๋ฌธ์— ๊ฐ€์žฅ ๋Šฆ๊ฒŒ ๋บŒ์œผ๋กœ์จ ์ตœ์ ์„ ๋‹ฌ์„ฑํ•˜์ง€ ๋ชปํ•œ๋‹ค.

 

๊ทธ๋ž˜์„œ ์ด ๋ฌธ์ œ๋Š” ๊ทธ๋ฆฌ๋””ํ•œ ์ ‘๊ทผ์œผ๋กœ ๊ฐ€์žฅ ๋‚˜์ค‘์— ์‚ฌ์šฉ๋˜๋Š” ๊ฒƒ์„ ๋จผ์ € ๋ฝ‘์•„์•ผ ํ•œ๋‹ค๋Š” ์•„์ด๋””์–ด๋ฅผ ์ƒ๊ฐํ•ด์•ผํ•œ๋‹ค. ์ด๋ ‡๊ฒŒ ๋˜๋ฉด ๋ฐ˜๋ก€๊ฐ€ ์—†๊ฒŒ๋˜๊ณ  ๋งค์ˆœ๊ฐ„ ์ตœ์ ์œผ๋กœ ๋‹ต์— ๋„๋‹ฌํ•  ์ˆ˜ ์žˆ๊ฒŒ ๋œ๋‹ค.

 

Set์ž๋ฃŒ๊ตฌ์กฐ๋ฅผ ์ด์šฉํ•œ ๋‹ค๋ฅธ ์‚ฌ๋žŒ์˜ ํ’€์ด๋ฅผ ๋ณด๊ณ  ๊ต‰์žฅํžˆ ์ฐธ์‹ ํ•˜๋‹ค๊ณ  ์ƒ๊ฐ์ด ๋“ค์—ˆ๋‹ค.

 

๊ธฐ์กด์˜ ์›๋ฆฌ๋Š” ์ด๋ ‡๋‹ค.

๋ฌธ์ œ์˜ ์˜ˆ์‹œ๋กœ 2๊ฐœ์˜ ์ฝ˜์„ผํŠธ์™€ 2 3 2 3 1 2 7์˜ ์ˆœ์„œ๋กœ ๋“ค์–ด์˜จ๋‹ค๊ณ  ํ–ˆ์„๋•Œ

์ฒ˜์Œ์—๋Š” 2 3์ด ์žˆ๊ฒŒ ๋œ๋‹ค.

๊ทธ๋ฆฌ๊ณ  ์ดํ›„ 2์™€ 3์ด๋‚˜์˜ฌ ๋•Œ๋Š” ๋ฌด์‹œํ•˜๊ณ  ์ง€๋‚˜๊ฐ€๊ณ  2์™€ 3์ด ์•„๋‹๋•Œ(์—ฌ๊ธฐ์„œ๋Š” 1์ด ๋“ค์–ด์™€์•ผํ• ๋•Œ) 1์˜ ๋’ค์— ์ธ๋ฑ์Šค๋ฅผ ํƒ์ƒ‰ํ•˜๋ฉด์„œ ๊ฐ€์žฅ ๋งˆ์ง€๋ง‰์— ์‚ฌ์šฉ๋˜๋Š” ์ „์ž๊ธฐ๊ธฐ๋ฅผ ๋นผ์ค€๋‹ค. ๋งŒ์•ฝ ์ดํ›„ ์‚ฌ์šฉ๋˜์ง€ ์•Š๋Š” ์ „์ž๊ธฐ๊ธฐ๊ฐ€ ํ˜„์žฌ ์ฝ˜์„ผํŠธ์— ๋ฝ‘ํ˜€์žˆ๋‹ค๋ฉด ๊ทธ๊ฑธ ๋บด์ค€๋‹ค.

 

์ด ๊ตฌ์กฐ๋ฅผ ํŽธํ•˜๊ฒŒ Set์œผ๋กœ ํ‘œํ˜„ํ–ˆ๋‹ค.

ํ˜„์žฌ ์ฝ˜์„ผํŠธ์— ๋‹ด๊ณ  ์žˆ๋Š” ๊ฒƒ์„ Set์œผ๋กœ ๋†“์•„ ๋ณ€์ˆ˜ temp์œผ๋กœ ๋‘” ๋’ค ์ธ๋ฑ์Šค๋ฅผ ํƒ์ƒ‰ํ•˜๋Š”๋ฐ temp์•ˆ์— ๊ฐ’์ด ์žˆ๋‹ค๋ฉด temp์—์„œ ๊ทธ ๊ฐ’์„ ๋ฐ”๋กœ ์ œ๊ฑฐ๋ฅผํ•œ๋‹ค. ๊ทธ๋ฆฌ๊ณ  temp์˜ count๊ฐ€ 1์ผ๋•Œ break๋ฅผ ํ•ด์ฃผ๊ณ  ๊ทธ ๊ฐ’์„ ์ฝ˜์„ผํŠธ์—์„œ ์ง€์šด๋‹ค. 

์ด๋ ‡๊ฒŒ ํ•œ๋‹ค๋ฉด ๊ฐ€์žฅ ๋งˆ์ง€๋ง‰์— ์‚ฌ์šฉ๋˜๋Š” ์ „์ž๊ธฐ๊ธฐ๋งŒ ๋‚จ๊ฒจ๋‘˜ ์ˆ˜ ์žˆ๊ณ , ๋งŒ์•ฝ ๋งˆ์ง€๋ง‰์— ์‚ฌ์šฉ๋˜๋Š” ์ „์ž๊ธฐ๊ธฐ๊ฐ€ ํ•˜๋‚˜๋„ ์—†๋Š” ๊ฒฝ์šฐ์—๋„ temp.first๋ฅผ ํ†ตํ•ด ์•„๋ฌด๊ฑฐ๋‚˜ ์ง€์šธ ์ˆ˜ ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ๋…ผ๋ฆฌ๊ฐ€ ๊ฐ€๋Šฅํ•ด์ง„๋‹ค.

 

import Foundation

let input = readLine()!.components(separatedBy:" ").map{Int($0)!}
let N = input[0], K = input[1]
let usageList = readLine()!.components(separatedBy:" ").map{Int($0)!}

var keep = [Int]()
var cnt = 0
for (i, plug) in usageList.enumerated() {
    if keep.contains(plug) {
        continue
    } else if keep.count < N {
        keep.append(plug)
    } else {
        var temp = Set(keep)
        for j in i+1..<K {
            if temp.count == 1 { break }
            if temp.contains(usageList[j]) {
                temp.remove(usageList[j])
            }
        }
        keep.remove(at: keep.firstIndex(of: temp.first!)!)
        keep.append(usageList[i])
        cnt += 1
    }
}
print(cnt)

 

 

 

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

 

import Foundation

let input = readLine()!.components(separatedBy:" ").map{Int($0)!}
let N = input[0], K = input[1]
let usageList = readLine()!.components(separatedBy:" ").map{Int($0)!}

var keep = [Int]()
var cnt = 0
for (i, plug) in usageList.enumerated() {
    if keep.contains(plug) { // ์ฝ˜์„ผํŠธ์— ๊ฝ‚ํ˜€์žˆ์œผ๋ฉด ๋„˜์–ด๊ฐ€๊ณ 
        continue
    } else if keep.count < N { // ์ฝ˜์„ผํŠธ๊ฐ€ ๊ฝ‰์ฐจ์ง€์•Š์•˜๋‹ค๋ฉด ์ถ”๊ฐ€
        keep.append(plug)
    } else {
        var offIndex = -1 // ์ฝ˜์„ผํŠธ ์ธ๋ฑ์Šค
        var lastUsed = -1 // ์ฝ˜์„ผํŠธ์— ๊ฝ‚ํ˜€์žˆ๋Š” ๊ฐ’๋“ค์„ ํƒ์ƒ‰ํ•˜๋ฉด์„œ ๋” ํฐ ์ธ๋ฑ์Šค๊ฐ€ ๋ฌด์—‡์ธ์ง€ ์ฐพ๊ธฐ์œ„ํ•œ ๋ณ€์ˆ˜
        
        for (plugIndex, p) in keep.enumerated() {
            var shouldIndex = -1
            for j in i+1..<K {
                if p == usageList[j] { //์ฝ˜์„ผํŠธ์— ๊ฝ‚ํ˜€์žˆ๋Š” ๊ฒŒ ํŠน์ •์ธ๋ฑ์Šค์—์„œ ์‚ฌ์šฉ์ด ๋˜์—ˆ๋‹ค๋ฉด ์ธ๋ฑ์Šค ๋ณด๊ด€
                    shouldIndex = j 
                    break
                }
            }
            if shouldIndex == -1 { // ํ˜„์žฌ ํƒ์ƒ‰ํ•˜๊ณ  ์žˆ๋Š” ์ฝ˜์„ผํŠธ์— ๊ฝ‚ํžŒ ์ˆซ์ž๊ฐ€ ์ดํ›„์— ์—†๋‹ค๋ฉด break
                offIndex = plugIndex
                break
            } else {
                if shouldIndex > lastUsed { //์ด์ „์— ๊ฝ‚ํ˜€์žˆ๋˜ ์ธ๋ฑ์Šค๋ณด๋‹ค ๋†’๋‹ค๋ฉด ๊ฐฑ์‹ .
                    lastUsed = shouldIndex
                    offIndex = plugIndex
                }
            }
        }
        keep[offIndex] = plug
        cnt += 1
        
    }
}
print(cnt)

 

 ํ”ผ๋“œ๋ฐฑ

 

์ข€ ์–ด๋ ค์›Œ์„œ ๋‹ค๋ฅธ์‚ฌ๋žŒ์˜ ํ’€์ด๋ฅผ ์ฐพ์•„๋ณด๊ณ  ๋ฌธ์ œ๋ฅผ ํ’€์—ˆ๋‹ค.

๋ฌธ์ œ์˜ ํŠน์„ฑ์ƒ ์ฝ˜์„ผํŠธ๊ฐ€ ๊ฝ‰์ฐจ๋Š” ๊ฒƒ์„ ๋จผ์ € if๋ฌธ์— ๋‘๋Š” ๊ฒƒ์ด ์•„๋‹Œ ์ฝ˜์„ผํŠธ์— ์ด๋ฏธ ๊ฐ’์ด ์žˆ๋‹ค๋ฉด continueํ•˜๋Š” ๋กœ์ง์„ ๋จผ์ € ๋„ฃ์–ด์ค˜์•ผํ–ˆ๋‹ค. 

 

๊ทธ๋ฆฌ๊ณ  ๋ฌธ์ œ๋ฅผ Set์„ ์ด์šฉํ•ด์„œ ๋‹จ์ˆœํ™” ์‹œํ‚จ ์ ์ด ์žฌ๋ฐŒ์—ˆ๋Š”๋ฐ ๋‚˜๋„ ์œ ์—ฐํ•œ ์‚ฌ๊ณ ๋ฅผ ๊ฐ€์ง€๋ ค๊ณ  ๋…ธ๋ ฅํ•ด์•ผ๊ฒ ๋‹ค.