const input = require('fs').readFileSync('/dev/stdin').toString().split('\n');
// const input = require('fs').readFileSync('./input.txt').toString().split('\n');

let a = [];
let b = [];
let z;
const bs = (left, right) => {
  const mid = Math.floor((left + right) / 2); 
  if(left > right) {
    return 0;
  }
  let tval = a[mid]; 
  if(tval > z) {
    return bs(left, mid-1);
  } else if(tval < z){
    return bs(mid+1, right);
  } else {
    return 1;
  }
}

for(let i=0;i<Number(input[0]);++i) {
  a = input[i*4+2].split(' ').map(x => parseInt(x));
  b = input[i*4+4].split(' ').map(x => parseInt(x));
  a.sort();
  for(const v of b) {
    z = v;
    console.log(bs(0, Number(input[i*4+1]-1)));
  }
}
#include <iostream>
#include <algorithm>
#include <vector>

using namespace std;

int arr[1000010];
int cmp;
int bs(int left, int right) {
  int mid = (left + right) / 2;
  if(left > right) {
    return 0;
  }
  int tval = arr[mid];
  if(tval > cmp) {
    return bs(left, mid-1);
  } else if(tval < cmp) {
    return bs(mid+1, right);
  } else {
    return 1;
  }
}

int main(void) {
  int t;
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);

  cin >> t;
  while(t--) {
    int a, b;
    cin >> a;
    for(int i=0;i<a;++i) {
      cin >> arr[i];
    }
    sort(arr,arr+a);
    cin >> b;
    for(int i=0;i<b;++i) {
      cin >> cmp;
      cout << bs(0,a-1) << '\n';
    }
  }
}

js로 똑같이 구현해도 시간초과가 났다. 질의응답에서 데이터 입력이 많아 입력속도가 중요하다고 하는데 javascript로 어떻게 올려야 할지 모르겠다.

'알고리즘' 카테고리의 다른 글

백준 2210 숫자판 점프 c++  (0) 2020.09.15
백준 2933 미네랄 C++ 풀이  (0) 2020.09.12
[javascript] b1072.js  (0) 2020.09.03
[프로그래머스] 정수삼각형 c++ 재귀로 풀기  (0) 2020.07.04
[DC] 백준 1992 쿼드트리  (0) 2018.04.29

+ Recent posts