NTHU OJ 10924

Program link: http://acm.cs.nthu.edu.tw/problem/10924/

Description

實作Max heap的三種操作:push, pop, top。

 

指令0 x:代表push,將x push進max heap。

指令1 : 代表pop,將最大的數字pop出來,若heap為空則忽略這道指令。

指令2 : 代表top,將最大的數字印出來,若heap為空則忽略這道指令。

Input

本題只有一道測資。

    測資第一行為一個數字N,代表接下來有N行指令。每行指令個格式如題目敘述。

 

所有push的數字皆不相同。

 

0 < N < 20000

0 < x < 10000000

Output

將所有top指令輸出的數字作加總,最後輸出這個和。

Hint : 注意overflow!

Sample Input

15
1
0 9550684
0 8533293
1
2
2
1
0 505825
0 9809892
0 1484329
0 4958409
0 3788064
0 28006
2
0 2979864

EOF

Sample Output

26876478 
EOF

Code:

</pre><pre name="code" class="cpp">#include <iostream>
#include <queue>

using namespace std;

int main()
{
	int N;
	cin >> N;
	priority_queue <int> pq;
	int singal;
	int x;
	long long sum = 0;
	while (N--)
	{
		cin >> singal;
		if (!singal)
		{
			cin >> x;
			pq.push(x);
		}
		else if (singal == 1)
		{
			if (pq.size())
			{
				pq.pop();
			}
		}
		else
		{
			if (pq.size())
			{
				sum+=pq.top();
			}
		}
	}
	cout<<sum<<endl;
    return 0;
}




發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章