题目链接

F. Bear and Fair Set
time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

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 nbq 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".

Input

The first line contains three integers nb 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).

Output

Print ''fair" if there exists at least one set that has all the required properties and matches all the given hints. Otherwise, print ''unfair".

Examples
input
10 20 1
10 10
output
fair
input
10 20 3
15 10
5 0
10 5
output
fair
input
10 20 2
15 3
20 10
output
unfair
Note

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个数, n是5的倍数,这n个数都不大于b, 并且不相同。 然后刚好有n/5个数%5余0, n/5个数%5余1……。

然后给你q个限制, 每个限制给出2个数x, y。 说明不大于x的数有y个。 然后问你能否找到一个这样的集合, 满足所给的条件。

一道网络流的题, 首先如果x1>x2但是y1<y2, 那么肯定不满足。

源点先向1, 2, 3, 4, 5这5个点连边, 表示余0, 1, 2, 3, 4这五种情况。

我们根据所给的x, 把[0, b]这个区间划分为q+1个小区间, 然后1, 2, 3, 4, 5这五个点, 分别向这q+1个区间连边,比如说1向某个区间连边, 权值就为这个区间里%5余0的数的个数, 以此类推。

然后每个区间向汇点连边, 权值为这个区间内的数的个数。

跑一遍网络流, 看结果是否等于n。

#include <iostream>
#include <vector>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <map>
#include <set>
#include <string>
#include <queue>
#include <stack>
#include <bitset>
using namespace std;
#define pb(x) push_back(x)
#define ll long long
#define mk(x, y) make_pair(x, y)
#define lson l, m, rt<<1
#define mem(a) memset(a, 0, sizeof(a))
#define rson m+1, r, rt<<1|1
#define mem1(a) memset(a, -1, sizeof(a))
#define mem2(a) memset(a, 0x3f, sizeof(a))
#define rep(i, n, a) for(int i = a; i<n; i++)
#define fi first
#define se second
typedef pair<int, int> pll;
const double PI = acos(-1.0);
const double eps = 1e-;
const int mod = 1e9+;
const int inf = ;
const int dir[][] = { {-, }, {, }, {, -}, {, } };
const int maxn = 1e6+;
int q[maxn*], head[maxn*], dis[maxn/], s, t, num;
struct node
{
int to, nextt, c;
node(){}
node(int to, int nextt, int c):to(to), nextt(nextt), c(c){}
}e[maxn*];
void init() {
num = ;
mem1(head);
}
void add(int u, int v, int c) {
e[num] = node(v, head[u], c); head[u] = num++;
e[num] = node(u, head[v], ); head[v] = num++;
}
int bfs() {
mem(dis);
dis[s] = ;
int st = , ed = ;
q[ed++] = s;
while(st<ed) {
int u = q[st++];
for(int i = head[u]; ~i; i = e[i].nextt) {
int v = e[i].to;
if(!dis[v]&&e[i].c) {
dis[v] = dis[u]+;
if(v == t)
return ;
q[ed++] = v;
}
}
}
return ;
}
int dfs(int u, int limit) {
if(u == t) {
return limit;
}
int cost = ;
for(int i = head[u]; ~i; i = e[i].nextt) {
int v = e[i].to;
if(e[i].c&&dis[v] == dis[u]+) {
int tmp = dfs(v, min(limit-cost, e[i].c));
if(tmp>) {
e[i].c -= tmp;
e[i^].c += tmp;
cost += tmp;
if(cost == limit)
break;
} else {
dis[v] = -;
}
}
}
return cost;
}
int dinic() {
int ans = ;
while(bfs()) {
ans += dfs(s, inf);
}
return ans;
}
int a[], sum[];
int main()
{
int n, b, q, x;
cin>>n>>b>>q;
for(int i = ; i<=q; i++) {
scanf("%d%d", &a[i], &x);
sum[a[i]] = x;
}
sort(a+, a+q+);
if(a[q]!=n) {
a[++q] = b;
sum[b] = n;
}
for(int i = ; i<=q; i++) {
if(a[i]<sum[a[i]]) {
puts("unfair");
return ;
}
if(sum[a[i]]<sum[a[i-]]) {
puts("unfair");
return ;
}
}
s = ;
init();
for(int i = ; i<=; i++) {
add(s, i, n/);
}
for(int i = ; i<=; i++) {
for(int j = ; j<=q; j++) {
int sum1 = a[j]/+(a[j]%>=i);
int sum2 = a[j-]/+(a[j-]%>=i);
add(i, +j, sum1-sum2);
}
}
t = +q+;
for(int i = ; i<=q; i++) {
add(i+, t, sum[a[i]]-sum[a[i-]]);
}
int ans = dinic();
if(ans == n) {
puts("fair");
} else {
puts("unfair");
}
return ;
}

最新文章

  1. 解决maven下载jar慢的问题(如何更换Maven下载源)
  2. git bash下对文件的操作
  3. Redis3.0 Install
  4. .NET自动识别HttpWebResponse的编码及是否压缩
  5. ExtJS5_MVVM特性的简单说明
  6. boost.asio系列——Timer
  7. shell printf格式化输出语句
  8. MVC3 Html.ActionLink
  9. nginx &amp;amp; flup &amp;amp; django &amp;amp; python3.x @ window7配置备忘录
  10. netflix zuul 学习
  11. MyBatis延迟加载和缓存
  12. 老男孩python学习之作业二---三级菜单
  13. [Swift]LeetCode349. 两个数组的交集 | Intersection of Two Arrays
  14. CentOS 7 安装Kubernetes(单机版)
  15. springcloud实战案例苏宁和海信
  16. 快捷键设置 keyiing.json
  17. Inno Setup入门(五)——添加readme文件
  18. &lt;[完整版]中国式价值投资&gt;读书笔记
  19. 重启oracle数据库的操作方法
  20. 2743: [HEOI2012]采花

热门文章

  1. JS判断是否安装flash player及当前版本
  2. linux经常使用(一)linux 安装配置 jdk之 找不到安装文件文件夹及source /etc/profile 报unexpected end of file 错误 解决
  3. 怎样在UICollectionView中添加Header和footer
  4. mysql 调用存储过程及例子
  5. Java socket字节流传输的示例
  6. SGU题目总结
  7. 关于Python网络爬虫实战笔记③
  8. 《JavaScript+DOM编程艺术》的摘要(二)---DOM中的几个方法
  9. java中常用的数据加密算法
  10. java selenium webdriver实战 页面元素定位