https://www.acmicpc.net/problem/19637
19637번: IF문 좀 대신 써줘
첫 번째 줄에는 칭호의 개수 N (1 ≤ N ≤ 105)과 칭호를 출력해야 하는 캐릭터들의 개수 M (1 ≤ M ≤ 105)이 빈칸을 사이에 두고 주어진다. (1 ≤ N, M ≤ 105) 두 번째 줄부터 N개의 줄에 각 칭
www.acmicpc.net
생각해 보기
- 단순히 순차탐색의 경우 10^5 * 10*5으로 최악의 경우 시간초과가 발생한다.
- 이미 들어온 칭호 값의 경우 무시하고 넘어간다.
- 빠르게 값을 찾기 위해선 이분 탐색을 이용해야 한다.
- map의 lower_bound와 upper_bound를 이용하자.
코드
#include<iostream>
#include<map>
using namespace std;
map<int, string> titles;
int N,M;
void input(){
string name;
int power;
cin>>N>>M;
while(N--){
cin>>name>>power;
if(titles.find(power) != titles.end())
continue;
titles[power]=name;
}
}
void solution(){
int power;
while(M--){
cin>>power;
auto it = titles.lower_bound(power);
cout<<(*it).second<<'\n';
}
}
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
input();
solution();
return 0;
}

728x90
'알고리즘 > 백준 문제' 카테고리의 다른 글
| 20006 - 랭킹전 대기열 (0) | 2023.03.08 |
|---|---|
| 22233 - 가희와 키워드 (0) | 2023.03.08 |
| 20310 - 타노스 (0) | 2023.03.07 |
| 3758 - KCPC (0) | 2023.03.06 |
| 2607 - 비슷한 단어 (0) | 2023.03.06 |
댓글