跳到正文

个人记录

代码源模拟赛 day8 总结

liuyi0905约 17 分钟
原文
文章信息与版本

文章信息

发布
2026年8月17日
收录
2026年8月28日
更新
2026年11月17日
篇幅
约 17 分钟

版本

  1. v1当前

比赛情况

时间时长赛制题数估分得分排名
2026-8-16 8:004.5hIOI6/348/60074/279

回顾

8 : 00,比赛开始。

8 : 01,打开了 A 题题面。

8 : 10,打开了 B 题题面。

8 : 20,发现 A 题好像直接用一个 queue 维护一下就做完了。

8 : 30,通过 A 题。

8 : 40,B 题数据范围好像很小?或许可以暴力跑 qq 遍 dijkstra?

8 : 50,写完代码,WA 了。

9 : 10,优先队列权值和编号好像写反了?

9 : 20,TLE 了。

9 : 30,改成 3232 个分层图写法,还是 T 了。

9 : 35,貌似不需要跑 qq 遍,可以建反图跑多起点的。

9 : 40,通过 B 题。

9 : 45,打开了 E 题题面。听说就是一个简单的平衡树,但看完题后直接吓哭了。

9 : 50,打开了 D 题题面。但好像没啥思路。

10 : 10,打开了 C 题题面。

10 : 20,将天数视为边权,直接矩阵快速幂。

10 : 30,通过 C 题。

10 : 40,想到 D 题做法,但貌似卡空间?

11 : 20,写了一个 kruskal 重构树 + 倍增的做法,果然 MLE 了。

11 : 30,将倍增求 lca 改成了树剖,得到 40 分,还是 MLE 了。

11 : 50,发现是栈溢出的问题,开始思考解决方案。

12 : 10,将线段树改成了 ST 表,MLE 了。

12 : 15,拼劲全力无法战胜 D 题了,出题人我【】。

12 : 20,打开了 F 题题面。

12 : 29,直接乱输出,成功写出 8 分代码。

12 : 30,比赛结束。

题目复盘

A 蜗蜗的序列还原

得分 100,用时 20min。

可以用大的数放在小的数前面从而消掉小的数,考虑从小到大枚举每一个数,用一个 queue 维护当前无法被消掉的数,可以用比它大且有剩余的数来抵消。

代码
C++
#include <bits/stdc++.h>
using namespace std;
using LL = long long;
const int N = 6e5 + 5;
int t, n, a[N], c[N];
int main() {
  cin.tie(0)->sync_with_stdio(0);
  for (cin >> t; t; t--) {
    cin >> n, n *= 2;
    fill(c + 1, c + n + 1, 0);
    for (int i = 1; i <= n; i++) cin >> a[i], c[a[i]]++;
    queue<int> q, s;
    for (int i = 1; i <= n; i++) {
      if (!c[i]) continue;
      if (c[i] < 2) q.push(i);
      else {
        s.push(i), c[i] -= 2;
        for (; q.size() && c[i]; q.pop(), c[i]--) s.push(q.front());
        for (; c[i] >= 2; c[i] -= 2) s.push(i);
        c[i] && (q.push(i), 1);
      }
    }
    if (q.empty()) {
      cout << "Yes\n";
      for (; s.size(); s.pop()) cout << s.front() << " ";
      cout << "\n";
    } else cout << "No\n";
  }
  return 0;
}

B 符文大陆

得分 100,用时 1h。

可以建分层图,对于一个点 Sn+xSn + x,表示点的编号为 xx,且购买的符文的二进制集合为 SS。那么对于一条边 (u,v,w)(u, v, w), 有两种边:

  1. 不在这条边上使用符文,连边 (Sn+u,Sn+v,w)(Sn + u, Sn + v, w)。
  2. 在这条边上使用符文,设符文为 k(k∉S)k(k \notin S),连边 (Sn+u,(S+2k)n+v,w⋅(1−k10))(Sn + u, (S + 2 ^ k)n + v, w \cdot (1 - \frac{k}{10}))。

直接跑 dijkstra 即可。但可以发现没有必要跑 qq 遍,考虑建反图,将求 ss 到 pip_i 的最短路转化为求 pip_i 到 ss 的最短路。跑一个多起点的 dijkstra,求出所有 pip_i 到每一个 ss 的最短路。

代码
C++
#include <bits/stdc++.h>
using namespace std;
using LL = long long;
using Arr = array<LL, 3>;
using Pii = pair<LL, LL>;
const int N = 5005 * 32;
LL n, m, k, t, p[N], d[N], h[32];
vector<pair<LL, LL>> e[N];
int main() {
  cin.tie(0)->sync_with_stdio(0);
  cin >> n >> m >> k;
  priority_queue<Pii, vector<Pii>, greater<Pii>> q;
  fill(d + 1, d + 32 * n + 1, 1e18);
  for (int i = 1; i <= k; i++) {
    cin >> p[i], p[i]++, q.push({d[p[i]] = 0, p[i]});
  }
  for (LL u, v, w; m; m--) {
    cin >> u >> v >> w, u++, v++;
    for (int i = 0; i < 32; i++) {
      e[i * n + v].push_back({i * n + u, w});
      for (int j = 0; j < 5; j++) {
        !((i >> j) & 1) && (e[i * n + v].push_back({(i + (1 << j)) * n + u, w / 10 * (9 - j)}), 1);
      }
    }
  }
  for (; q.size();) {
    auto [z, u] = q.top();
    q.pop();
    for (auto [v, w] : e[u]) {
      d[u] + w < d[v] && (q.push({d[v] = d[u] + w, v}), 1);
    }
  }
  cin >> t;
  for (LL s, c[5]; t; t--) {
    cin >> s, s++;
    for (int i = 0; i < 5; i++) cin >> c[i];
    for (int i = 0; i < 32; i++) {
      h[i] = 0;
      for (int j = 0; j < 5; j++)
        (i >> j) & 1 && (~c[j] ? h[i] += c[j] : h[i] = 1e18);
    }
    LL r = 1e18;
    for (int i = 0; i < 32; i++) r = min(r, d[i * n + s] + h[i]);
    cout << (r != 1e18 ? r : -1) << "\n";
  }
  return 0;
}

C 蜗蜗的远足计划

得分 100,用时 30min。

将每一条道路建成的时间设为边权,那么 xx 要沿着一条路径走到 yy,其要求的最早时间就是这条路径上边权的最大值。所以 xx 走到 yy 的最小结束时间为路径上边权的最大值的最小。

因为要恰好走 ww 步,可以倍增 Floyd,也可以用矩阵快速幂实现。

代码
C++
#include <bits/stdc++.h>
using namespace std;
using LL = long long;
const int N = 205;
int n, m, k, q, w;
struct V {
  int a[N][N];
  auto &operator[](int x) { return a[x]; }
  V() { fill(a[0], a[0] + N * N, 1e9); }
  friend V operator*(V a, V b) {
    V c;
    for (int k = 1; k <= n; k++)
      for (int i = 1; i <= n; i++)
        for (int j = 1; j <= n; j++)
          c[i][j] = min(c[i][j], max(a[i][k], b[k][j]));
    return c;
  }
} a;
V P(V x, int y) {
  V s = x;
  for (; y; x = x * x, y >>= 1) y & 1 && (s = s * x, 1);
  return s;
}
int main() {
  cin.tie(0)->sync_with_stdio(0);
  cin >> n >> m;
  for (int u, v; m; m--) {
    cin >> u >> v, a[u][v] = 0;
  }
  cin >> k;
  for (int i = 1, u, v; i <= k; i++) {
    cin >> u >> v, a[u][v] = min(a[u][v], i);
  }
  cin >> q >> w, a = P(a, w - 1);
  for (int x, y; q; q--) {
    cin >> x >> y;
    cout << (a[x][y] != 1e9 ? a[x][y] : -1) << "\n";
  }
  return 0;
}

D 蜗蜗的藤曼通道

得分 40,用时 2h。

首先 kruskal 重构树建出最大生成树,再求 xx 到 yy 的路径最小值就做完了。

kruskal 重构树:首先将相同权值的点合并,再从大到小枚举 w=gcd⁡(ax,ay)w = \gcd(a_x, a_y),将权值是 ww 的倍数的点合并。

路径最小值:考虑在合并两个点 x,yx, y 时,新建一个虚拟点作为两点的父节点,其点权为 gcd⁡(ax,ay)\gcd(a_x, a_y)。那么 xx 到 yy 的路径最小值就是 lca(x,y)\mathrm{lca}(x, y) 的点权。

最近公共祖先:重链剖分,直接 dfs 可能会爆栈,要将所有递归写成非递归的形式。按照合并的性质,每个点最多两个儿子,所以可以不用建边,直接维护左儿子和右儿子。

快读也要加上。

代码
C++
#include <bits/stdc++.h>
using namespace std;
using LL = long long;
const int N = 4e6 + 5;
int n, m, q, r, a[N], f[N], fa[N], l[N], h[N], s[N], c[N], k[N], ls[N], rs[N];
int Rd() {
  int x = 0, f = 1;
  char c = getchar();
  for (; c < '0' || c > '9'; c == '-' && (f = -1), c = getchar());
  for (; c >= '0' && c <= '9'; x = (x << 1) + (x << 3) + (c ^ 48), c = getchar());
  return x * f;
}
int R(int x) {
  return f[x] == x ? x : f[x] = R(f[x]);
}
void U(int x, int y, int w) {
  if ((x = R(x)) == (y = R(y))) return;
  int z = ++m;
  f[x] = f[y] = fa[x] = fa[y] = z, a[z] = w;
  ls[z] = x, rs[z] = y;
}
int L(int x, int y) {
  for (; k[x] != k[y]; x = fa[k[x]]) h[k[x]] < h[k[y]] && (swap(x, y), 1);
  return h[x] < h[y] ? x : y;
}
int main() {
  cin.tie(0)->sync_with_stdio(0);
  n = Rd(), q = Rd(), m = n;
  iota(f + 1, f + 2 * n + 1, 1);
  for (int i = 1, x; i <= n; i++) {
    a[i] = x = Rd();
    r = max(r, x), l[x] ? U(l[x], i, x), 1 : l[x] = i;
  }
  for (int i = r; i; i--) {
    int p = 0;
    for (int j = i; j <= r; j += i)
      l[j] && (p ? U(l[j], p, i), 1 : p = l[j]);
  }
  for (int u = m; u; u--) {
    for (int v : {ls[u], rs[u]}) h[v] = h[u] + 1;
  }
  for (int u = 1; u <= m; u++) {
    s[u] = 1;
    for (int v : {ls[u], rs[u]}) {
      v && (s[u] += s[v], s[v] > s[c[u]] && (c[u] = v));
    }
  }
  k[R(1)] = R(1);
  for (int u = m; u; u--) {
    for (int v : {ls[u], rs[u]}) k[v] = (v == c[u] ? k[u] : v);
  }
  for (int x, y; q; q--) {
    x = Rd(), y = Rd(), cout << a[L(x, y)] << "\n";
  }
  return 0;
}

E 蜗蜗的闪光蜗牛培育园

得分 0,用时 1min。

捕获:因为每一次捕获的精灵都是相同的,所以可以将一次捕获视为一个块,用树状数组维护每一个块的大小。可以类似双端队列,每加入一个块都要将队首多余的块删掉。

击败:在树状数组上找到前缀和 ≤k\le k 的最大块的位置,直接按题意模拟即可。

代码
C++
#include <bits/stdc++.h>
using namespace std;
using LL = long long;
const int N = 4e5 + 5;
LL n, e, m, k, q, a[N], b[N], nd[N], lk[N];
LL l = 1, r, p[N], pr[N], pt[N], al;
struct T {
  LL t[N];
  void U(int x, LL v) {
    for (; x <= q; x += x & -x) t[x] += v;
  }
  LL Q(int x) {
    LL p = x - 1, s = 0;
    for (; x; x -= x & -x) s += t[x];
    for (; p; p -= p & -p) s -= t[p];
    return s;
  }
  int K(LL v) {
    LL x = 0, s = 0, d = 1;
    for (; d * 2 <= q; d *= 2);
    for (int y; d; d /= 2) {
      y = x + d;
      y <= q && s + t[y] < v && (x = y, s += t[y]);
    }
    return x + 1;
  }
} t;
int main() {
  cin.tie(0)->sync_with_stdio(0);
  cin >> n >> e >> m >> k >> q, m += e;
  for (int i = 1; i <= n; i++) cin >> a[i] >> b[i];
  for (int i = 1; i <= m; i++) cin >> nd[i];
  for (int i = 1; i <= m; i++) cin >> lk[i];
  for (LL i = 1, x, y; i <= q; i++) {
    char o, h; cin >> o;
    if (o == 'C') {
      cin >> h >> x >> y;
      x = (h == 'F' ? a[x] : e + b[x]), pr[x] += y;
      LL z = pr[x] / nd[x];
      if (!z) continue;
      pr[x] %= nd[x], p[++r] = x, t.U(r, z), al += z;
      for (; al > k;) {
        LL f = min(t.Q(l), al - k);
        for (t.U(l, -f), al -= f; l <= r && !t.Q(l); l++);
      }
    } else {
      cin >> x >> y;
      if (al < x) {
        cout << "MISS\n";
        continue;
      }
      LL bl = t.K(x), id = p[bl];
      for (t.U(bl, -1), al--, pt[id]++; l <= r && !t.Q(l); l++);
      auto P = [&](int x) {
        x <= e ? cout << "F " << x : cout << "E " << x - e;
      };
      if (y <= lk[id] || pt[id] == 80) {
        cout << "SHINY ", P(id), cout << "\n", pt[id] = 0;
      } else {
        cout << "NORMAL ", P(id), cout << " " << pt[id] << "\n";
      }
    }
  }
  return 0;
}

F 蜗蜗的魔法投掷

得分 8,用时 5min。

反思

  • 最后一次代码源模拟赛了,还反思啥呢。
原文