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个不同的数字,每个数字都在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 ;
}

Codeforces CF#628 Education 8 F. Bear and Fair Set的更多相关文章

  1. Codeforces CF#628 Education 8 C. Bear and String Distance

    C. Bear and String Distance time limit per test 1 second memory limit per test 256 megabytes input s ...

  2. Codeforces CF#628 Education 8 D. Magic Numbers

    D. Magic Numbers time limit per test 2 seconds memory limit per test 256 megabytes input standard in ...

  3. Codeforces CF#628 Education 8 E. Zbazi in Zeydabad

    E. Zbazi in Zeydabad time limit per test 5 seconds memory limit per test 512 megabytes input standar ...

  4. Codeforces CF#628 Education 8 B. New Skateboard

    B. New Skateboard time limit per test 1 second memory limit per test 256 megabytes input standard in ...

  5. Codeforces CF#628 Education 8 A. Tennis Tournament

    A. Tennis Tournament time limit per test 1 second memory limit per test 256 megabytes input standard ...

  6. Educational Codeforces Round 8 F. Bear and Fair Set 最大流

    F. Bear and Fair Set 题目连接: http://www.codeforces.com/contest/628/problem/F Description Limak is a gr ...

  7. codeforces 628F. Bear and Fair Set 网络流

    题目链接 F. Bear and Fair Set time limit per test 2 seconds memory limit per test 256 megabytes input st ...

  8. Codeforces Good Bye 2017 908F F. New Year and Rainbow Roads

    题 OvO http://codeforces.com/contest/908/problem/F CF 908F 解 需要注意细节的模拟题. 如果三种颜色都存在,则记每两个相邻的G组成一个段,对每个 ...

  9. Codeforces Round #376 (Div. 2)F. Video Cards(前缀和)

    题目链接:http://codeforces.com/contest/731/problem/F 题意:有n个数,从里面选出来一个作为第一个,然后剩下的数要满足是这个数的倍数,如果不是,只能减小为他的 ...

随机推荐

  1. Open xml 操作Excel 透视表(Pivot table)-- 实现Excel多语言报表

    我的一个ERP项目中,客户希望使用Excel Pivot table 做分析报表. ERP 从数据库中读出数据,导出到Excel中的数据源表(统一命名为Data),刷新Pivot table! 客户还 ...

  2. java基础 super 子类调用父类

    如果希望在子类中,去调用父类的构造方法,要求在子类的构造函数调用 example如下: package test; /* * 如果希望在子类中,去调用父类的构造方法,要求在子类的构造函数调用 * */ ...

  3. html显示缩略小图 无失真图片

    <html> <head> <title>我的图片处理</title> <style type="text/css"> ...

  4. 163邮件出错:不允许使用邮箱名称。 服务器响应为: authentication is required,smtp7,C8CowEDpS0+Uke9VvSmXBg--.546S2 1441763733

    原因:用163邮箱发邮件,需开启smtp服务,开启服务时,要求使用客户端授权码. 在.net中,使用smtp发邮件,在验证中使用的密码,是上面所讲的客户端授权码,而不是注册和web登录时用的邮箱密码. ...

  5. Web Config配置备忘

    数据压缩 <httpCompression>节点用于配置静态压缩和动态压缩,<urlCompression>则用于开关 http压缩 <urlCompression do ...

  6. 在Web Api中集成protobuf

    安装WebApiContrib.Formatting.ProtoBuf Install-Package WebApiContrib.Formatting.ProtoBuf 注册ProtoBufForm ...

  7. 【原创】threejs实现一个全景地球

    介绍 本demo实现一个旋转的全景地球,效果如下 技术分析 1.球体 2.球体表面贴图 实现 创建容器 <div id="container"></div> ...

  8. php学习中——知识点(1)

    php是嵌入式脚本语言(意义也就不言而喻) 标识:<?php ....  ?>         输出:echo "**"; 使用美元符号($)后跟变量名表示变量,区分大 ...

  9. ios 获取文件扩展名备忘

    NSString *lastComponent = [cachePath lastPathComponent];              NSString *pathLessFilename = [ ...

  10. JDBC、DAO

    JDBC是Java数据库连接技术的简称,提供连接各种常用数据库的能力 JDBC的工作原理 JDBC 驱动器由数据库厂商提供 1.在个人开发与测试中,可以使用JDBC-ODBC桥连方式 2.在生产型开发 ...