2 seconds
256 megabytes
standard input
standard output
Limak is a grizzly bear. He is big and dreadful. You were chilling in the forest when you suddenly met him. It's very unfortunate for you. He will eat all your cookies unless you can demonstrate your mathematical skills. To test you, Limak is going to give you a puzzle to solve.
It's a well-known fact that Limak, as every bear, owns a set of numbers. You know some information about the set:
- The elements of the set are distinct positive integers.
- The number of elements in the set is n. The number n is divisible by 5.
- All elements are between 1 and b, inclusive: bears don't know numbers greater than b.
- For each r in {0, 1, 2, 3, 4}, the set contains exactly elements that give remainder r when divided by 5. (That is, there are elements divisible by 5, elements of the form 5k + 1, elements of the form 5k + 2, and so on.)
Limak smiles mysteriously and gives you q hints about his set. The i-th hint is the following sentence: "If you only look at elements that are between 1 and upToi, inclusive, you will find exactly quantityi such elements in my set."
In a moment Limak will tell you the actual puzzle, but something doesn't seem right... That smile was very strange. You start to think about a possible reason. Maybe Limak cheated you? Or is he a fair grizzly bear?
Given n, b, q and hints, check whether Limak can be fair, i.e. there exists at least one set satisfying the given conditions. If it's possible then print ''fair". Otherwise, print ''unfair".
The first line contains three integers n, b and q (5 ≤ n ≤ b ≤ 104, 1 ≤ q ≤ 104, n divisible by 5) — the size of the set, the upper limit for numbers in the set and the number of hints.
The next q lines describe the hints. The i-th of them contains two integers upToi and quantityi (1 ≤ upToi ≤ b, 0 ≤ quantityi ≤ n).
Print ''fair" if there exists at least one set that has all the required properties and matches all the given hints. Otherwise, print ''unfair".
10 20 1
10 10
fair
10 20 3
15 10
5 0
10 5
fair
10 20 2
15 3
20 10
unfair
In the first example there is only one set satisfying all conditions: {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}.
In the second example also there is only one set satisfying all conditions: {6, 7, 8, 9, 10, 11, 12, 13, 14, 15}.
Easy to see that there is no set satisfying all conditions from the third example. So Limak lied to you :-(
题意:
有n个不同的数字,每个数字都在1-b之间。
按照每个数对5取余进行分类,每一类的数都有n/5个。
现在给出q个条件。
1-up_i之间,包含那n个数的个数。
求,这些条件有没有可能全为真。
题解:
这道题目很精妙。
是一道简单的线性规划和网络流问题。
首先根据q个条件我们可以得到若干个区间,
知道这些区间各自包含n个数字的哪几个数。
首先对于每个区间,
我们可以设x0,x1,x2..x4为每个类里面的数的数目。
然后这么多个区间,我们得到了xi,0、xi,1、...、xi,4,
并且我们有sigma(xi_0)=n/5,
并且每个数非零。 那如果我们这样建图,
由源向li连边,流量为n/5,
由汇向ri连边,流量为各个区间包含的数目。
每个li代表第i类点的总数,ri代表区间的数目总和。
那么每个li向每个ri连边,流量代表ri这个区间最多包含几个li这个类的点。
如果有可行流(满流),代表有一种分配方案符合条件。
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <ctime>
#include <string>
#include <cmath>
#include <iostream>
#include <algorithm>
#include <queue>
#include <vector>
#include <map>
#include <set>
#include <deque>
using namespace std;
typedef long long ll;
const int INF = , MIT = (1ll << ) - ;
const ll MLL = 1e18 + ;
#define sz(x) ((int) (x).size()) class ISap {
/**
* Time Complexity: O(N^2*M)
* Special Case Time Complexity:
* If the capacity and flow are integer, the time complexity is
* O(min{N^(2/3), M^(1/2)}*M)
* Improve:
* Gap, Multiple Augmented, Manual Stack, Active Label
* Usage:
* init()
* {addEdge()}
* [preLabel()]
* maxflow()
* preLabel function is not nessary. But will slower 10% if not use.
* */
private :
static const int N = , M = , INF = ;
public :
int fir[N], nex[M * ], son[M * ], cap[M * ], flow[M * ], totEdge;
int gap[N], dep[N], src, des, totPoint; void init(int _totPoint) {
totEdge = , totPoint = _totPoint;
for(int i = ; i <= totPoint; ++i)
gap[i] = dep[i] = , fir[i] = -;
} void addEdge(const int u, const int v, const int c) {
nex[totEdge] = fir[u], son[totEdge] = v, cap[totEdge] = c, flow[totEdge] = ;
fir[u] = totEdge++;
nex[totEdge] = fir[v], son[totEdge] = u, cap[totEdge] = c, flow[totEdge] = c;
fir[v] = totEdge++;
} void preLabel() {
static int que[N], head, tail;
head = tail = ;
for(int i = ; i <= totPoint; ++i) dep[i] = totPoint, gap[i] = ; que[tail++] = des, dep[des] = , ++gap[];
while(head < tail) {
int u = que[head++];
for(int tab = fir[u], v; tab != -; tab = nex[tab]) {
if(flow[tab ^ ] >= cap[tab ^ ]) continue;
if(dep[v = son[tab]] < totPoint) continue;
++gap[dep[v] = dep[u] + ], que[tail++] = v;
}
}
} int maxflow() {
static int cur[N], rev[N];
int flows = ;
for(int i = ; i <= totPoint; ++i) cur[i] = fir[i];
for(int u = src; dep[u] < totPoint && dep[src] < totPoint; ) {
if(u == des) {
int nowFlow = INF;
for(int p = src; p != des; p = son[cur[p]])
nowFlow = min(nowFlow, cap[cur[p]] - flow[cur[p]]);
for(int p = src; p != des; p = son[cur[p]])
flow[cur[p]] += nowFlow, flow[cur[p] ^ ] -= nowFlow;
flows += nowFlow, u = src;
} int tab;
for(tab = cur[u]; tab != -; tab = nex[tab])
if(cap[tab] > flow[tab] && dep[u] == dep[son[tab]] + ) break;
if(tab != -) {
cur[u] = tab, rev[son[tab]] = tab ^ ;
u = son[tab];
} else {
if(--gap[dep[u]] == ) break;
cur[u] = fir[u];
int minDep = totPoint;
for(tab = fir[u]; tab != -; tab = nex[tab])
if(cap[tab] > flow[tab]) minDep = min(minDep, dep[son[tab]]);
++gap[dep[u] = minDep + ];
if(u != src) u = son[rev[u]];
}
}
return flows;
}
};
int n, b, q;
vector<pair<int, int> > hints; int calculate(int lef, int rig, int r) {
if(!r) r = ;
--lef;
rig = rig / + (rig % >= r);
lef = lef < ? : lef / + (lef % >= r);
return rig - lef;
} void solve() {
hints.push_back(make_pair(b, n)), ++q;
sort(hints.begin(), hints.end()); ISap *maxflow = new ISap();
maxflow->init( + q + );
maxflow->src = + q + , maxflow->des = + q + ;
int st = , lastQuan = ;
for(int remainder = ; remainder < ; ++remainder)
maxflow->addEdge(maxflow->src, remainder + , n / );
for(int i = ; i < q; ++i) {
int nowQuan = hints[i].second - lastQuan;
if(nowQuan < ) {
puts("unfair");
return;
}
maxflow->addEdge( + i + , maxflow->des, nowQuan); int ed = hints[i].first;
for(int remainder = ; remainder < ; ++remainder) {
int cnt = calculate(st, ed, remainder);
maxflow->addEdge(remainder + , + i + , cnt);
}
st = ed + , lastQuan = hints[i].second;
} int ans = maxflow->maxflow(); puts(ans == n ? "fair" : "unfair");
} int main() {
scanf("%d%d%d", &n, &b, &q);
for(int i = ; i < q; ++i) {
int upto, quan;
scanf("%d%d", &upto, &quan);
hints.push_back(make_pair(upto, quan));
}
solve();
return ;
}