Thief of Wealth

www.acmicpc.net/problem/1920

 

1920번: 수 찾기

첫째 줄에 자연수 N(1≤N≤100,000)이 주어진다. 다음 줄에는 N개의 정수 A[1], A[2], …, A[N]이 주어진다. 다음 줄에는 M(1≤M≤100,000)이 주어진다. 다음 줄에는 M개의 수들이 주어지는데, 이 수들이 A안

www.acmicpc.net

 

위 문제를 풀었던 소스 코드이다.

 

'use strict'

const readline = require("readline");
const rl = readline.createInterface({
    input: process.stdin,
    output: process.stdout,
});

const binarySearch = (array, target, start, end) => {
    let mid = -1;
    while (start <= end) {
        mid = Math.floor((start + end) / 2);
        if (array[mid] === target) {
            return mid;
        } else if (array[mid] > target) {
            end = mid - 1;
        } else {
            start = mid + 1;
        }
    }
    return -1;
}

const solution = function (input) {
    const n = parseInt(input[0]);
    const A = input[1].split(" ").map(e => parseInt(e));
    const m = parseInt(input[2]);
    const B = input[3].split(" ").map(e => parseInt(e));
    let ans = '';
    A.sort((a, b) => a - b);

    B.forEach(
        (elem, idx) => {
            if (binarySearch(A, elem, 0, n - 1) !== -1) {
                ans += idx === m - 1 ? '1' : "1\n";
            } else {
                ans += idx === m - 1 ? '0' : '0\n';
            }
        }
    );

    console.log(ans);
};


const input = [];
rl.on("line", function (line) {
    input.push(line);
}).on("close", function () {
    solution(input);
    process.exit();
});
profile on loading

Loading...