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;
}




  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值