import Foundation
/// N - ํ
์คํธ ์ผ์ด์ค ๊ฐ์
/// n - ๋ฌธ์์ ๊ฐ์
/// m - ํ์ฌ Queue์์ ๋ช๋ฒ์งธ๋ก ๋์ฌ์๋์ง ๋ํ๋ด๋ ์ ์ (์ด ๋ฌธ์๊ฐ ๋ช๋ฒ์งธ๋ก ์ธ์๋์๋์ง ์ฐพ์์ผํจ)
///
/// ํ๋ฆฐํฐ ์๋๋ฐฉ์
/// 1. ํ์ฌ Queue์ ๊ฐ์ฅ ์์ ์๋ ๋ฌธ์์ '์ค์๋' ํ์ธ
/// 2. ๋๋จธ์ง ๋ฌธ์๋ค ์ค ์ค์๋๊ฐ ๋์ ๋ฌธ์๊ฐ ์์ผ๋ฉด ๋งจ๋ค๋ก ๋ณด๋ด๋ฒ๋ฆผ ๊ทธ๋ ์ง์๋ค๋ฉด ๋ฐ๋ก ์ธ์
let N = Int(readLine()!)!
for _ in 0..<N {
let nm = readLine()!.components(separatedBy: " ").map { Int($0)! }
let n = nm[0]
let m = nm[1]
var docs = readLine()!.components(separatedBy: " ").map { Int($0)! }
var printCount = 0
var currentIndex = m
while true {
// ๊ฐ์ด ์์๊ฒฝ์ฐ
if docs.isEmpty { break }
// ๋ฝ์ ๊ฐ์ด ์ต๋๊ฐ์ผ ๊ฒฝ์ฐ
if docs.first == docs.max() {
printCount += 1
// ์ฒซ๋ฒ์งธ ๊ฐ์ด ์ต๋๊ฐ์ธ ๊ฒฝ์ฐ ์ข
๋ฃ
if currentIndex == 0 {
break
}
docs.removeFirst()
currentIndex -= 1
} else {
// ๋ฝ์ ๊ฐ์ด ์ต๋๊ฐ์ด ์๋ ๊ฒฝ์ฐ ๋ง์ง๋ง์ผ๋ก ์ฎ๊ธฐ๊ธฐ
docs.append(docs.removeFirst())
currentIndex = currentIndex == 0 ? docs.endIndex - 1 : currentIndex - 1
}
}
print(printCount)
docs.removeAll()
}
๋ฐ์ํ
'๐ ์ฝํ > BOJ' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[Swift] ๋ฐฑ์ค2231 (0) | 2023.03.24 |
---|---|
[Swift] ๋ฐฑ์ค2164 (0) | 2023.03.24 |
[Swift] ๋ฐฑ์ค1436 (0) | 2023.03.23 |
[Swift] ๋ฐฑ์ค1181 (0) | 2023.03.23 |
[Swift] ๋ฐฑ์ค1259 (0) | 2023.03.23 |