题意:
给定N,B,Q≤104,N能被5整除
N为set大小(无相同元素),元素范围为[1,B],需满足set里元素模5的余数为[0,4]的元素个数相等
Q个条件,bi cnti,表示[1,bi]应该有cnti个数
问这种set是否存在,存在输出“fair”,否则“unfair”
分析:
把题目条件(B,N)和Q次询问一起按照Bi排序,这样形成了Q+1个不相交区间(加上一个(0,0))
建图,源点→Remainders0∼4,容量为n/5;区间→汇点,容量为cnti
Remaindersi→区间,容量为区间里数模5余Remainderi的个数
考虑求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定理:对于X部的子集Xmask,假设Xmask在Y部的匹配数为M(Xmask)
如果X部的所有子集Xmask,均有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;
}