题解:AT_abc413_c [ABC413C] Large Queue

· · 题解

思路:

由于本题有大量的添加和删除操作,所以不能直接模拟,我们可以第一个结构体,记录队中的数和个数。删除时只要在个数上减即可,如果个数为 0 再出队。其它按题意模拟即可。

代码:

#include<bits/stdc++.h>
#define int long long
using namespace std;
struct node{
    int x,y;
};
queue<node> a;
int q;
signed main() {
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    cin >> q;
    while (q--) {
        int op,u,v;
        cin >> op;
        if (op==1) {
            cin >> u >> v;
            a.push({v,u});
        }
        else {
            cin >> u;
            int ans=0;
            while (u&&!a.empty()) {
                if (u>=a.front().y) {
                    ans+=a.front().x*a.front().y;
                    u-=a.front().y;
                    a.pop();
                }
                else {
                    ans+=a.front().x*u;
                    a.front().y-=u;
                    u=0;
                }
            }
            cout <<ans<<endl;
        }
    }
    return 0;
}