Time Limit: 2 sec / Memory Limit: 1024 MB
Score :400 points
Problem Statement
We have a horizontal cylinder. Given Q queries, process them in the given order.
Each query is of one of the following two types.
1 x c
: Insert c balls, with a number x written on each of them, to the right end of the cylinder.2 c
: Take out the c left most balls contained in the cylinder and print the sum of the numbers written on the balls that have been taken out.
We assume that the balls do never change their order in the cylinder.
Constraints
- 1≤Q≤2×10^5
- 0≤x≤10^9
- 1≤c≤10^9
- Whenever a query of type
2 c
is given, there are c or more balls in the cylinder. - All values in input are integers.
Input
Input is given from Standard Input in the following format:
Q query1 queryQ
The i-th query{i} is in one of the following two formats.
1 x c
2 c
Output
Print the response to the queries of type 2 c
in the given order, with newlines in between.
Sample Input 1 Copy
Copy
4 1 2 3 2 2 1 3 4 2 3
Sample Output 1 Copy
Copy
4 8
- For the 1-st query, insert 3 balls, with a number 2 written on each of them, to the right end of the cylinder.
The cylinder has now balls with numbers (2,2,2) written on them, from left to right. - For the 2-nd query, take out the 2 leftmost balls contained in the cylinder.
The numbers written on the balls taken out are 2,2, for a sum of 4, which should be printed. The cylinder has now a ball with a number (2)(2) written on it, from left to right. - For the 3-rd query, insert 4 balls, with a number 33 written on each of them, to the right end of the cylinder.
The cylinder has now balls with numbers (2,3,3,3,3) written on them, from left to right. - For the 4-th query, take out the 3 leftmost balls contained in the cylinder.
The numbers written on the balls taken out are (2,3,3) for a sum of 8, which should be printed. The cylinder has now balls with numbers (3,3) written on them, from left to right.
Sample Input 2 Copy
Copy
2 1 1000000000 1000000000 2 1000000000
Sample Output 2 Copy
Copy
1000000000000000000
Sample Input 3 Copy
Copy
5 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
Sample Output 3 Copy
Copy
There may be nothing you should print.
题目翻译:给出两个操作,1 x c表示:在一个序列最后添加 c个x
2 c 表示在序列的最前面删掉c个数 并求出这c个数的和
思路:很明显是一个队列啊,但是要思考一下怎么取写这个队列,如果直接用数组模拟,然后开那么多个数组,肯定会爆内存,那么我们如何存储呢,直接开一个二元组的队列即可,第一元x存储个数c,第二元存储大小x。每次进行删除时,先判断队头的个数是不是比删除的个数大,如果大,只删除c个,否则就全部删除,然后将这个队头弹出
见代码
#include <bits/stdc++.h>
#define x first
#define y second
using namespace std;
typedef long long ll;
typedef pair<int,int> PII;
int n;
queue<PII> q;
int main()
{
cin >> n;
for (int i = 0; i < n; i ++ )
{
int a, b;
cin >> a >> b;
if (a == 1)
{
int c;
cin >> c;
q.push({c, b});//在这里插入c个b
}
else
{
ll res = 0;
while (b)
{
int d = min(b, q.front().x);//先看一下要取的数和当前队头的b的个数的大小
res += 1ll*q.front().y * d;
b -= d;
q.front().x -= d;
if (q.front().x == 0) q.pop();
}
cout << res << endl;
}
}
return 0;
}