-
11286, Silver 1백준 2021. 10. 3. 01:54
11286번: 절댓값 힙
첫째 줄에 연산의 개수 N(1≤N≤100,000)이 주어진다. 다음 N개의 줄에는 연산에 대한 정보를 나타내는 정수 x가 주어진다. 만약 x가 0이 아니라면 배열에 x라는 값을 넣는(추가하는) 연산이고, x가 0
www.acmicpc.net
문제
- 절댓값 힙을 구현하라
풀이
비교함수 연습문제
1234567891011121314151617181920212223242526272829303132333435363738#include <bits/stdc++.h>#define endl "\n"#define ooop(i, n) for(int i = 0; i < n; i++)#define loop(i, n) for(int i = 1; i <= n; i++)using namespace std;typedef long long ll;typedef pair<int, int> pii;typedef pair<ll, ll> pll;struct cmp{bool operator() (int& parent, int& me){return abs(parent) > abs(me) || abs(parent) == abs(me) && parent > me;}};int main(){ios::sync_with_stdio(false);cin.tie(0), cout.tie(0);int n;cin >> n;priority_queue<int, vector<int>, cmp> pq;int cmd;while(n--){cin >> cmd;if(cmd) pq.push(cmd);else{if(pq.empty()) cout << 0 << endl;else{cout << pq.top() << endl; pq.pop();}}}return 0;}cs'백준' 카테고리의 다른 글
2042, Gold 1 (0) 2021.10.07 16236, Gold 4 (0) 2021.10.03 15589, Gold 1 (0) 2021.10.03 2170, Gold 5 (0) 2021.10.03 16768, Gold 4 (0) 2021.09.21