Educational Codeforces Round 8 F. Bear and Fair Set(最大流 | Hall定理)

題意:

N,B,Q104,N5
Nset(),[1,B],滿set5[0,4]
Q,bi cnti,[1,bi]cnti
set,fair,unfair

分析:

(B,N)QBi,Q+1((0,0))
,Remainders04,n/5;,cnti
Remaindersi,5Remainderi
Remainders,N

代碼:

//
//  Created by TaoSama on 2016-02-22
//  Copyright (c) 2016 TaoSama. All rights reserved.
//
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <set>
#include <vector>

using namespace std;
#define pr(x) cout << #x << " = " << x << "  "
#define prln(x) cout << #x << " = " << x << endl
const int N = 1e5 + 10, INF = 0x3f3f3f3f, MOD = 1e9 + 7;
const int M = 5e5 + 10;

int head[N], pnt[M], cap[M], nxt[M], cnt;

void add_edge(int u, int v, int w) {
    pnt[cnt] = v;
    cap[cnt] = w;
    nxt[cnt] = head[u];
    head[u] = cnt++;
}

void add_double(int u, int v, int w1, int w2 = 0) {
    add_edge(u, v, w1);
    add_edge(v, u, w2);
}

int lev[N], cur[N];
bool bfs(int s, int t) {
    queue<int> q;
    memset(lev, 0, sizeof lev);
    q.push(s);  lev[s] = 1;
    while(q.size() && !lev[t]) {
        int u = q.front(); q.pop();
        for(int i = head[u]; ~i; i = nxt[i]) {
            int v = pnt[i];
            if(cap[i] > 0 && !lev[v]) {
                lev[v] = lev[u] + 1;
                q.push(v);
            }
        }
    }
    return lev[t];
}

int dfs(int u, int t, int delta) {
    if(u == t || !delta) return delta;
    int ret = 0;
    for(int i = cur[u]; ~i; i = nxt[i]) {
        int v = pnt[i];
        if(cap[i] > 0 && lev[v] == lev[u] + 1) {
            int d = dfs(v, t, min(delta, cap[i]));
            cur[u] = i;
            ret += d; delta -= d;
            cap[i] -= d;
            cap[i ^ 1] += d;

            if(delta == 0) return ret;
        }
    }
    lev[u] = 0;
    return ret;
}

int dinic(int s, int t) {
    int ret = 0;
    while(bfs(s, t)) {
        for(int i = s; i <= t; ++i) cur[i] = head[i];
        ret += dfs(s, t, INF);
    }
    return ret;
}

int n, b, q;

int main() {
#ifdef LOCAL
    freopen("C:\\Users\\TaoSama\\Desktop\\in.txt", "r", stdin);
//  freopen("C:\\Users\\TaoSama\\Desktop\\out.txt","w",stdout);
#endif
    ios_base::sync_with_stdio(0);

    while(scanf("%d%d%d", &n, &b, &q) == 3) {
        cnt = 0; memset(head, -1, sizeof head);
        vector<pair<int, int> > v;
        v.push_back({b, n});
        while(q--) {
            int x, y; scanf("%d%d", &x, &y);
            v.push_back({x, y});
        }
        sort(v.begin(), v.end());

        int s = 0, t = 5 + v.size() + 1;
        for(int i = 1; i <= 5; ++i) add_double(s, i, n / 5);
        pair<int, int> pre = {0, 0};
        bool ok = true;
        for(int i = 0; i < v.size(); ++i) {
            auto& cur = v[i];
            int sz = cur.first - pre.first, cnt = cur.second - pre.second;
            if(cnt < 0 || sz < cnt) {ok = false; break;}
            add_double(6 + i, t, cnt);
            for(int j = 1; j <= 5; ++j) {
                int w = (cur.first + 5 - j) / 5 - (pre.first + 5 - j) / 5;
                add_double(j, 6 + i, w);
            }
            pre = cur;
        }
        if(!ok || dinic(s, t) != n) puts("unfair");
        else puts("fair");
    }
    return 0;
}

Hall
Hall:XXmask,XmaskYM(Xmask)
XXmask,M(Xmask)|Xmask|,
O(25N)

題解代碼:

// Bear and Fair Set, by Errichto
#include<bits/stdc++.h>
using namespace std;

const int K = 5;

void NO() {
    puts("unfair");
    exit(0);
}

int main() {
    int n, b, q;
    scanf("%d%d%d", &n, &b, &q);
    vector<pair<int,int>> w;
    w.push_back(make_pair(b, n));
    while(q--) {
        int a, b;
        scanf("%d%d", &a, &b);
        w.push_back(make_pair(a, b));
    }
    sort(w.begin(), w.end());
    // use the Hall's theorem
    // check all 2^K sets of remainders
    for(int mask = 0; mask < (1 << K); ++mask) {
        int at_least = 0, at_most = 0;

        int prev_upto = 0, prev_quan = 0;
        for(pair<int,int> query : w) {
            int now_upto = query.first, now_quan = query.second;
            int places_matching = 0; // how many do give remainder from "mask"
            int places_other = 0;
            for(int i = prev_upto + 1; i <= now_upto; ++i) {
                if(mask & (1 << (i % K)))
                    ++places_matching;
                else
                    ++places_other;
            }
            if(now_quan < prev_quan) NO();
            int quan = now_quan - prev_quan;
            int places_total = now_upto - prev_upto;
            assert(places_total == places_matching + places_other);
            if(quan > places_total) NO();

            at_least += max(0, quan - places_other);
            at_most += min(quan, places_matching);

            prev_upto = now_upto;
            prev_quan = now_quan;
        }

        // "mask" represents a set of popcount(mask) remainders
        // their total degree is (n/K)*popcount(mask)
        int must_be = n / K * __builtin_popcount(mask);
        if(!(at_least <= must_be && must_be <= at_most)) NO();
    }
    puts("fair");       
    return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章