1 条题解

  • 0
    @ 2026-4-27 1:06:54
    #include <bits/stdc++.h>
    
    /*
     *  at most one portal should be used -> also the nearest
     */
    
    int main() {
        
        int n, m, q;
        scanf("%d%d%d", &n, &m, &q);
    
        std::vector<int> portals(m + m);
        for (int i = 0; i < m; ++i) {
            int x;
            scanf("%d", &x);
            portals[i] = x;
            portals[i + m] = x + n;
        }
        
        auto dist = [&](int x, int y) -> int {
            if (x < y) std::swap(x, y);
            return std::min(x - y, n + n + y - x);
        };
        
        auto dist_by_portal = [&](int x, int y, int z) -> int {
            return dist(x, z) + 1 + dist(z <= n ? z + n : z - n, y);
        };
        
        while (q--) {
    
            int x, y;
            scanf("%d%d", &x, &y);
            
            int ans = dist(x, y);
            int i = std::lower_bound(portals.begin(), portals.end(), x) - portals.begin();
            
            ans = std::min(ans, dist_by_portal(x, y, i == portals.size() ? portals.front() : portals[i]));
            ans = std::min(ans, dist_by_portal(x, y, i == 0 ? portals.back() : portals[i - 1]));
            printf("%d\n", ans);
            
        }
    
    }
    
    • 1

    信息

    ID
    10944
    时间
    2000ms
    内存
    2024MiB
    难度
    10
    标签
    递交数
    3
    已通过
    1
    上传者