반응형
https://jungol.co.kr/bbs/board.php?bo_table=pbank&code=4639&sca=5010
#include <bits/stdc++.h>
using namespace std;
int main()
{
set <int> s;
int n;
cin >> n;
set<int>::iterator it,p,b;
while(n--){
char cmd;
int a;
cin >> cmd >> a;
if(cmd == 'i') s.insert(a);
else if(cmd == 'r') s.erase(a);
else if(cmd == 'f'){
it = s.find(a);
if(it != s.end()){
if(it!=s.begin()){
p = prev(it);
b = next(it);
if(b != s.end()){
if(abs(*p-a)<=abs(*b-a)) cout << *p << "\n";
else cout <<*b<< "\n";
}
else cout << *p<< "\n";
}
else{
b = next(it);
if(b != s.end()){
cout << *b<< "\n";
}
else cout << "UNIQUE\n";
}
}
else {
cout << "NOPE\n";
}
}
}
return 0;
}
iterator 를 사용하여 찾는 위치를 찾은 후 prev 또는 next 를 사용하여 주변의 값을 찾는다.
반응형
'정올문제풀이' 카테고리의 다른 글
4640 : Tutorial: STL Set 4 (0) | 2023.05.28 |
---|---|
4989 : Tutorial: STL Map (0) | 2023.05.28 |
4637 : Tutorial: STL Set 1 (0) | 2022.10.22 |
4905 : STL - Priority Queue (0) | 2022.10.20 |
5203 : 식사 계획 세우기 (0) | 2022.10.18 |