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个数,从里面选出来一个作为第一个,然后剩下的数要满足是这个数的倍数,如果不是,只能减小为他的 ...
 
随机推荐
- php编译 :virtual memory exhausted: Cannot allocate memory
			
有时候用vps建站时需要通过编译的方式来安装主机控制面板.对于大内存的VPS来说一般问题不大,但是对于小内存,比如512MB内存的VPS来说,很有可能会出现问题,因为编译过程是一个内存消耗较大的动作. ...
 - APP支付报错ALI40247处理方案!
			
简直日狗!这里要吐槽支付宝: 1.支付宝文档太复杂,分类虽然详细,但是我找不到app支付 对应服务端的demo 2.提供下载的sdk都是全整合的 用下来都是一条龙服务,还有一些客户端(app)的请求也 ...
 - yii2 登录用户和未登录用户使用不同的 layout
			
可以在配置文件中增加一个 “beforeRequest” 事件: 'on beforeRequest' => function () { Yii::$app->layout = Yii:: ...
 - 【TortoiseGit】TortoiseGit将本地库push到远端
			
以前也在使用GitHub,2年前电脑上就装了TortoiseGit和SVN,公司也在用Git,但是并没有刻意去做一些事情,未免觉得有些生疏,今天闲来无聊.玩了一把.[做中成长] 对于GitToiseG ...
 - ASP.NET MVC 5 Jquery Validate
			
ClientValidationEnabled 在asp.net mvc 5中ClientValidationEnabled默认为TRUE,所以也不需要刻意去设置 应用ValidationAttrib ...
 - 【TIP】已经上架的app在AppStore上搜不到的解决办法
			
相信很多人都遇到过这个问题,天天刷iTunes connect,终于发现app已经上架了,兴奋的跑过去告诉老板,老板说好,大家都装一个吧!然后大家过来问你怎么搜不到,老板看你都是一副这个表情: 这 ...
 - web前端页面性能优化
			
影响用户访问的最大部分是前端的页面.网站的划分一般为二:前端和后台.我们可以理解成后台是用来实现网站的功能的,比如:实现用户注册,用户能够为文章发表评论等等.而前端呢?其实应该是属于功能的表现. 而我 ...
 - JAVA基础 Exception, Error
			
转载请附上本文地址: http://www.cnblogs.com/nextbin/p/6219677.html 本文参考: JAVA源码 http://swiftlet.net/archives/9 ...
 - 【纯css】左图右文列表,左图外框宽度占一定百分比的正方形,右上下固定,右中自动响应高度。支持不规则图片。
			
查看演示 <!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF- ...
 - tp框架之session
			
系统提供了Session管理和操作的完善支持,全部操作可以通过一个内置的session函数完成,该函数可以完成Session的设置.获取.删除和管理操作. session初始化设置 如果session ...