๐ฅ Algorithm/Baekjoon
[๋ฐฑ์ค 1966๋ฒ] ํ๋ฆฐํฐ ํ - ์๋ฐ์คํฌ๋ฆฝํธ(nodejs)
Lennon
2022. 2. 15. 01:48
728x90
๋ฐ์ํ
https://www.acmicpc.net/problem/1966
const fs = require('fs');
const [n, ...input] = fs.readFileSync('dev/stdin').toString().trim().split('\n');
const result = input.map((v) => v.split(' ').map(Number));
let queue;
let answer = [];
while (result.length) {
queue = [];
const order = result.shift()[1];
const paperArr = result.shift().map((v, i) => [v, i]);
const len = paperArr.length;
while (queue.length !== len) {
if (paperArr[0][0] !== Math.max(...paperArr.map((v) => v[0]))) {
const a = paperArr.shift();
paperArr.push(a);
} else {
const a = paperArr.shift();
queue.push(a);
}
}
answer.push(queue.map((v) => v[1]).indexOf(order) + 1);
}
console.log(answer.join('\n'));
3
1 0
5
4 2
1 2 3 4 => [[1,0], [5], [4,2], [1,2,3,4], [6,0], [1,1,9,1,1,1]]
6 0 ex) 2๋ฒ์งธ ๊ฒฝ์ฐ
1 1 9 1 1 1 order = 2
paperArr = [[1,0], [2,1], [3,2], [4,2]]
์์ฒ๋ผ ์ ๋ ฅ๊ฐ์ ์กฐ์ ํ๊ณ , ์ต๋๊ฐ์ด ์๋๋ฉด ์์์ shiftํ๊ณ ๋ค๋ก ๋ค์ push, ์ต๋๊ฐ์ด๋ฉด queue๋ฐฐ์ด์ pushํ๋ค.
queue๋ฐฐ์ด์ด ์๋ณธ ๋ฐฐ์ด์ ๊ธธ์ด์ ๊ฐ์ด์ง๋ฉด ๋ฐ๋ณต๋ฌธ์ ์ข ๋ฃํ๊ณ ๋ฐฐ์ด์ 1๋ฒ์งธ ์ธ์๊ฐ order๊ฒ์ ์ฐพ์์ +1 ํด์ค ํ answer๋ฐฐ์ด์ pushํ๋ค. ๊ทธ๋ฆฌ๊ณ ์ถ๋ ฅํด์ฃผ๋ฉด ๋!
728x90
๋ฐ์ํ