nowcoder网络流小Y写文章

Posted kaka0010

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了nowcoder网络流小Y写文章相关的知识,希望对你有一定的参考价值。

原题链接:https://ac.nowcoder.com/acm/problem/15600

题意

在这里插入图片描述

分析

求最大值的最小值问题,自然想到二分答案。然后就是建图跑网络流,我最开始的想法是m个段落去匹配n+1个空位,最后看流量是否满足m。很快就被证明是错误的,因为这样不能保证剩下n-m+1个空位是满足要求的。

既然要让n+1个位置都满足要求,那么必须让n+1个空位对m个段落匹配,但发现m肯定小于n,因此需要再用上一些本来就符合要求的段落作为虚点。如果你这个空位本来就符合要求,你可以向n-m+1个虚点连边,如果在插入段落之后满足要求,你可以向m个段落连边,最后跑一遍最大流,流量是n+1就符合要求。

Code

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> PII;
const ll INF = 1e9;
const int N = 400 + 10;
const int M = 3e6 + 10;
const int MOD = 1e9 + 7;
int st, ed;
struct Maxflow {
    int h[N], cnt, maxflow, deep[N], cur[N];
    struct Edge {
        int to, next;
        ll cap;
    } e[M<<1];

    void init() {
        memset(h, -1, sizeof h);
        cnt = maxflow = 0;
    }
    void add(int u, int v, int cap) {
        e[cnt].to = v;
        e[cnt].cap = cap;
        e[cnt].next = h[u];
        h[u] = cnt++;

        e[cnt].to = u;
        e[cnt].cap = 0;
        e[cnt].next = h[v];
        h[v] = cnt++;

    }

    bool bfs() {
        for (int i = 0; i <= ed; i++) deep[i] = -1, cur[i] = h[i];
        queue<int> q; q.push(st); deep[st] = 0;
        while (q.size()) {
            int u = q.front();
            q.pop();
            for (int i = h[u]; ~i; i = e[i].next) {
                int v = e[i].to;
                if (e[i].cap && deep[v] == -1) {
                    deep[v] = deep[u] + 1;
                    q.push(v);
                }
            }
        }
        if (deep[ed] >= 0) return true;
        else return false;
    }

    ll dfs(int u, ll mx) {
        ll a;
        if (u == ed) return mx;
        for (int i = cur[u]; ~i; i = e[i].next) {
            cur[u] = i;//优化
            int v = e[i].to;
            if (e[i].cap && deep[v] == deep[u] + 1 && (a = dfs(v, min(e[i].cap, mx)))) {
                e[i].cap -= a;
                e[i ^ 1].cap += a;
                return a;
            }
        }
        return 0;
    }

    void dinic() {
        ll res;
        while (bfs()) {
            while (1) {
                res = dfs(st, INF);
                if (!res) break;
                maxflow += res;
            }
        }
    }
}mf;
int a[N], b[N];
void solve() {
    int T; cin >> T; while (T--) {
        int n, m; cin >> n >> m;
        for (int i = 1; i <= n; i++) cin >> a[i];
        for (int i = 1; i <= m; i++) cin >> b[i];
        st = 2 * n + 2; ed = 2 * n + 3;
        int l = 1, r = 1e9;
        while (l <= r) {
            int mid = (l + r) >> 1;
            mf.init();
            for (int i = 0; i <= n; i++) mf.add(st, i, 1);
            for (int i = n+1; i <= 2*n+1; i++) mf.add(i, ed, 1);
            for (int i = 0; i <= n; i++) {
                if (i == 0) {
                    for (int j = n+m+1; j <= 2*n+1; j++) mf.add(i, j, 1);
                    for (int j = 1; j <= m; j++) {
                        if (abs(b[j] - a[1]) <= mid) mf.add(i, j+n, 1);
                    }
                } else if (i == n) {
                    for (int j = n+m+1; j <= 2*n+1; j++) mf.add(i, j, 1);
                    for (int j = 1; j <= m; j++) {
                        if (abs(b[j] - a[n]) <= mid) mf.add(i, j+n, 1);
                    }
                } else {
                    for (int j = n+m+1; j <= 2*n+1; j++) {
                        if (abs(a[i+1]-a[i]) <= mid) mf.add(i, j, 1);
                    }
                    for (int j = 1; j <= m; j++) {
                        if (abs(b[j] - a[i]) <= mid && abs(b[j] - a[i+1]) <= mid) mf.add(i, j+n, 1);
                    }
                }
            }
            mf.dinic();
            if (mf.maxflow >= n+1) r = mid - 1;
            else l = mid + 1;
        }
        cout << l << endl;
    }
}
signed main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
#ifdef ACM_LOCAL
    freopen("input", "r", stdin);
    freopen("output", "w", stdout);
    signed test_index_for_debug = 1;
    char acm_local_for_debug = 0;
    do {
        if (acm_local_for_debug == '$') exit(0);
        if (test_index_for_debug > 20)
            throw runtime_error("Check the stdin!!!");
        auto start_clock_for_debug = clock();
        solve();
        auto end_clock_for_debug = clock();
        cout << "Test " << test_index_for_debug << " successful" << endl;
        cerr << "Test " << test_index_for_debug++ << " Run Time: "
             << double(end_clock_for_debug - start_clock_for_debug) / CLOCKS_PER_SEC << "s" << endl;
        cout << "--------------------------------------------------" << endl;
    } while (cin >> acm_local_for_debug && cin.putback(acm_local_for_debug));
#else
    solve();
#endif
    return 0;
}

以上是关于nowcoder网络流小Y写文章的主要内容,如果未能解决你的问题,请参考以下文章

JAVA IO流小实例

Nowcoder 练习赛26 D xor序列 ( 线性基 )

[Nowcoder] 有向无环图 | 拓扑排序简单应用

Android 逆向Linux 文件权限 ( Linux 权限简介 | 系统权限 | 用户权限 | 匿名用户权限 | 读 | 写 | 执行 | 更改组 | 更改用户 | 粘滞 )(代码片段

nowcoder 寻找(LCA)

VSCode自定义代码片段14——Vue的axios网络请求封装