AtCoder Beginner Contest 212 D - Querying Multiset (优先队列)

D - Querying Multiset

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.

Input

Input is given from Standard Input in the following format:

Q
query1
query2
:
queryQ

Each queryi in the 2-nd through (Q+1)-th lines is in the following format:

1 Xi
2 Xi
3

The first number in each line is 1≤Pi≤3, representing the type of the operation. If Pi=1 or Pi=2, it is followed by a space, and then by Xi.

Output

For each operation with Pi=3 among the Q operations, print the recorded integer in its own line.

Sample Input 1

5
1 3
1 5
3
2 2
3

Sample Output 1

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.

Sample Input 2

6
1 1000000000
2 1000000000
2 1000000000
2 1000000000
2 1000000000
3

Sample Output 2

5000000000

Note that the outputs may not fit into a 32-bit integer.

题目大意:

Takahashi可以进行三种操作:

  1. 将一个写有整数X的球放入袋子内;
  2. 将袋子里所有球上的数字加上X;
  3. 拿出袋子内数字最小的球并记录这个数字。
    对于每一个操作3,输出记录的数字。

题解:

因为每一次都要拿出容器中的最小值,所以可以利用优先队列来作为容器。定义递增的优先队列:

priority_queue <ll, vector<ll>, greater<ll> > qu; 

这样可以保证最小值在队首。
对于操作3我们只需要将队首出队并输出即可;
对于操作1我们将Xpush入队;
对于操作2,如果我们暴力修改所有袋子里的球,算上多组样例,时间复杂度为O(n^2),会超时。换一种思路,要把袋子里所有的球加上X,可以先将后面加入的球都减去X再入队,出队的时候再加上X复原,也可以实现题目的要求,且时间复杂度为O(1)。我们可以使用一个变量sum来累加减掉的X,每次入队都入队x - sum,出队输出x + sum即可。

注意这题数据会很大,要开long long。

AC代码:

#include <iostream>
#include <queue>
using namespace std;
typedef long long ll;
const int N = 1e6 + 5;
ll a[N];
int main() {
    ios::sync_with_stdio(false);
	ll q;
	cin >> q;
	priority_queue <ll, vector<ll>, greater<ll> > qu; // 递增的优先队列 
	ll sum = 0; // 累加差值 
	while (q --) {
		ll f, x;
		cin >> f;
		if (f == 1) {
			cin >> x;
			qu.push(x - sum); // 入队x-sum, 相当于袋子里每个球都加sum 
		}
		else if (f == 2) {
			cin >> x; // 袋子里的球都加数相当于后面入队的球都减数 
			sum += x; // 差值累计在sum里 
		}
		else {
			cout << qu.top() + sum << '\n'; // 出队时加上sum复原 
			qu.pop();
		}
	}
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值