티스토리 뷰

https://www.acmicpc.net/problem/1269

 

1269번: 대칭 차집합

첫째 줄에 집합 A의 원소의 개수와 집합 B의 원소의 개수가 빈 칸을 사이에 두고 주어진다. 둘째 줄에는 집합 A의 모든 원소가, 셋째 줄에는 집합 B의 모든 원소가 빈 칸을 사이에 두고 각각 주어

www.acmicpc.net

const fs = require('fs');
const input = fs.readFileSync("./dev/stdin").toString().trim().split("\n").map(v => v.split(' ').map(Number))
const [N, M] = input.shift();
const A = input.shift().sort((a, b) => a - b);
const B = input.shift().sort((a, b) => a - b);

let answer = A.length + B.length;
A.forEach(a => {
  let min = 0;
  let max = B.length - 1;
  let find = false;
  while (min <= max) {
    let mid = Math.floor((max + min) / 2);
    if (B[mid] == a) {
      find = true;
      break;
    } else if (B[mid] > a) {
      max = mid - 1;
    } else {
      min = mid + 1;
    }
  }
  if (find) answer--;
})

B.forEach(b => {
  let min = 0;
  let max = A.length - 1;
  let find = false;
  while (min <= max) {
    let mid = Math.floor((max + min) / 2);


    if (A[mid] == b) {
      find = true;
      break;
    } else if (A[mid] > b) {
      max = mid - 1;
    } else {
      min = mid + 1;
    }
  }
  if (find) answer--;
})

console.log(answer)

 

728x90
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2025/05   »
1 2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28 29 30 31
글 보관함