TA的每日心情 | 擦汗 半小时前 |
---|
签到天数: 37 天 [LV.5]常住居民I
注册会员

- 积分
- 67
|
一、问题背景二、核心算法思路三、完整代码解析(带注释)
- #include <iostream>
- #include <map>
- using namespACe std;
- const int MOD = 998244353;
- int main() {
- ios::sync_with_stdio(false);
- cin.tie(nullptr);
-
- int n, m, k;
- cin >> n >> m >> k;
-
- map<int, long long> cnt; // 体积->数量映射
-
- // 初始化统计
- for (int i = 0; i < n; ++i) {
- int v;
- cin >> v;
- cnt[v]++;
- }
-
- for (int day = 0; day < m; ++day) {
- int x, y;
- cin >> x >> y;
-
- map<int, long long> new_cnt;
- long long sum = 0;
-
- // 处理现有冰山
- for (auto &[v, num] : cnt) {
- int new_v = v + x;
- if (new_v <= 0) continue;
-
- if (new_v > k) {
- new_cnt[k] = (new_cnt[k] + num) % MOD;
- new_cnt[1] = (new_cnt[1] + (new_v - k) * num) % MOD;
- } else {
- new_cnt[new_v] = (new_cnt[new_v] + num) % MOD;
- }
- }
-
- // 添加新冰山
- if (y > 0) {
- new_cnt[y] = (new_cnt[y] + 1) % MOD;
- }
-
- cnt = move(new_cnt);
-
- // 计算总和
- long long total = 0;
- for (auto &[v, num] : cnt) {
- total = (total + v * num) % MOD;
- }
-
- cout << total << "\n";
- }
-
- return 0;
- }
复制代码
四、关键算法点详解五、复杂度分析时间复杂度:O(m * S) S为每日不同体积的数量
六、实际应用场景这种映射统计方法广泛应用于: 游戏中的粒子系统模拟 物理仿真中的质量分布计算 资源管理中的分类统计 大数据中的分桶计数
来源:蓝桥杯2021国赛A组冰山问题:冰山模拟问题的映射统计解法
|
|