Rylah's Study & Daily Life

[SWEA/C++] 2930. 힙 본문

SW Expert Academy

[SWEA/C++] 2930. 힙

Rylah 2022. 4. 4. 01:58

https://swexpertacademy.com/main/code/problem/problemDetail.do?contestProbId=AV-Tj7ya3jYDFAXr 

 

SW Expert Academy

SW 프로그래밍 역량 강화에 도움이 되는 다양한 학습 컨텐츠를 확인하세요!

swexpertacademy.com

 

BOJ의 최소 힙과 문제가 유사하다.

https://www.acmicpc.net/problem/1927

 

1927번: 최소 힙

첫째 줄에 연산의 개수 N(1 ≤ N ≤ 100,000)이 주어진다. 다음 N개의 줄에는 연산에 대한 정보를 나타내는 정수 x가 주어진다. 만약 x가 자연수라면 배열에 x라는 값을 넣는(추가하는) 연산이고, x가 0

www.acmicpc.net

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#include <bits/stdc++.h>
using namespace std;
 
int main(void)
{
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
 
    int testCase;
    cin >> testCase;
    for (int t = 1; t <= testCase; t++)
    {
        priority_queue<intvector<int>, less<>> pq;
        int n;
        cin >> n;
        vector<int> res;
        for (int i = 0; i < n; i++)
        {
            int cmd;
            cin >> cmd;
            
            switch (cmd)
            {
            case 1:
                int input;
                cin >> input;
                pq.emplace(input);
                break;
            case 2:
                if (pq.empty())
                {
                    res.emplace_back(-1);
                }
                else
                {
                    res.emplace_back(pq.top());
                    pq.pop();
                }
                break;
            }
        }
        cout << "#" << t << " ";
        for (auto& e : res)
            cout << e << " ";
        cout << "\n";
    }
 
    return 0;
}
cs

'SW Expert Academy' 카테고리의 다른 글

[SWEA/C++] 13547. 팔씨름  (0) 2022.04.04