14:01
์ฒ์์ ๋ฌธ์๋ฅผ ์ ์ฅํ ๋, ๋ฌธ์์ ์ค์๋์ ์ด๊ธฐ ์ธ๋ฑ์ค๋ฅผ ๊ฐ์ด ์ ์ฅํ๋ค. ๊ทธ๋ฐ ๋ค์, ํ๊ฐ ๋น ๋๊น์ง ํ์์ ๊ฐ์ฅ ์ค์๋๊ฐ ๋์ ๊ฐ์ ๊ตฌํ๊ณ ์ฒซ๋ฒ์งธ ๋ฌธ์์ ์ค์๋์ ๋น๊ตํ๋ค. ๊ฐ์ผ๋ฉด answer
๋ฐฐ์ด์ ์ฝ์
ํ๊ณ , ๊ฐ์ง ์์ผ๋ฉด ํ์ ๋งจ ๋ค๋ก ๋ณด๋ธ๋ค.
ํ๊ฐ ๋น๋ฉด, answer
๋ฐฐ์ด์์ ์ด๊ธฐ ์ธ๋ฑ์ค๋ฅผ ๊ฐ๊ณ ์๋ ๋ฌธ์์ ์์น๋ฅผ ์ฐพ์์ + 1 ํด์ ์ถ๋ ฅํ๋ค.
๋ฌธ์
https://www.acmicpc.net/problem/1966
๋ด๊ฐ ์์ฑํ ์ฝ๋
Swift
let t = Int(readLine()!)!
for _ in 0..<t {
let n = readLine()!.split(separator: " ").map { Int(String($0))! }
var importance = readLine()!.split(separator: " ").enumerated().map { (index, element) in
return [Int(String(element))!, index]}
var answer = [[Int]]()
while !importance.isEmpty {
if let first = importance.first, let max = importance.max(by: { $0[0] < $1[0] }) {
if first[0] == max[0] {
answer.append(importance.removeFirst())
} else {
importance.append(importance.removeFirst())
}
}
}
if let find = answer.firstIndex(where: { $0[1] == n[1] }) {
print(find + 1)
}
}