본문 바로가기

백준/실버

[백준 19637번] IF문 좀 대신 써줘

문제링크 : https://www.acmicpc.net/problem/19637

 

19637번: IF문 좀 대신 써줘

첫 번째 줄에는 칭호의 개수 N (1 ≤ N ≤ 105)과 칭호를 출력해야 하는 캐릭터들의 개수 M (1 ≤ M ≤ 105)이 빈칸을 사이에 두고 주어진다. (1 ≤ N, M ≤ 105) 두 번째 줄부터 N개의 줄에 각 칭

www.acmicpc.net

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define MAX 987654321
#define pii pair <int, int>

int main(void) {
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    
    int N, M;
    cin >> N >> M;

    map <int, string> m;

    for(int i=0; i<N; i++)
    {
        string s;
        int power;
        cin >> s >> power;  
        m.insert({power, s});  //key, value
    }

    int num;
    for(int i=0; i<M; i++)
    {   
        cin >> num;
        cout << m.lower_bound(num) -> second << "\n";  //해당 변수보다 낮거나 같은 값 탐색
    }
    return 0;
}

map을 이용해 {key : power, value : s} 한 쌍을 입력받는다.

이후 입력받은 num을 lower_bound(이진탐색) 함수를 통해 탐색하고, 해당 값에 대한 value을 출력한다.