๐ฅ๋ฌธ์ ๋งํฌ
https://www.acmicpc.net/problem/1021
๐ฅํ์ด
์ฒ์์๋ ์ฐ์ฐํ์๋ฅผ ๊ณ ๋ คํด์ arr์ ๋ฐฐ์ด์์์ shift๋ฅผ ํ์ง์๊ณ ํธ๋ ์ค ์์๋๋ฐ ๊ตณ์ด ๊ทธ๋ฌ์ง ์๊ณ ๊ทธ๋ฅ shift์ push๋ฅผ ์ด์ฉํ๋ฉด ์ฝ๊ฒ ํ์ ์๋ค.
const fs = require("fs");
const input = fs.readFileSync("/dev/stdin").toString().split("\n");
const arr = input[1].split(" ").map((t) => Number(t));
const [n, m] = input[0].split(" ").map((t)=>Number(t));
let dic = [];
for (let i = 1; i <= n; ++i) {
dic.push(i);
}
const f1 = () => {
dic.shift();
};
const f2 = () => {
let v = dic.shift();
dic.push(v);
};
const f3 = () => {
let v = dic.pop();
dic.unshift(v);
};
let answer =0
for (let i = 0; i < arr.length; ++i) {
let target = arr[i];
if (dic[0] === target) {
f1();
} else {
let index = dic.indexOf(target)
let flag = dic.length/2
if (index < flag) {
while(dic[0]!==target){
f2();
answer+=1;
}
} else {
while(dic[0]!==target){
f3();
answer+=1;
}
}
f1();
}
}
console.log(answer);
'๐Algorithm' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
๋ฐฑ์ค[JS] > 1058๋ฒ ์น๊ตฌ (0) | 2024.10.31 |
---|---|
๋ฐฑ์ค[JS] > 1051๋ฒ ์ซ์ ์ ์ฌ๊ฐํ (0) | 2024.10.31 |
๋ฐฑ์ค[JS] > 1074๋ฒ Z (0) | 2024.10.28 |
ํ๋ก๊ทธ๋๋จธ์ค[JS] > [PCCP ๊ธฐ์ถ๋ฌธ์ ] 2๋ฒ / ํผ์ฆ ๊ฒ์ ์ฑ๋ฆฐ์ง (1) | 2024.09.11 |
ํ๋ก๊ทธ๋๋จธ์ค[JS] > [PCCP ๊ธฐ์ถ๋ฌธ์ ] 3๋ฒ / ์ถฉ๋์ํ ์ฐพ๊ธฐ (0) | 2024.09.11 |