Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 8653e81

Browse files
Create BOJ7662.md
1 parent d1282af commit 8653e81

File tree

1 file changed

+69
-0
lines changed

1 file changed

+69
-0
lines changed

‎challenges/BOJ7662.md‎

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
# 문제
2+
이중 우선순위 큐
3+
## 문제 원본
4+
문제의 원본은 [여기서](https://www.acmicpc.net/problem/7662) 확인하세요.
5+
6+
## 분류
7+
* 자료구조 (MULTI SET)
8+
9+
# 풀이
10+
11+
`C++ STL``multiset`은 내부적으로 `Red Black Tree`를 이용한다. 즉 이진 탐색 트리이기 떄문에 정렬되어 있다고 볼 수 있고 균형 트리로 O(log(N)) 시간을 보장한다.
12+
이 문제에서 삽입 연산의 경우 `multiset`의 단순 삽입 하며, 삭제 연산의 경우는 최대 값 삭제는 `multiset``iterator`를 통해 마지막값,
13+
최소값 삭제의 경우는 처음 값을 삭제한다.
14+
15+
``` c++
16+
#include <iostream>
17+
#include <set>
18+
19+
using namespace std;
20+
21+
int main(void) {
22+
23+
multiset<int> s;
24+
25+
int T;
26+
cin >> T;
27+
28+
29+
for (int i = 0; i < T; i++) {
30+
int nOp;
31+
cin >> nOp;
32+
33+
char op;
34+
int v;
35+
for (int j = 0; j < nOp; j++) {
36+
cin >> op >> v;
37+
38+
if (op == 'I') {
39+
s.insert(v);
40+
}
41+
else if (op == 'D') {
42+
if (!s.empty()) {
43+
// max-delete
44+
if (v == 1) {
45+
s.erase(--(s.end()));
46+
}
47+
// min-delete
48+
else if (v == -1) {
49+
s.erase(s.begin());
50+
}
51+
}
52+
}
53+
54+
}
55+
56+
if (s.empty()) {
57+
cout << "EMPTY" << endl;
58+
}
59+
else {
60+
cout << *(--s.end()) << " " << *s.begin() << endl;
61+
}
62+
63+
s.clear();
64+
}
65+
66+
67+
return 0;
68+
}
69+
```

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /