Atcode – ABC 212- D – Querying Multiset

  • Post author:
  • Post category:其他




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



版权声明:本文为weixin_51942493原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。