hades

[Baekjoon] 7662๋ฒˆ: ์ด์ค‘ ์šฐ์„ ์ˆœ์œ„ ํ ๋ณธ๋ฌธ

๐Ÿ‘Š PS/Algorithm

[Baekjoon] 7662๋ฒˆ: ์ด์ค‘ ์šฐ์„ ์ˆœ์œ„ ํ

hades1 2024. 7. 17. 22:42

๐Ÿฅ… ๋ฌธ์ œ

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

 

๐Ÿ” ์„ค๊ณ„

์ฒ˜์Œ์— min_pq์™€ max_pq๋งŒ์„ ๋งŒ๋“ค์–ด์„œ D์ผ ๋•Œ n์ด ๋ฌด์—‡์ด๋ƒ์— ๋”ฐ๋ผ ํ•œ์ชฝ์œผ๋กœ ๋ชฐ์•„์„œ ์ฒ˜๋ฆฌํ•˜๋Š” ๋ฐฉ์‹์„ ํƒํ–ˆ๋Š”๋ฐ ์‹œ๊ฐ„ ์ดˆ๊ณผ๊ฐ€ ๋ฐœ์ƒํ–ˆ๋‹ค. ๋‹ค๋ฅธ ๋ฐฉ๋ฒ•์„ ์ฐพ์•„์•ผ ํ•œ๋‹ค.

 

map์„ ์ด์šฉํ•˜์—ฌ ๊ฐœ์ˆ˜๋ฅผ ์ €์žฅํ•˜๊ณ , D์ผ ๋•Œ, ์‚ญ์ œ๋ฅผ ํ•˜๊ธฐ ์ „, ๋‹ค๋ฅธ ๋ช…๋ น์—์„œ ๊ฐœ์ˆ˜๊ฐ€ 0์ด ๋˜์—ˆ์„ ์ˆ˜๋„ ์žˆ์œผ๋ฏ€๋กœ ๊ทธ ๊ฒฝ์šฐ๋ฅผ ๊ณ ๋ คํ•˜์—ฌ ์‚ญ์ œ ์ฒ˜๋ฆฌ๋ฅผ ํ•ด์ฃผ์–ด์•ผ ํ•œ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด D 1์ด์–ด์„œ max_pq์—์„œ๋Š” ์‚ญ์ œํ–ˆ์ง€๋งŒ, min_pq์—์„œ๋Š” ์‚ญ์ œํ•  ์ˆ˜ ์—†์œผ๋ฏ€๋กœ ๋ฐ˜์˜๋˜์ง€ ์•Š์•˜๋‹ค. ๊ฐœ์ˆ˜๋งŒ ๊ฐ์†Œ์‹œํ‚ค๊ณ , ์‚ญ์ œํ•˜๋ ค๋Š” ์ˆ˜์˜ ๊ฐœ์ˆ˜๊ฐ€ 0์ผ ๋•Œ, ๋ชจ๋‘ ์‚ญ์ œํ•œ๋‹ค.

 

๐Ÿ‘Š ํ’€์ด

#include <iostream>
#include <queue>
#include <map>
using namespace std;

char ch;
int t, k, n;

int main(void)
{	
	cin >> t;
	for (int i = 0; i < t; i++) {
		cin >> k;
		priority_queue<int, vector<int>, greater<int>> min_pq;
		priority_queue<int> max_pq;
		map<int, int> save;
		for (int j = 0; j < k; j++) {
			cin >> ch >> n;
			if (ch == 'I') {
				min_pq.push(n);
				max_pq.push(n);
				if (save.count(n) == 0){
					save[n] = 1;
				}
				else{
					save[n] += 1;
				}
			}
			else {
				if (n == 1) {
					while (!max_pq.empty() && save[max_pq.top()] == 0){
						max_pq.pop();
					}
					if (!max_pq.empty()){
						save[max_pq.top()] -= 1;
						max_pq.pop();
					}
				}
				else {
					while (!min_pq.empty() && save[min_pq.top()] == 0){
						min_pq.pop();
					}
					if (!min_pq.empty()){
						save[min_pq.top()] -= 1;
						min_pq.pop();
					}
				}
			}
		}
		while (!max_pq.empty() && save[max_pq.top()] == 0){
			max_pq.pop();
		}
		while (!min_pq.empty() && save[min_pq.top()] == 0){
			min_pq.pop();
		}
		if (max_pq.empty() && min_pq.empty()){
			cout << "EMPTY" << "\n";
		}
		else{
			cout << max_pq.top() << " " << min_pq.top() << "\n";
		}
	}
	return 0;
}

'๐Ÿ‘Š PS > Algorithm' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

[Baekjoon] 10026๋ฒˆ: ์ ๋ก์ƒ‰์•ฝ  (2) 2024.07.22
[Baekjoon] 9095๋ฒˆ: 1, 2, 3 ๋”ํ•˜๊ธฐ  (1) 2024.07.20
[Baekjoon] 7569๋ฒˆ: ํ† ๋งˆํ†   (0) 2024.07.16
[Baekjoon] 5525๋ฒˆ: IOIOI  (0) 2024.07.15
[Baekjoon] 5430๋ฒˆ: AC  (0) 2024.07.15