Codeforces CF#628 Education 8 F. Bear and Fair Set
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 ;
}
Codeforces CF#628 Education 8 F. Bear and Fair Set的更多相关文章
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- Codeforces Good Bye 2017 908F F. New Year and Rainbow Roads
题 OvO http://codeforces.com/contest/908/problem/F CF 908F 解 需要注意细节的模拟题. 如果三种颜色都存在,则记每两个相邻的G组成一个段,对每个 ...
- Codeforces Round #376 (Div. 2)F. Video Cards(前缀和)
题目链接:http://codeforces.com/contest/731/problem/F 题意:有n个数,从里面选出来一个作为第一个,然后剩下的数要满足是这个数的倍数,如果不是,只能减小为他的 ...
随机推荐
- hdu 4329
problem:http://acm.hdu.edu.cn/showproblem.php?pid=4329 题意:模拟 a. p(r)= R'/i rel(r)=(1||0) R ...
- I18n问题 国际化
http://www.cnblogs.com/guaniu/archive/2012/01/18/2325556.html java国际化 1.了解缺省Locale是由操作系统决定的,Locale是由 ...
- Uiautomator--断言的使用
一.断言函数的使用 1.用例结构: 自动化用例结构,一般可以分成一个用例集,然后用例集下面会有非常多的用例组成,我们可以从多个用例中抽出一些用例组成测试套件. 2.用例的标准结构: setUp:初始化 ...
- sublime text 2 快捷键
快捷键 功能 ctrl+shift+n 打开新Sublime ctrl+shift+w 关闭Sublime,关闭所有打开文件 ctrl+shift+t 重新打开最近关闭文件 ctrl+n 新建文件 c ...
- RecyclerView解密篇(二)
在上一篇(RecyclerView解密篇(一))文章中简单的介绍了RecyclerView的基本用法,接下来要来讲讲RecyclerView的更多用法,要实现不同的功能效果,大部分都还是在于Recyc ...
- js 数组删去重复的加上没有的元素
为了一个数组的删除操作竟然费了一个多小时,下面分享一下我的代码: 代码功能:判断数组里是否有我要看的元素,如果没有就添加到数组里,如果有就去掉. var selectArr=[]; function ...
- JS脚本语言是什么意思?
javascript,Javascript是一种浏览器端的脚本语言,用来在网页客户端处理与用户的交互,以及实现页面特效.比如提交表单前先验证数据合法性,减少服务器错误和压力.根据客户操作,给出一些提升 ...
- PHP之session与cookie
1.session与cookie的关系 众所周知,session是存储在服务器端,cookie是存储在客户端,如果禁用了浏览器的cookie功能,很多时候(除非进行了特殊配置)服务器端就无法再读取se ...
- Js 日期 多少分钟前,多少秒前
;(function(window){ /** * [dateDiff 算时间差] * @param {[type=Number]} hisTime [历史时间戳,必传] * @param {[typ ...
- [知识笔记]Java 基本数据类型的大小、取值范围、默认值
数据类型 大小(字节) 范围 默认值 boolean 1/8(1bit) true/false false byte 1 -128~127 (-2^7~2^7-1) 0 short 2 -32768~ ...