codeforces 628F. 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个数, 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 ;
}
codeforces 628F. Bear and Fair Set 网络流的更多相关文章
- Codeforces 628F Bear and Fair Set
题意: 给定若干个上限upto以及集合中在[1,upto]中的元素个数,问是否存在这样的集合使得集合中的元素除以5的余数的个数相等. 分析: 首先可以想到区间的数与其除以5的余数和区间编号分别一一对应 ...
- 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 CF#628 Education 8 F. Bear and Fair Set
F. Bear and Fair Set time limit per test 2 seconds memory limit per test 256 megabytes input standar ...
- Codeforces 385C Bear and Prime Numbers
题目链接:Codeforces 385C Bear and Prime Numbers 这题告诉我仅仅有询问没有更新通常是不用线段树的.或者说还有比线段树更简单的方法. 用一个sum数组记录前n项和, ...
- Codeforces 385B Bear and Strings
题目链接:Codeforces 385B Bear and Strings 记录下每一个bear的起始位置和终止位置,然后扫一遍记录下来的结构体数组,过程中用一个变量记录上一个扫过的位置,用来去重. ...
- Codeforces 680D Bear and Tower of Cubes 贪心 DFS
链接 Codeforces 680D Bear and Tower of Cubes 题意 求一个不超过 \(m\) 的最大体积 \(X\), 每次选一个最大的 \(x\) 使得 \(x^3\) 不超 ...
- Codeforces 385C Bear and Prime Numbers(素数预处理)
Codeforces 385C Bear and Prime Numbers 其实不是多值得记录的一道题,通过快速打素数表,再做前缀和的预处理,使查询的复杂度变为O(1). 但是,我在统计数组中元素出 ...
- [Codeforces 639F] Bear and Chemistry (Tarjan+虚树)(有详细注释)
[Codeforces 639F] Bear and Chemistry(Tarjan+虚树) 题面 给出一个n个点,m条边的无向图(不保证连通,可能有自环和重边),有q次询问,每次询问给出p个点和q ...
- Codeforces 791B Bear and Friendship Condition(DFS,有向图)
B. Bear and Friendship Condition time limit per test:1 second memory limit per test:256 megabytes in ...
随机推荐
- Largest Submatrix(动态规划)
Largest Submatrix Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others ...
- 怎样通过css的media属性,适配不同分辨率的终端设备?
怎样通过css的media属性,适配不同分辨率的终端设备,示比例如以下: <!DOCTYPE html> <html> <head> <title>首页 ...
- HDU ACM 1046 Gridland 找规律
分析:给出一个矩阵.问最短从一个点经过全部点以此回到起点的长度是多少.绘图非常好理解.先画3*4.3*3.4*4的点阵图案.试着在上面用最短路走一走,能够发现当矩形点阵的长宽都是奇数时,最短路中必然有 ...
- Android 自动编译、打包生成apk文件 3 - 使用SDK Ant方式
相关文章列表: < Android 自动编译.打包生成apk文件 1 - 命令行方式> < Android 自动编译.打包生成apk文件 2 - 使用原生Ant方式> &l ...
- SQL 常用基础语句
1.SQL SELECT 语句 语法:SELECT 列名称 FROM 表名称 2.SQL SELECT DISTINCT 语句 语法:SELECT DISTINCT 列名 ...
- C#Excel导出导入
using System; using System.Collections.Generic; using NPOI; using NPOI.HPSF; using NPOI.HSSF; using ...
- Javascript知识四(DOM)
[箴 10:4] 手懒的,要受贫穷:手勤的,却要富足. He becometh poor that dealeth with a slack hand: but the hand of the di ...
- JAVA编译中拒绝访问的问题及解决方案
在java编译时出现,可以将C盘内的文件转移到其他盘,此问题可能是权限不足不能够读取C盘文件造成的. 文件名与类名要一致,包括大小写,也是要一致!
- EL表达式中引用隐式变量
除了在jsp中9大隐式变量(在前面文章也叫预定义变量)在转化成为servlet后_jspService中可以看到: public void _jspService(final javax.servle ...
- windows系统中的dll的作用详细解释
什么是.DLL文件? DLL 是一个包含可由多个程序同时使用的代码和数据的库.例如,在 Windows 操作系统中,Comdlg32 DLL 执行与对话框有关的常见函数.因此,每个程序都可以使用该 D ...