mojo's Blog

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

백준/Binary Search

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

_mojo_ 2021. 7. 1. 01:02

문제 링크 => 19637번: IF문 좀 대신 써줘 (acmicpc.net)

 

19637번: IF문 좀 대신 써줘

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

www.acmicpc.net


이분탐색 문제이다.

 

이문제에서 Input 은 n 개의 (string, long long) 형태로 주어지는데 long long 값이 오름차순으로 주어지는 것에 대해서 눈여겨 볼 필요가 있다.

 

즉, 벡터에다가 값을 push한 후에 정렬하는 operation을 하지 않고 바로 이분탐색을 할 수 있다는 것이 핵심이다.

 

풀이 code

#define _CRT_SECURE_NO_WARNINGS
#include <string>
#include <stdio.h>
#include <math.h>
#include <iostream>
#include <vector>
#include <algorithm>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <time.h>
#define INF 100000000
#define endl '\n'

using namespace std;

int n, m;
vector<long long> v;
vector<string> strV;

void input() {
	cin >> n >> m;
	for (int i = 0; i < n; i++) {
		string s;
		long long x;
		cin >> s >> x;
		strV.push_back(s);
		v.push_back(x);
	}
}

void solve() {
	for (int i = 0; i < m; i++) {
		long long x;
		cin >> x;
		cout << strV[lower_bound(v.begin(),v.end(),x)-v.begin()] << endl;
	}
}

int main() {
	ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);

	input();
	solve();
	
	return 0;
}

 

Comments