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个数,从里面选出来一个作为第一个,然后剩下的数要满足是这个数的倍数,如果不是,只能减小为他的 ...
随机推荐
- Azure上的那些IP
相信第一次接触Azure的读者都会碰到这样一个问题,就是Azure的IP地址,笔者第一次接触Azure也是被搞懵逼了,一会儿VIP,不知道的还以为是会员的意思呢,一会儿又是DIP,后来又来了个PIP, ...
- Java与各种数据库连接代码
6.MySQL数据库Class.forName("org.gjt.mm.mysql.Driver").newInstance();String url ="jdbc:my ...
- MongoDB安装配置示例
参考 http://www.runoob.com/mongodb/mongodb-window-install.html http://www.cnblogs.com/lecaf/archive/20 ...
- php多线程操作同一文件-待续
同意文件操作同意文件的问题在于逻辑有些地方不合适,如果多个线程同时写入,在不加锁的情况下,可能导致得到结果不如意,为了安全,和脏读(数据库的词),应该使用排他锁,这就意味着每次只能被一个线程操作.其他 ...
- bootstrap的table调用本列ID
我们是用json解析数据. 后台传送data数据~ String data = JSON.toJSONString(baseInfoService.list());request.setAttribu ...
- 如何方便的保存WinForm窗体控件的位置大小等等配置信息
由于分辨率.屏幕主题.字体大小的不同,窗体显示效果在不同机器上不尽相同.窗体的弹性设计并不能满足多样的需求.为保证在各种情况下,能有满意的效果.窗体的多样显示方式能改变,并且保存.载入配置,显得很重要 ...
- websocket与socket.io
什么是Websocket? Websocket是一个独立于http的实时通信协议,最初是在HTML5中被引用进来的,在HTML5规范中作为浏览器与服务器的核心通信技术被嵌入到浏览器中.WebSocke ...
- MVC 导出Excel 的其中一方法(View导出excel)
场景:mvc下导出excel 思路:使用View导出excel 步骤: 1.导出标签添加事件 $("#export_A").click(function(){ //省略代码.... ...
- jQuery简单实现iframe的高度根据页面内容自适应的方法(转)
本文实例讲述了jQuery简单实现iframe的高度根据页面内容自适应的方法.分享给大家供大家参考,具体如下: 方式1: //注意:下面的代码是放在和iframe同一个页面中调用 $("#i ...
- Duilib源码分析(五)UI布局—Layout与各子控件
接下来,继续分析duilib之UI布局Layout,目前提供的布局有:VerticalLayout.HorizontalLayout.TileLayout.TabLayout.ChildLayout分 ...