1. 首页 >  信息技术服务 >  AtCoder Beginner Contest 323

AtCoder Beginner Contest 323

有的人边上课边打abc

A - Weak Beats (abc323 A)

题目大意

给定一个\(01\)字符串,问偶数位(从\(1\)开始) 是否全为\(0\)。

解题思路

遍历判断即可。

神奇的代码

#include <bits/stdc++.h>
using namespace std;
using LL = long long;

int main(void) {
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    string s;
    cin >> s;
    bool ok = true;
    for (int i = 1; i < s.size(); i += 2) {
        ok &= (s[i] == '0');
    }
    if (ok)
        cout << "Yes" << '\n';
    else
        cout << "No" << '\n';

    return 0;
}

B - Round-Robin Tournament (abc323 B)

题目大意

给定\(n\)个人与其他所有人的胜负情况。问最后谁赢。

一个人获胜次数最多则赢,若次数相同,则编号小的赢。

解题思路

统计每个人的获胜次数,排个序即可。

神奇的代码

#include <bits/stdc++.h>
using namespace std;
using LL = long long;

int main(void) {
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int n;
    cin >> n;
    vector<int> a(n);
    for (auto& i : a) {
        string s;
        cin >> s;
        i = count(s.begin(), s.end(), 'o');
    }
    vector<int> id(n);
    iota(id.begin(), id.end(), 0);
    sort(id.begin(), id.end(), [&](int x, int y) {
        if (a[x] != a[y])
            return a[x] > a[y];
        else
            return x < y;
    });
    for (auto& i : id)
        cout << i + 1 << ' ';

    return 0;
}

C - World Tour Finals (abc323 C)

题目大意

给定每道题的分数,以及每个人的通过情况,每个人的得分为解决题目的分的和,其中第\(i\)个人还有\(i\)的额外加分。

对于每个人,问最少还得解决多少道题,才能成为第一。

解题思路

因为范围只有\(100\),可以采取最朴素的做法,先统计每个人的分数,得到当前最高的分数。

然后对于每个人,从未通过题目的分数,排个序,大到小依次解决获得分数,看何时超过最高分。

由于每个人有不同的额外得分,所以不会有同分。

时间复杂度为 \(O(n^2\log n)\)

也可以每次遍历未通过的题目,找分数最大的,这样复杂度为\(O(n^3)\)同样可以通过。

神奇的代码

#include <bits/stdc++.h>
using namespace std;
using LL = long long;

int main(void) {
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int n, m;
    cin >> n >> m;
    vector<int> a(m);
    for (auto& i : a)
        cin >> i;
    vector<int> score(n);
    vector<string> s(n);
    for (int i = 0; i < n; ++i) {
        cin >> s[i];
        score[i] = i + 1;
        for (int j = 0; j < m; ++j)
            score[i] += a[j] * (s[i][j] == 'o');
    }
    int maxx = *max_element(score.begin(), score.end());
    for (int i = 0; i < n; ++i) {
        if (score[i] == maxx) {
            cout << 0 << '\n';
            continue;
        }
        int dis = maxx - score[i];
        vector<int> unsolve;
        for (int j = 0; j < m; ++j)
            if (s[i][j] == 'x')
                unsolve.push_back(a[j]);
        sort(unsolve.begin(), unsolve.end(), greater<int>());
        int ans = 0;
        for (; dis > 0 && ans < unsolve.size(); ++ans) {
            dis -= unsolve[ans];
        }
        cout << ans << '\n';
    }

    return 0;
}

D - Merge Slimes (abc323 D)

题目大意

有\(n\)类数字,第 \(i\)类数字为 \(s_i\),有 \(c_i\)个。

同类数字可以两两合并,得到一个\(s_i + s_i\)数字。

问最后剩下的数字数量。

解题思路

因为数字很大,可以考虑用map存对应数的数量。

但是不能遍历\(map\)的每个元素,然后合并,累计到更大的数里。因为如果更大的数不存在,会插入到 \(map\)中,从而可能导致原因迭代器失效。

但考虑到一类元素不断合并,最终停止时,其个数一定是\(1\),就不用管它了,我们只考虑一开始的那些类别。 因此事先用set储存原来的数的类别。

然后对于set的每一个类别,从小到大考虑不断合并,直到数量变为\(1\)。由于每次合并数量都除以 \(2\)。因此每一类最多 \(\log\)次就结束了。

最后看 map里剩余数的个数。

神奇的代码

#include <bits/stdc++.h>
using namespace std;
using LL = long long;

int main(void) {
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int n;
    cin >> n;
    unordered_map<LL, int> cnt;
    set<int> a;
    for (int i = 0; i < n; ++i) {
        int s, c;
        cin >> s >> c;
        cnt[s] = c;
        a.insert(s);
    }
    int ans = 0;
    for (auto& i : a) {
        int sum = 0;
        for (LL j = i; true; j = j + j) {
            sum = (sum + cnt[j]);
            if (sum < 2) {
                cnt[j] = sum;
                break;
            }
            cnt[j] = sum % 2;
            sum /= 2;
        }
    }
    for (auto& i : cnt) {
        ans += i.second;
    }
    cout << ans << '\n';

    return 0;
}

E - Playlist (abc323 E)

题目大意

给定\(n\)首歌的播放时间。

从第\(0\)秒,随机选一首歌播放。当一首歌播放完后,立刻随机选下一首歌播放。

问第 \(X + 0.5\)秒 时播放第一首歌的概率。

解题思路

假设第一首歌的播放时长为\(t\)。

那么要在 \(X + 0.5\)秒播放第一首歌,则要求第一首歌在第 \(X, X - 1, X - 2, …, X - t + 1\)秒中的一秒开始播放。

而如果我知道能够在 第 \(X,X-1,X-2,…,X-t + 1\)秒开始播放的概率(或者说这些时刻恰好播放完的概率),那么这些概率的和,再乘以\(\frac{1}{n}\)(即选到第一首歌播放的概率),就是答案。

设 \(dp[i]\)表示能够在第 \(i\)秒开始播放的概率,转移则枚举之前播放的是哪一首歌,比如第 \(j\)首,播放时长为\(t_j\),则有 \(dp[i] += dp[i - t_j] \times \frac{1}{n}\)。

即 \(dp[i] = \sum_{t_j \leq i} dp[i - t_j] \times \frac{1}{n}\)

初始条件就是\(dp[0] = 1\)。即第 \(0\)秒肯定能够播放一首歌。

最后答案就是\(\sum_{i = x - t + 1}^{x} dp[i] \times \frac{1}{n}\)

时间复杂度为 \(O(n^2)\)

神奇的代码

#include <bits/stdc++.h>
using namespace std;
using LL = long long;

const int mo = 998244353;

int qpower(int a, int b) {
    int ret = 1;
    while (b) {
        if (b & 1) {
            ret = 1ll * ret * a % mo;
        }
        a = 1ll * a * a % mo;
        b >>= 1;
    }
    return ret;
}

int inv(int x) { return qpower(x, mo - 2); }

int main(void) {
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int n, x;
    cin >> n >> x;
    vector<int> t(n);
    for (auto& i : t) {
        cin >> i;
    }
    vector<int> dp(x + 1);
    int one = inv(n);
    dp[0] = 1;
    for (int i = 1; i <= x; ++i) {
        int sum = 0;
        for (auto& j : t) {
            if (i < j)
                continue;
            sum += 1ll * dp[i - j] * one % mo;
            if (sum >= mo)
                sum -= mo;
        }
        dp[i] = sum;
    }
    int ans = 0;
    for (int i = max(0, x - t[0] + 1); i <= x; ++i) {
        ans = ans + dp[i];
        if (ans >= mo)
            ans -= mo;
    }
    ans = 1ll * ans * one % mo;
    cout << ans << '\n';

    return 0;
}

F - Push and Carry (abc323 F)

题目大意

二维平面,推箱子。给定你的位置,箱子位置,目标位置,一次上下左右移动一格,问把箱子推到目标位置的最小步数。

解题思路

虽然坐标范围挺大的,但实际上决策只有两种。枚举两种决策分别计算下步数取最小即可。

假设箱子在左下,目标位置在右上。很显然,推箱子只有两个决策:

  • 先向上推,再向右推。
  • 先向右推,再向上推。

而为了把箱子向上推,那么事先要走到箱子的下方。同理向右推,要事先走到箱子的左方。

最终步数的贡献来自于:

  • 起始位置走到推箱子的位置的步数
  • 推动箱子的步数(固定的,为箱子位置目标位置 的曼哈顿距离)
  • 中间转动方向时额外需要的步数(固定的,为\(2\)),比如向上推时,你在箱子下方,然后要向右推,你需要走到箱子的左方,步数为\(2\)。

因此只有第一个位置需要枚举,事实上就两种情况:

  • 先向上推,则求出初始位置到箱子下方的距离
  • 先向右推,则求出初始位置到箱子左方的距离

而这个距离事实上也是个曼哈顿距离,因为除了箱子别无障碍,两种方式到达指定位置中,必有一种是畅通无阻的。

当注意一种情况,当初始位置、箱子位置、到达箱子的位置处于同一条线,且箱子位置在中间的时候,此时并不是畅通无阻的,必须要偏移一下。比如初始位置在左边,箱子在中间,为了把箱子往左推,你要跑到箱子右边,但不能穿过箱子,此时的步数在曼哈顿距离的基础上还要\(+2\),即先向上走或向下走,再走回来。

代码里两种情况的分别考虑就相当于交换了下两个坐标,再考虑一次。

神奇的代码

#include <bits/stdc++.h>
using namespace std;
using LL = long long;

int sign(LL x) {
    if (x == 0)
        return 0;
    else if (x > 0)
        return 1;
    else
        return -1;
}

int main(void) {
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    array<array<LL, 2>, 3> pos;
    for (auto& i : pos)
        for (auto& j : i)
            cin >> j;
    auto& [player, box, target] = pos;
    auto dis = [&](const array<LL, 2>& a, const array<LL, 2>& b) {
        return abs(a[0] - b[0]) + abs(a[1] - b[1]);
    };
    LL bound = dis(box, target);
    auto in_middle = [](LL l, LL m, LL r) {
        LL L = min(l, r), R = max(l, r);
        return m > L && m < R;
    };
    auto solve = [&]() {
        LL ret = bound;
        int d = sign(box[0] - target[0]);
        if (d == 0)
            return numeric_limits<LL>::max();
        array<LL, 2> t1 = box;
        t1[0] += d;
        ret += dis(player, t1);
        if ((player[0] == t1[0] && player[0] == box[0] &&
             in_middle(player[1], box[1], t1[1])) ||
            (player[1] == t1[1] && player[1] == box[1] &&
             in_middle(player[0], box[0], t1[0])))
            ret += 2;
        if (box[1] != target[1])
            ret += 2;
        return ret;
    };
    LL ans = solve();
    for (auto& i : pos)
        swap(i[0], i[1]);
    ans = min(ans, solve());
    cout << ans << '\n';

    return 0;
}

G - Inversion of Tree (abc323 G)

题目大意

给定一个关于\(n\)的全排列\(p\)。对于每个\(k = 0,1,2,…,n - 1\),问有多少种形态的树,满足树上恰好有\(k\)条边\((u,v)(u < v)\),有 \(p_u > p_v\)。

解题思路

是一道关于矩阵树黑科技的计数问题。

考虑朴素做法,假设给定的排列\(p\)有 \(k\)个逆序对 \((i, j), p_i > p_j\)。

先花 \(O(2^k)\)枚举要保留哪些边,那问题就剩下:

给定一张\(n\)个点的完全图,我要求删除一些边,得到一棵树,其中特定边要保留下来,特定边要去掉,其余边无所谓。问这种树的方案数。

这种图的生成树计数问题,矩阵树科技可以解决。

矩阵树定理指一个关于图的矩阵\(L\)(拉普拉斯矩阵),它的行列式的任意余子式(去掉任一行、任一列)都是一样的,且等于该图的生成树个数。这个图可以有向、无向,可以有重边,但不能有自环。

其中拉普拉斯矩阵\(L\)=度数矩阵\(D-\)邻接矩阵\(A\)。

度数矩阵\(D_{ii}=\)与点 \(i\)相连的边的个数,边有边权时则为边权和。其余为 \(0\)。

邻接矩阵\(A_{ij}=\)边\((i,j)\)的个数,边有边权时则为边权和。

其矩阵的任意余子式的结果即为\(\sum{T} \prod{ei \in T} w{e_i}\),\(w_i\)为第\(i\)条边的边权,\(T\)是所有生成树的集合,每个元素是一些列边的集合。 当边权为\(1\)时就是生成树的数量。

现在我们要求保留了一些边的生成树的方案数,怎么做呢?

考虑生成函数的思想,我们把要保留的边的边权设为 \(x\)(就生成函数里无意义的自变量),其余边权为 \(1\)。于是其矩阵\(L\)的余子式是一个关于 \(x\)的多项式,最高次的系数就是保留了特定边的方案数。

因此我们把上述 \(k\)个逆序对对应的边的边权全设为 \(x\),其余为 \(1\),其矩阵 \(L\)的余子式就是一个关于\(x\)的多项式,其中\(x^i\)的系数就是恰好保留了 i$个特殊边(逆序对)的方案数。

问题剩下如何快速地求出这个余子式套个板子吧

神奇的代码

#include <bits/stdc++.h>
using namespace std;
using LL = long long;

template <unsigned M_> struct ModInt {
    static constexpr unsigned M = M_;
    unsigned x;
    constexpr ModInt() : x(0U) {}
    constexpr ModInt(unsigned x_) : x(x_ % M) {}
    constexpr ModInt(unsigned long long x_) : x(x_ % M) {}
    constexpr ModInt(int x_)
        : x(((x_ %= static_cast<int>(M)) < 0) ? (x_ + static_cast<int>(M))
                                              : x_) {}
    constexpr ModInt(long long x_)
        : x(((x_ %= static_cast<long long>(M)) < 0)
                ? (x_ + static_cast<long long>(M))
                : x_) {}
    ModInt& operator+=(const ModInt& a) {
        x = ((x += a.x) >= M) ? (x - M) : x;
        return *this;
    }
    ModInt& operator-=(const ModInt& a) {
        x = ((x -= a.x) >= M) ? (x + M) : x;
        return *this;
    }
    ModInt& operator*=(const ModInt& a) {
        x = (static_cast<unsigned long long>(x) * a.x) % M;
        return *this;
    }
    ModInt& operator/=(const ModInt& a) { return (*this *= a.inv()); }
    ModInt pow(long long e) const {
        if (e < 0)
            return inv().pow(-e);
        ModInt a = *this, b = 1U;
        for (; e; e >>= 1) {
            if (e & 1)
                b *= a;
            a *= a;
        }
        return b;
    }
    ModInt inv() const {
        unsigned a = M, b = x;
        int y = 0, z = 1;
        for (; b;) {
            const unsigned q = a / b;
            const unsigned c = a - q * b;
            a = b;
            b = c;
            const int w = y - static_cast<int>(q) * z;
            y = z;
            z = w;
        }
        assert(a == 1U);
        return ModInt(y);
    }
    ModInt operator+() const { return *this; }
    ModInt operator-() const {
        ModInt a;
        a.x = x ? (M - x) : 0U;
        return a;
    }
    ModInt operator+(const ModInt& a) const { return (ModInt(*this) += a); }
    ModInt operator-(const ModInt& a) const { return (ModInt(*this) -= a); }
    ModInt operator*(const ModInt& a) const { return (ModInt(*this) *= a); }
    ModInt operator/(const ModInt& a) const { return (ModInt(*this) /= a); }
    template <class T> friend ModInt operator+(T a, const ModInt& b) {
        return (ModInt(a) += b);
    }
    template <class T> friend ModInt operator-(T a, const ModInt& b) {
        return (ModInt(a) -= b);
    }
    template <class T> friend ModInt operator*(T a, const ModInt& b) {
        return (ModInt(a) *= b);
    }
    template <class T> friend ModInt operator/(T a, const ModInt& b) {
        return (ModInt(a) /= b);
    }
    explicit operator bool() const { return x; }
    bool operator==(const ModInt& a) const { return (x == a.x); }
    bool operator!=(const ModInt& a) const { return (x != a.x); }
    friend std::ostream& operator<<(std::ostream& os, const ModInt& a) {
        return os << a.x;
    }
};

// det(a + x I)
// O(n^3)
//   Call by value: Modifies a (Watch out when using C-style array!)
template <class T> vector<T> charPoly(vector<vector<T>> a) {
    const int n = a.size();
    // upper Hessenberg
    for (int j = 0; j < n - 2; ++j) {
        for (int i = j + 1; i < n; ++i) {
            if (a[i][j]) {
                swap(a[j + 1], a[i]);
                for (int ii = 0; ii < n; ++ii)
                    swap(a[ii][j + 1], a[ii][i]);
                break;
            }
        }
        if (a[j + 1][j]) {
            const T s = 1 / a[j + 1][j];
            for (int i = j + 2; i < n; ++i) {
                const T t = s * a[i][j];
                for (int jj = j; jj < n; ++jj)
                    a[i][jj] -= t * a[j + 1][jj];
                for (int ii = 0; ii < n; ++ii)
                    a[ii][j + 1] += t * a[ii][i];
            }
        }
    }
    // fss[i] := det(a[0..i][0..i] + x I_i)
    vector<vector<T>> fss(n + 1);
    fss[0] = {1};
    for (int i = 0; i < n; ++i) {
        fss[i + 1].assign(i + 2, 0);
        for (int k = 0; k <= i; ++k)
            fss[i + 1][k + 1] = fss[i][k];
        for (int k = 0; k <= i; ++k)
            fss[i + 1][k] += a[i][i] * fss[i][k];
        T prod = 1;
        for (int j = i - 1; j >= 0; --j) {
            prod *= -a[j + 1][j];
            const T t = prod * a[j][i];
            for (int k = 0; k <= j; ++k)
                fss[i + 1][k] += t * fss[j][k];
        }
    }
    return fss[n];
}

template <class T> vector<T> detPoly(vector<vector<T>> a, vector<vector<T>> b) {
    const int n = a.size();
    T prod = 1;
    int off = 0;
    for (int h = 0; h < n; ++h) {
        for (;;) {
            if (b[h][h])
                break;
            for (int j = h + 1; j < n; ++j) {
                if (b[h][j]) {
                    prod *= -1;
                    for (int i = 0; i < n; ++i) {
                        swap(a[i][h], a[i][j]);
                        swap(b[i][h], b[i][j]);
                    }
                    break;
                }
            }
            if (b[h][h])
                break;
            if (++off > n)
                return vector<T>(n + 1, 0);
            for (int j = 0; j < n; ++j) {
                b[h][j] = a[h][j];
                a[h][j] = 0;
            }
            for (int i = 0; i < h; ++i) {
                const T t = b[h][i];
                for (int j = 0; j < n; ++j) {
                    a[h][j] -= t * a[i][j];
                    b[h][j] -= t * b[i][j];
                }
            }
        }
        prod *= b[h][h];
        const T s = 1 / b[h][h];
        for (int j = 0; j < n; ++j) {
            a[h][j] *= s;
            b[h][j] *= s;
        }
        for (int i = 0; i < n; ++i)
            if (h != i) {
                const T t = b[i][h];
                for (int j = 0; j < n; ++j) {
                    a[i][j] -= t * a[h][j];
                    b[i][j] -= t * b[h][j];
                }
            }
    }
    const vector<T> fs = charPoly(a);
    vector<T> gs(n + 1, 0);
    for (int i = 0; off + i <= n; ++i)
        gs[i] = prod * fs[off + i];
    return gs;
}

using MInt = ModInt<998244353>;

int main(void) {
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int n;
    cin >> n;
    vector<int> p(n);
    for (auto& i : p)
        cin >> i;
    vector<vector<MInt>> a(n, vector<MInt>(n, 0));
    auto b = a;
    for (int i = 0; i < n; ++i)
        for (int j = i + 1; j < n; ++j) {
            if (p[i] > p[j]) {
                b[i][i] += 1;
                b[j][j] += 1;
                b[i][j] -= 1;
                b[j][i] -= 1;
            } else {
                a[i][i] += 1;
                a[j][j] += 1;
                a[i][j] -= 1;
                a[j][i] -= 1;
            }
        }
    a.resize(n - 1);
    b.resize(n - 1);
    for (auto& i : a)
        i.resize(n - 1);
    for (auto& i : b)
        i.resize(n - 1);
    auto ans = detPoly(a, b);
    for (auto& i : ans)
        cout << i << ' ';

    return 0;
}

/xin-xi-ji-zhu-fu-wu/atcoder-beginner-contest-323-6900.html