Codeforces 1129 / 1130 (D-E) 解题报告

目录

  • 综述
  • A2 Toy Train
  • B Wrong Answer
  • C Morse Code
  • D Isolation

综述

Codeforces 1129 (Div. 1) / 1130 (Div. 2) 是由 Master top34051Master zoomswkH.Q. MikeMirzayanov 举办的比赛。题目为:

1 2 3 4 5 6 7 8 9
2 A B C D1 D2 E
1 A1 A2 B C D E

A2 Toy Train

题目描述

火车沿环线运行,沿线有 \(n\) 个站,顺时针编号,火车也按顺时针运行。一共要运输 \(m\) 件货物,第 \(i\) 件货物需要从第 \(x_i\) 站出发,到达第 \(y_i\) 站。火车可以在每站卸下任意数量的货物,但装货每次停车只能装一件。火车的容量无限。问:从第 \(i\) 站出发的火车需要经过(停靠或通过)多少个站台才能运输所有货物。\(n\le5000\)\(m\le20000\)

解决思路

为了方便,假设每站之间的距离为 \(1\)。可以发现答案等于火车行驶的里程。假设运输完毕第 \(i\) 个站所有货物所需的时间为 \(\operatorname{sum}_i\),设初始站台为 \(a\),不难发现答案等于 \(\max{(\operatorname{sum}_i+\operatorname{dis}(a,i))}\)。计算 \(\operatorname{sum}_i\) 时,若第 \(i\) 站共 \(p_i\) 件货物要运输,货物中运输距离最短的那件距离为 \(d_i\),因为一次只能装一件货,所以需要绕 \(p_i-1\) 圈,再多走 \(d_i\) 的路程。即 \(\operatorname{sum}_i=n\times(p_i-1)+d_i\)

\(p_i\)\(d_i\) 都很容易计算。因此可以用 \(O(n^2)\) 的时间复杂度解决这道题。

代码

#include <bits/stdc++.h>
using namespace std;
const int N = 5005;
int d[N], p[N], n, m;
int dis(int x, int y) {return ((y - x) % n + n) % n;}
int sum(int x) {return (p[x] - 1) * n + d[x];}
int main() {
    memset(d, 0x3f, sizeof(d));
    memset(p, 0, sizeof(p));
    ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    cin >> n >> m;
    for (int i = 1; i <= m; i++) {
        int x, y;
        cin >> x >> y;
        p[x]++;
        d[x] = min(d[x], dis(x, y));
    }
    for (int i = 1; i <= n; i++) {
        int ans = 0;
        for (int j = 1; j <= n; j++) {
            if (p[j] == 0) continue;
            ans = max(ans, sum(j) + dis(i, j));
        }
        cout << ans << ' ';
    }
    cout << '\n';
    return 0;
}

B Wrong Answer

题目描述

posted @ 2025-08-29 10:54  cwkapn  阅读(16)  评论(0)    收藏  举报