跳到正文

题解

题解:AT_abc413_c [ABC413C] Large Queue

william1118约 3 分钟
原文
文章信息与版本

文章信息

发布
2025年7月6日
收录
2025年12月12日
更新
2026年11月16日
篇幅
约 3 分钟
获赞
1

版本

  1. v1当前

题意简述

有 QQ 次查询,每次查询有两种操作:

  1. 向序列末尾插入 cc 份 xx;
  2. 删除序列中的前 kk 个元素,并输出它们的和。

思路

可以开一个 deque 来维护所有操作。将数字 xx 和份数 cc 开个 pair 存进队尾,每次删除时不断弹出队首然后累加答案,最后更新剩下的数字的份数再重新加入队首即可。具体见代码。

AC code

C++
#include <iostream>
#include <algorithm>
#include <deque>

using namespace std;
using ll = long long;

int q;
deque<pair<int, int>> dq;

int main() {
    cin.tie(0)->sync_with_stdio(0);
    cin >> q;
    for (int i = 1, o, c, x, k; i <= q; i++) {
        cin >> o;
        if (o == 1) {
            cin >> c >> x;
            dq.push_back({c, x});
        } else {
            ll ans = 0;
            cin >> k;
            while (dq.size() && k >= dq.front().first) {          //如果还够删就一直删,并累加答案
                k -= dq.front().first;
                ans += 1ll * dq.front().first * dq.front().second;
                dq.pop_front();
            }
            if (dq.empty()) {
                cout << ans << '\n';
                continue;
            }
            // 处理剩下的数字
            ans += 1ll * k * dq.front().second;
            int nc = dq.front().first - k, nx = dq.front().second;//nc:还剩多少份
            dq.pop_front();
            dq.push_front({nc, nx});                              //重新加入队首
            cout << ans << '\n';
        }
    }
    return 0;
}

AC记录

原文