Atcode - ABC 212- D - Querying Multiset

D - Querying Multiset

Time Limit: 2 sec / Memory Limit: 1024 MB

Score : 400 points

Problem Statement

Takahashi has many balls, on which nothing is written, and one bag. Initially, the bag is empty. Takahashi will do Q operations, each of which is of one of the following three types.

Type 1: Write an integer Xi on a blank ball and put it in the bag.
Type 2: For each ball in the bag, replace the integer written on it with that integer plus Xi.
Type 3: Pick up the ball with the smallest integer in the bag (if there are multiple such balls, pick up one of them). Record the integer written on this ball and throw it away.
For each
1≤ i ≤ Q, you are given the type Pi of the i-th operation and the value of Xi
if the operation is of Type 1 or 2. Print the integers recorded in the operations of Type 3 in order.

Constraints

1 ≤ Q ≤ 2×10^5
1 ≤ Pi ≤ 3
1 ≤ Xi ≤ 10^9
All values in input are integers.
There is one or more i such that Pi=3.
If Pi = 3, the bag contains at least one ball just before the i-th operation.

Sample Input

5
1 3
1 5
3
2 2
3

Sample Output

3
7

Takahashi will do the following operations.

Write 3 on a ball and put it in the bag.
Write 5 on a ball and put it in the bag.
The bag now contains a ball with 3 and another with 5.
Pick up the ball with the smaller of them, or 3.
Record 3 and throw it away.The bag now contains just a ball with 5.
Replace this integer with 5+2=7.
The bag now contains just a ball with 7.
Pick up this ball, record 7, and throw it away.
Therefore, we should print 3 and 7, in the order they are recorded.

C++ code

analyze

观察本题,可以发现本题可以通过STL里面的优先队列(即堆) 的方式来解决, 因为本题op 3要输出最小值,所以要用小根堆
但是本题麻烦的地方在于操作2, 我们没有现成的操作可以直接让堆里的每一个元素plus,但是我们可以逆向思维,在每次要输出的时候将要输出的值加上sum就可以,不过这样每次要将插入根堆里的数减去sum,(因为会在输出的时候补上sum)就可以实现op 2

priority_queue

在这里插入图片描述

code
#include<iostream>
#include<algorithm>
#include<queue>
using namespace std;
int main(){
	priority_queue<long long, vector<long long>, greater<long long> > heap;
	int q, op;
	long long x;
	long long sum=0;
	cin >> q;
	for(int i=0; i<q; i++){
		cin >> op;
		if(p == 1){
			cin >> x;
			heap.push(x - sum);
		}
		else if(op == 2){
			cin >> x;
			sum += x;
		}
		else if(op == 3){
			cout << heap.top() + sum << endl;
			heap.pop();
		}
	}
	return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值