๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
Algorithm ๐Ÿง‘๐Ÿปโ€๐Ÿ’ป/๋ฐฑ์ค€(BOJ)

[๋ฐฑ์ค€,c++] 11286๋ฒˆ - ์ ˆ๋Œ“๊ฐ’ ํž™

by dkswnkk 2021. 10. 27.
 

11286๋ฒˆ: ์ ˆ๋Œ“๊ฐ’ ํž™

์ฒซ์งธ ์ค„์— ์—ฐ์‚ฐ์˜ ๊ฐœ์ˆ˜ N(1โ‰คNโ‰ค100,000)์ด ์ฃผ์–ด์ง„๋‹ค. ๋‹ค์Œ N๊ฐœ์˜ ์ค„์—๋Š” ์—ฐ์‚ฐ์— ๋Œ€ํ•œ ์ •๋ณด๋ฅผ ๋‚˜ํƒ€๋‚ด๋Š” ์ •์ˆ˜ x๊ฐ€ ์ฃผ์–ด์ง„๋‹ค. ๋งŒ์•ฝ x๊ฐ€ 0์ด ์•„๋‹ˆ๋ผ๋ฉด ๋ฐฐ์—ด์— x๋ผ๋Š” ๊ฐ’์„ ๋„ฃ๋Š”(์ถ”๊ฐ€ํ•˜๋Š”) ์—ฐ์‚ฐ์ด๊ณ , x๊ฐ€ 0

www.acmicpc.net

#include <iostream>
#include <queue>
using namespace std;
struct compare {
bool operator()(int x, int y) {
if (abs(x) == abs(y)) return x > y;
else return abs(x) > abs(y);
}
};
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
priority_queue<int, vector<int>, compare>pq;
int N; cin >> N;
while (N--) {
int x; cin >> x;
if (x != 0) pq.push(x); //์ž…๋ ฅ์ด 0์ด ์•„๋‹๋•Œ
else { //์ž…๋ ฅ์ด 0์ผ๋•Œ
if (pq.empty()) cout << "0" << "\n"; //ํ๊ฐ€ ๋น„์–ด์žˆ๋‹ค๋ฉด 0 ์ถœ๋ ฅ
else { //๋น„์–ด์žˆ์ง€ ์•Š๋‹ค๋ฉด
cout << pq.top()<<'\n'; //์ตœ์†Ÿ๊ฐ’ ์ถœ๋ ฅํ•˜๊ณ 
pq.pop(); //์‚ญ์ œ
}
}
}
}

GitHub

LinkedIn

GitHub

LinkedIn