跳到正文

题解

题解:P15246 [WC2026] 猫和老鼠

_Z_F_R_约 11 分钟
原文
文章信息与版本

文章信息

发布
2026年2月20日
收录
2026年2月20日
更新
2026年11月13日
篇幅
约 11 分钟
题目
P15246

版本

  1. v1当前

我们把原数轴当成横轴,时间当成纵轴,并且把猫视为线段,并且画两条直线 x=0x = 0 和 x=mx = m 来限定 Jerry 活动的范围,那么 Jerry 会从底下任意一点出发向上移动,且移动轨迹的“瞬时斜率”的绝对值不小于 11。

为了方便,我们把点绕原点旋转 45∘45^{\circ},可以通过 (x,y)→(x+y,y−x)(x, y) \to (x + y, y - x) 做到这一点。那么 Jerry 就相当于只能往右上走,且线段均是水平或竖直的。

考虑一个猫的集合,如果 Jerry 无论如何都会碰到这些猫中的一个,那么我们称这些猫构成一层“边界”。

刻画“边界”,我们从 x=0x = 0 旋转后的 y=xy = x 出发,尝试沿线段走到 x=mx = m 旋转后的 y=x−2my = x - 2m。这两条线走到线段均应当满足:线段与这两条线直接相交。

如何实现线段之间的衔接呢?讨论一下可以发现,我们如果刚刚沿 (xs,ys)→(xt,yt)(x_s, y_s) \to (x_t, y_t) 走完了上一条线段,要走到 (xs′,ys′)(x_{s'}, y_{s'}) 的话必须要满足:xt≥xs′x_t \ge x_{s'} 且 yt≤ys′y_t \le y_{s'}。

又不难发现:竖直的线段向上走、水平的线段向左走均是不优的。因为我们可以把这条线段删掉。

对于这种偏序关系,用 CDQ 分治建图就可以了。这样我们要跑一个点数和边数均为 O(nlog⁡n)O(n \log n) 级别的费用流,那么单次复杂度大概是 O(kn2log⁡2n)O(kn^2 \log^2 n) 级别的。

注意到边权非负,那么原始对偶一下就可以做到 O(knlog⁡2n)O(kn \log^2 n),可以通过。

C++
#include <bits/stdc++.h>
#include "game.h"
#define ll long long
using namespace std;
namespace qwq {
    const int N = 50005;
    const ll inf = 1e14;
    int n, m, k;
    bool bel[N << 1];
    struct Cat { ll sx, sy, tx, ty, w; }a[N];
    struct tNode { ll x, y; int id; bool typ; }p[N << 1], tmp[N << 1];
    bool cmpx(tNode x, tNode y) {
        if(x.x == y.x) {
            if(x.y == y.y) return x.typ < y.typ;
            else return x.y < y.y;
        }
        else return x.x > y.x;
    }
    bool cmpy(tNode x, tNode y) { return x.y == y.y ? x.typ < y.typ : x.y < y.y; }
    namespace Flow {
        const int _N = 2000005, _M = 5000005;
        int s, t, tot, head[_N], cur[_N], cntedge = 2;
        bool vis[_N];
        ll dis[_N], h[_N];
        struct Edge { int v, nxt; ll res, cost; }e[_M << 1];
        void Reset() { cntedge = 2, memset(head, 0, sizeof(head)); }
        void Add_Edge(int u, int v, ll cap, ll cost) {
            e[cntedge] = {v, head[u], cap, cost}, head[u] = cntedge++;
            e[cntedge] = {u, head[v], 0ll, -cost}, head[v] = cntedge++;
        }
        bool Dijkstra() {
            int i; priority_queue<pair<ll, int> > q;
            queue<pair<ll, int> > _q;
            for(i = 1; i <= tot; i++) dis[i] = inf;
            dis[s] = 0, q.emplace(0ll, s);
            while(!q.empty() || !_q.empty()) {
                int u; ll d;
                if(_q.empty()) u = q.top().second, d = -q.top().first, q.pop();
                else u = _q.front().second, d = _q.front().first, _q.pop();
                if(vis[u]) continue;
                vis[u] = true;
                for(i = head[u]; i; i = e[i].nxt) if(e[i].res) {
                    int v = e[i].v; ll w = h[u] - h[v] + e[i].cost;
                    if(dis[v] > d + w) {
                        if(!w) dis[v] = d, _q.emplace(dis[v], v);
                        else dis[v] = d + w, q.emplace(-dis[v], v);
                    }
                }
            }
            for(i = 1; i <= tot; i++) vis[i] = false;
            return dis[t] != inf;
        }
        ll Dfs(ll &cost, int u, ll flow) {
            if(u == t || !flow) return flow;
            vis[u] = true;
            for(; cur[u]; cur[u] = e[cur[u]].nxt) {
                int i = cur[u], v = e[i].v; ll w = e[i].res, c = e[i].cost;
                if(!vis[v] && w && h[u] + c == h[v]) {
                    ll r = Dfs(cost, v, min(flow, w));
                    if(r) {
                        e[i].res -= r, e[i ^ 1].res += r, cost += r * c;
                        vis[u] = false; return r;
                    }
                }
            }
            vis[u] = false;
            return 0ll;
        }
        pair<ll, ll> Solve() {
            int i; ll res = 0, ans = 0;
            for(i = 1; i <= tot; i++) h[i] = 0;
            while(Dijkstra()) {
                for(i = 1; i <= tot; i++) h[i] += dis[i], cur[i] = head[i];
                res += Dfs(ans, s, inf);
            }
            return make_pair(res, ans);
        }
    }
    void Build(int l, int r) {
        if(l == r) return ;
        int mid = (l + r) >> 1, i = l, j = mid + 1, k = l, lst = 0;
        Build(l, mid), Build(mid + 1, r);
        while(i <= mid && j <= r) {
            if(cmpy(p[i], p[j])) bel[k] = false, tmp[k++] = p[i++];
            else bel[k] = true, tmp[k++] = p[j++];
        }
        while(i <= mid) bel[k] = false, tmp[k++] = p[i++];
        while(j <= r) bel[k] = true, tmp[k++] = p[j++];
        for(i = l; i <= r; i++) {
            p[i] = tmp[i];
            int u = ++Flow::tot;
            if(lst) Flow::Add_Edge(lst, u, inf, 0ll);
            if(!bel[i] && !p[i].typ) Flow::Add_Edge(p[i].id + n, u, inf, 0ll);
            if(bel[i] && p[i].typ) Flow::Add_Edge(u, p[i].id, inf, 0ll);
            lst = u;
        }
    }
    ll Solve() {
        int i, _s; Flow::tot = n * 2, Flow::s = ++Flow::tot, Flow::t = ++Flow::tot;
        _s = ++Flow::tot, Flow::Add_Edge(Flow::s, _s, k, 0ll);
        for(i = 1; i <= n; i++) {
            if(a[i].sx == a[i].tx) swap(a[i].sy, a[i].ty);
            // cerr << a[i].sx << ' ' << a[i].sy << ' ' << a[i].tx << ' ' << a[i].ty << ' ' << a[i].w << endl;
            if(a[i].sx == a[i].sy) Flow::Add_Edge(_s, i, inf, 0ll);
            if(a[i].tx - a[i].ty == m * 2) Flow::Add_Edge(i + n, Flow::t, inf, 0ll);
            Flow::Add_Edge(i, i + n, 1, a[i].w);
            p[i] = {a[i].sx, a[i].sy, i, true}, p[i + n] = {a[i].tx, a[i].ty, i, false};
        }
        sort(p + 1, p + n * 2 + 1, cmpx), Build(1, n * 2);
        auto res = Flow::Solve();
        if(res.first != k) return -1ll;
        else return res.second;
    }
}
void init(int c, int t) {}
ll game(int n, int m, int k, vector<int> a, vector<int> b, vector<int> t, vector<int> w) {
    int i; qwq::n = n, qwq::m = m, qwq::k = k;
    for(i = 0; i < n; i++) {
        ll ty = t[i] + abs(a[i] - b[i]);
        qwq::a[i + 1] = {a[i] + t[i], t[i] - a[i], (ll)(b[i]) + ty, ty - (ll)(b[i]), w[i]};
    }
    ll res = qwq::Solve(); qwq::Flow::Reset(); return res;
}
原文
存档评论1 条抓取于