[BOJ] 20551 Sort 마스터 배지훈의 후계자
Post
취소

[BOJ] 20551 Sort 마스터 배지훈의 후계자

문제 요약 및 풀이

20551번: Sort 마스터 배지훈의 후계자

이진탐색을 잘 하자.

풀이 코드

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
#include <bits/stdc++.h>

#define for1(s,n) for(int i=s; i<n; i++)

using namespace std;

int N, M, a;
int ar[220000];

int main() {
  ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
  cin >> N >> M;

  for1(0, N) {
    cin >> ar[i];
  }

  sort(ar, ar+N);

  for1(0, M) {
    cin >> a;
    int *p = lower_bound(ar, ar+N, a);
    if(p != ar + N && *p == a) cout << p - ar;
    else cout << -1;
    cout << '\n';
  }
}

This post is licensed under CC BY 4.0 by the author.