跳到正文

个人记录

POI2017 简要题解

jiangly约 16 分钟
原文
文章信息与版本

文章信息

发布
2020年3月24日
收录
2025年12月12日
更新
2026年11月21日
篇幅
约 16 分钟

版本

  1. v2当前
  2. v1

Stage I

Task 1 Flappy Bird

题意

有一个二维平面,初始时一只鸟在 (0,0)(0,0),目标是到达横坐标为 XX 的某个点。每一秒,假设鸟的当前位置是 (x,y)(x,y),如果点击屏幕,则它会移动到 (x+1,y+1)(x+1,y+1),否则移动到 (x+1,y−1)(x+1,y-1)。平面上有 nn (0≤n≤5⋅1050\le n\le 5\cdot10^5) 个障碍,第 ii 个障碍要求当横坐标为 xix_i 时,纵坐标 yy 满足 ai<y<bia_i< y< b_i,保证 xix_i 递增。求到达终点需要的最小点击次数 (或者报告无解)。

题解

显然对于每个横坐标,可行的纵坐标是一段区间内所有的奇数或偶数。而要求点击次数最小,只需要最后的纵坐标最小即可。

时间复杂度:O(n)O(n)。

代码

C++
#include <iostream>
#include <vector>
int main() {
    std::ios::sync_with_stdio(false);
    std::cin.tie(nullptr);
    int x = 0, y1 = 0, y2 = 0;
    int n, l;
    std::cin >> n >> l;
    for (int i = 0; i < n; ++i) {
        int a, b, c;
        std::cin >> a >> b >> c;
        y1 -= a - x;
        y2 += a - x;
        x = a;
        y1 = std::max(y1, b + 1);
        y2 = std::min(y2, c - 1);
        if ((y1 ^ x) & 1)
            ++y1;
        if ((y2 ^ x) & 1)
            --y2;
        if (y1 > y2) {
            std::cout << "NIE" << "\n";
            return 0;
        }
    }
    y1 -= l - x;
    if ((y1 ^ l) & 1)
        ++y1;
    std::cout << (l + y1) / 2 << "\n";
    return 0;
}

Task 2 Podzielność

题意

有一些 BB (2≤B≤1062\le B\le 10^6) 进制数字,其中数字 ii 有 aia_i (ai≥1a_i\ge 1) 个。现在要用其中的一些组成一个最大的 BB 进制数 XX 满足 XX 是 B−1B-1 的倍数。之后有 qq (1≤q≤1051\le q\le 10^5) 次询问,第 ii 次询问 XX 从低位数起的第 kik_i 个数字是什么 (如果不存在第 kik_i 位则输出 −1-1)。

题解

显然 XX 是 B−1B-1 的倍数当且仅当 XX 所有位的和是 B−1B-1 的倍数。当选择所有数不满足条件时,由于 ai≥1a_i\ge 1,只需要删除对应的一个数字即可。之后将所有数字从大到小排列就是最大的 XX。每次询问二分即可。

时间复杂度:O(B+qlog⁡B)O(B+q\log B)。

代码

C++
#include <iostream>
#include <vector>
#include <algorithm>
int main() {
    std::ios::sync_with_stdio(false);
    std::cin.tie(nullptr);
    int n, q;
    std::cin >> n >> q;
    std::vector<long long> a(n);
    long long sum = 0;
    for (int i = 0; i < n; ++i) {
        std::cin >> a[i];
        sum += i * a[i];
    }
    if (sum % (n - 1) != 0)
        --a[sum % (n - 1)];
    for (int i = 1; i < n; ++i)
        a[i] += a[i - 1];
    for (int i = 0; i < q; ++i) {
        long long k;
        std::cin >> k;
        if (k >= a[n - 1]) {
            std::cout << -1 << "\n";
        } else {
            std::cout << std::upper_bound(a.begin(), a.end(), k) - a.begin() << "\n";
        }
    }
    return 0;
}

Task 3 Reprezentacje różnicowe

题意

给定数列 [a1,a2,…][a_1,a_2,\ldots],其递归定义为:a1=1a_1=1, a2=2a_2=2, a2n+1=2a2na_{2n+1}=2a_{2n}, a2n+2=a2n+1+rna_{2n+2}=a_{2n+1}+r_n,其中 n≥1n\ge 1,rnr_n 是最小的不能表示为 a1,a2,…,a2n+1a_1,a_2,\ldots,a_{2n+1} 中两项的差的正整数。可以证明,任意一个正整数都能唯一的表示为数列 aa 中两项的差 ap−aqa_p-a_q。给定 nn (1≤n≤1051\le n\le 10^5) 个正整数 xx (1≤x≤1091\le x\le 10^9),求出这个表示。

题解

由于 a56=1062283805a_{56}=1062283805, a57=2a56a_{57}=2a_{56},在这之后所有不超过 10910^9 的差都是 a2n+2−a2n+1a_{2n+2}-a_{2n+1} 的形式。那么求出所有 1≤q<p≤561\le q<p\le 56 的答案,所有不在这些当中的数会以 a58−a57,a60−a59,…a_{58}-a_{57},a_{60}-a_{59},\ldots 的形式依次出现。因此每次询问二分即可。

代码

C++
#include <iostream>
#include <vector>
#include <algorithm>
#include <tuple>
constexpr int A[] = {0, 1, 2, 4, 8, 16, 21, 42, 51, 102, 112, 224, 235, 470, 486, 972, 990, 1980, 2002, 4004, 4027, 8054, 8078, 16156, 16181, 32362, 32389, 64778, 64806, 129612, 129641, 259282, 259313, 518626, 518658, 1037316, 1037349, 2074698, 2074734, 4149468, 4149505, 8299010, 8299049, 16598098, 16598140, 33196280, 33196324, 66392648, 66392693, 132785386, 132785432, 265570864, 265570912, 531141824, 531141876, 1062283752, 1062283805};
std::vector<std::tuple<int, int, int>> ans;
int main() {
    std::ios::sync_with_stdio(false);
    std::cin.tie(nullptr);
    for (int i = 1; i <= 56; ++i)
        for (int j = 1; j < i; ++j)
            if (A[i] - A[j] <= 1e9)
                ans.emplace_back(A[i] - A[j], i, j);
    std::sort(ans.begin(), ans.end());
    int t;
    std::cin >> t;
    while (t--) {
        int x;
        std::cin >> x;
        int k = std::lower_bound(ans.begin(), ans.end(), std::make_tuple(x + 1, 0, 0)) - ans.begin() - 1;
        auto [y, p, q] = ans[k];
        if (x != y) {
            p = 2 * (x - k + 27);
            q = p - 1;
        }
        std::cout << p << " " << q << "\n";
    }
    return 0;
}

Task 4 Sabotaż

题意

一个公司有 nn (1≤n≤5⋅1051\le n\le 5\cdot10^5) 个人,构成了树形的上下级关系,其中出现了一个叛徒。如果一个人的下属中叛徒 的比例达到了 xx,则这个人以及所有下属都会变为叛徒。求最小的 xx,满足最坏情况下叛徒数量不超过 kk。

题解

显然最坏情况初始叛徒是叶子,因此最后所有的叛徒是一棵子树。简单 dp 出使得每棵子树全部成为叛徒的最大的 xx 即可。

时间复杂度:O(n)O(n)。

代码

C++
#include <iostream>
#include <vector>
#include <iomanip>
int n, k;
std::vector<double> dp;
std::vector<int> sz;
std::vector<std::vector<int>> e;
void dfs(int u) {
    if (e[u].empty()) {
        dp[u] = 1;
        sz[u] = 1;
        return;
    }
    for (int v : e[u]) {
        dfs(v);
        sz[u] += sz[v];
    }
    for (int v : e[u])
        dp[u] = std::max(dp[u], std::min(dp[v], 1.0 * sz[v] / sz[u]));
    ++sz[u];
}
int main() {
    std::ios::sync_with_stdio(false);
    std::cin.tie(nullptr);
    std::cin >> n >> k;
    e.resize(n);
    sz.resize(n);
    dp.resize(n);
    for (int i = 1; i < n; ++i) {
        int p;
        std::cin >> p;
        --p;
        e[p].push_back(i);
    }
    dfs(0);
    double ans = 0;
    for (int i = 0; i < n; ++i)
        if (sz[i] > k)
            ans = std::max(ans, dp[i]);
    std::cout << std::fixed << std::setprecision(10) << ans << "\n";
    return 0;
}

Task 5 Turysta

题意

给定一张 nn (2≤n≤20002\le n\le 2000) 个点的竞赛图,找到从每个点开始的经过点数最多的简单路径。

题解

众所周知 ≥3\ge 3 个点的强连通竞赛图有哈密顿回路,因此从 uu 开始最长的路径就是拓扑序在 uu 所属的强连通分量之后 (包括其本身) 的强连通分量的哈密顿路径相连。在每个强连通分量找到哈密顿回路即可。找哈密顿回路时,首先找到一条哈密顿路径 (按任意顺序加点即可),然后找到路径上第一个向第一个点有边的点,作为初始的环。之后每次找到第一个向环中点有边的点,将它及之前未加入的点加入即可。

时间复杂度:O(n2)O(n^2)。

代码

C++
#include <iostream>
#include <vector>
#include <algorithm>
int n;
std::vector<std::vector<bool>> g;
int cntScc, dfsClock;
std::vector<int> dfn, low, belong;
std::vector<int> stk;
std::vector<std::vector<int>> ans;
std::vector<std::vector<int>> h;
void tarjan(int u) {
    dfn[u] = low[u] = dfsClock++;
    stk.push_back(u);
    for (int v = 0; v < n; ++v) {
        if (g[u][v]) {
            if (dfn[v] == -1) {
                tarjan(v);
                low[u] = std::min(low[u], low[v]);
            } else if (belong[v] == -1) {
                low[u] = std::min(low[u], dfn[v]);
            }
        }
    }
    if (dfn[u] == low[u]) {
        int v;
        do {
            v = stk.back();
            belong[v] = cntScc;
            stk.pop_back();
        } while (v != u);
        ++cntScc;
    }
}
auto solve(std::vector<int> v) {
    if (int(v.size()) == 1)
        return v;
    std::vector<int> h;
    for (auto u : v)
        h.insert(std::find_if(h.begin(), h.end(), [&](int v) {return g[u][v];}), u);
    auto it = std::find_if(h.begin(), h.end(), [&](int v) {return g[v][h[0]];}) + 1;
    std::vector<int> c(h.begin(), it);
    while (it != h.end()) {
        auto r = it;
        while (std::find_if(c.begin(), c.end(), [&](int v) {return g[*r][v];}) == c.end())
            ++r;
        auto p = c.begin();
        while (p + 1 != c.end() && !(g[*p][*it] && g[*r][*(p + 1)]))
            ++p;
        c.insert(p == c.end() ? c.begin() : p + 1, it, r + 1);
        it = r + 1;
    }
    return c;
}
int main() {
    std::ios::sync_with_stdio(false);
    std::cin.tie(nullptr);
    std::cin >> n;
    g.assign(n, std::vector<bool>(n));
    dfn.assign(n, -1);
    low.resize(n);
    belong.assign(n, -1);
    for (int i = 0; i < n; ++i) {
        for (int j = 0; j < i; ++j) {
            int x;
            std::cin >> x;
            g[i][j] = x ^ 1;
            g[j][i] = x;
        }
    }
    for (int i = 0; i < n; ++i)
        if (dfn[i] == -1)
            tarjan(i);
    h.resize(cntScc);
    ans.resize(n);
    for (int i = 0; i < n; ++i)
        h[belong[i]].push_back(i);
    for (int i = 0; i < cntScc; ++i) {
        h[i] = solve(h[i]);
        for (auto it = h[i].begin(); it != h[i].end(); ++it) {
            int u = *it;
            ans[u].insert(ans[u].end(), it, h[i].end());
            ans[u].insert(ans[u].end(), h[i].begin(), it);
            for (int j = i - 1; j >= 0; --j)
                ans[u].insert(ans[u].end(), h[j].begin(), h[j].end());
        }
    }
    for (int i = 0; i < n; ++i) {
        std::cout << ans[i].size();
        for (int u : ans[i])
            std::cout << " " << u + 1;
        std::cout << "\n";
    }
    return 0;
}
原文
存档评论1 条抓取于