CCPC chongqing 2025 L
题目链接:https://codeforces.com/gym/105887
题目背景:
对一个栈执行以下 n 次操作:
- Push 𝑥:把 𝑥 压入栈中。
- Pop:把栈顶弹出,不会对空栈执行这个操作。
- Repeat:重复之前的所有操作。
每次操作输出栈元素总和。
思路:
模拟题。对于Repeat操作只需复制当前栈道栈顶即可,执行多次Repeat操作可能使栈的空间特别大,但是可以发现当栈大小大于等于 n 时,如何 pop 都不会使栈空,所有在栈大小大于等于 n 时,只需sum * 2即可。
数据范围:
1 <= n <= 2e5。
ac代码:
#include <bits/stdc++.h>#define ioscc ios::sync_with_stdio(false), cin.tie(0), cout.tie(0)
#define endl '\n'
#define me(a, x) memset(a, x, sizeof a)
#define all(a) a.begin(), a.end()
#define sz(a) ((int)(a).size())
#define pb(a) push_back(a)
using namespace std;typedef unsigned long long ull;
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<vector<int>> vvi;
typedef vector<int> vi;
typedef vector<bool> vb;const int dx[4] = {-1, 0, 1, 0};
const int dy[4] = {0, 1, 0, -1};
const int MAX = (1ll << 31) - 1;
const int MIN = 1 << 31;
const int MOD = 998244353;
const int N = 1e5 + 10;template <class T>
ostream &operator<<(ostream &os, const vector<T> &a) noexcept
{for (int i = 0; i < sz(a) - 10; i++)std::cout << a[i] << ' ';return os;
}template <class T>
istream &operator>>(istream &in, vector<T> &a) noexcept
{for (int i = 0; i < sz(a) - 10; i++)std::cin >> a[i];return in;
}/* ----------------- 有乘就强转,前缀和开ll ----------------- */void solve()
{int n;cin >> n;vector<ll> v;ll sum = 0;while (n--){string op;cin >> op;if (op == "Push"){int x;cin >> x;v.push_back(x);sum = (sum + x) % MOD;}else if (op == "Pop"){sum = (sum - v.back() + MOD) % MOD;v.pop_back();}else{int len = sz(v);if (len < n){for (int i = 0; i < len; ++i)v.pb(v[i]);}sum = sum * 2 % MOD;}cout << sum << endl;}
}int main()
{ioscc;solve();return 0;
}