[백준] - 1244 스위치 끄고 켜기 (node.js)

밀루·2025년 1월 16일
0

BOJ

목록 보기
52/82

문제링크

코드

// 스위치 켜고 끄기

const fs = require("fs");
const filePath = process.platform === "linux" ? "/dev/stdin" : "./input.txt";
const arr = fs.readFileSync(filePath).toString().trim().split("\n");

const n = Number(arr[0]);
const l1 = arr[1].split(" ").map(Number);
const student = Number(arr[2]);

const getList = (num, l1) => {
  const l2 = [num];
  for (let i = 1; i <= num; i++) {
    if (l1[num - i] === l1[num + i]) {
      l2.push(num - i);
      l2.push(num + i);
    } else {
      break;
    }
  }
  return l2;
};

for (let i = 0; i < student; i++) {
  const [sex, num] = arr[i + 3].split(" ").map(Number);
  if (sex === 1) { // 남학생
    for (let j = 0; j < n; j++) {
      if ((j + 1) % num === 0) {
        l1[j] = l1[j] === 0 ? 1 : 0;
      }
    }
  } else { // 여학생
    const dec = getList(num - 1, l1); // 대칭리스트 받기
    for (const d of dec) {
      l1[d] = l1[d] === 0 ? 1 : 0;
    }
  }
}

let result = [];
while (l1.length > 0) { // 20개씩 끊어서 저장
  result.push(l1.splice(0, 20).join(" "));
}

console.log(result.join("\n"));
profile
이밀루의 도전

0개의 댓글