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 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".
Input
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).
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".
Sample Input
10 20 1
10 10
Sample Output
fair
Hint
题意
你有n个数,每个数都不同,且每个数都不大于b。
这n个数中恰好有n/5个数是5的倍数,n/5个数mod5=1,n/5个数mod5=2,n/5个数mod5=3,n/5个数mod5=4
然后有q个限制
限制说,[1,upToi]内,你选了quantityi个数
问你这个可不可能
题解:
网络流rush一波就好了
首先意识到,关于5的倍数这个东西,他是独立的,显然我选择mod=2的,不会选到mod=3的东西。
然后对于每个限制,我们都把区间给划开
本来是[1,l1] 1,l2的,我们变成[1,l1][l1+1,l2],这样分开来
然后我们根据这个来建立网络流去跑就好了
原点连5种余数,流量为n/5;5种余数分别连向每个区间,流量为每个区间的区间内该余数的数的数量;然后每个区间连向T,流量为该区间的大小
然后最大流看看是不是满流就好了
代码
#include<bits/stdc++.h>
using namespace std;
const int MAXN=300000,MAXM=300000,inf=1e9;
struct Edge
{
    int v,c,f,nx;
    Edge() {}
    Edge(int v,int c,int f,int nx):v(v),c(c),f(f),nx(nx) {}
} E[MAXM];
int G[MAXN],cur[MAXN],pre[MAXN],dis[MAXN],gap[MAXN],sz;
void init()
{
    sz=0; memset(G,-1,sizeof(G));
}
void link(int u,int v,int c)
{
    E[sz]=Edge(v,c,0,G[u]); G[u]=sz++;
    E[sz]=Edge(u,0,0,G[v]); G[v]=sz++;
}
bool bfs(int S,int T)
{
    static int Q[MAXN]; memset(dis,-1,sizeof(dis));
    dis[S]=0; Q[0]=S;
    for (int h=0,t=1,u,v,it;h<t;++h)
    {
        for (u=Q[h],it=G[u];~it;it=E[it].nx)
        {
            if (dis[v=E[it].v]==-1&&E[it].c>E[it].f)
            {
                dis[v]=dis[u]+1; Q[t++]=v;
            }
        }
    }
    return dis[T]!=-1;
}
int dfs(int u,int T,int low)
{
    if (u==T) return low;
    int ret=0,tmp,v;
    for (int &it=cur[u];~it&&ret<low;it=E[it].nx)
    {
        if (dis[v=E[it].v]==dis[u]+1&&E[it].c>E[it].f)
        {
            if (tmp=dfs(v,T,min(low-ret,E[it].c-E[it].f)))
            {
                ret+=tmp; E[it].f+=tmp; E[it^1].f-=tmp;
            }
        }
    }
    if (!ret) dis[u]=-1; return ret;
}
int dinic(int S,int T)
{
    int maxflow=0,tmp;
    while (bfs(S,T))
    {
        memcpy(cur,G,sizeof(G));
        while (tmp=dfs(S,T,inf)) maxflow+=tmp;
    }
    return maxflow;
}
int n,b,q;
pair<int,int>p[MAXN];
int main()
{
    init();
    memset(p,0,sizeof(p));
    scanf("%d%d%d",&n,&b,&q);
    for(int i=1;i<=q;i++)
        scanf("%d%d",&p[i].first,&p[i].second);
    q++;p[q].first=b,p[q].second=n;
    sort(p+1,p+1+q);
    for(int i=1;i<=q;i++)
    {
        if(p[i].second<p[i-1].second)return puts("unfair");
        if((p[i].first==p[i-1].first)&&(p[i].second!=p[i-1].second))return puts("unfair");
    }
    int S=0,T=q+7;
    for(int i=1;i<=5;i++)
        link(S,i,n/5);
    for(int i=1;i<=q;i++)
    {
        link(i+5,T,p[i].second-p[i-1].second);
        for(int j=1;j<=5;j++)
        {
            int num1 = p[i].first/5+(p[i].first%5>=j);
            int num2 = p[i-1].first/5+(p[i-1].first%5>=j);
            link(j,i+5,num1-num2);
        }
    }
    if(dinic(S,T)!=n)printf("unfair\n");
    else printf("fair\n");
}												
											Educational Codeforces Round 8 F. Bear and Fair Set 最大流的更多相关文章
- Educational Codeforces Round 40  F. Runner's Problem
		
Educational Codeforces Round 40 F. Runner's Problem 题意: 给一个$ 3 * m \(的矩阵,问从\)(2,1)$ 出发 走到 \((2,m)\) ...
 - Educational Codeforces Round 61 F 思维 + 区间dp
		
https://codeforces.com/contest/1132/problem/F 思维 + 区间dp 题意 给一个长度为n的字符串(<=500),每次选择消去字符,连续相同的字符可以同 ...
 - Educational Codeforces Round 51 F. The Shortest Statement(lca+最短路)
		
https://codeforces.com/contest/1051/problem/F 题意 给一个带权联通无向图,n个点,m条边,q个询问,询问两点之间的最短路 其中 m-n<=20,1& ...
 - Educational Codeforces Round 12 F. Four Divisors 求小于x的素数个数(待解决)
		
F. Four Divisors 题目连接: http://www.codeforces.com/contest/665/problem/F Description If an integer a i ...
 - Educational Codeforces Round 26 F. Prefix Sums 二分,组合数
		
题目链接:http://codeforces.com/contest/837/problem/F 题意:如题QAQ 解法:参考题解博客:http://www.cnblogs.com/FxxL/p/72 ...
 - Educational Codeforces Round 9 F. Magic Matrix 最小生成树
		
F. Magic Matrix 题目连接: http://www.codeforces.com/contest/632/problem/F Description You're given a mat ...
 - Educational Codeforces Round 6 F. Xors on Segments 暴力
		
F. Xors on Segments 题目连接: http://www.codeforces.com/contest/620/problem/F Description You are given ...
 - Educational Codeforces Round 7 F. The Sum of the k-th Powers 拉格朗日插值法
		
F. The Sum of the k-th Powers 题目连接: http://www.codeforces.com/contest/622/problem/F Description Ther ...
 - Educational Codeforces Round 8 C. Bear and String Distance 贪心
		
C. Bear and String Distance 题目连接: http://www.codeforces.com/contest/628/problem/C Description Limak ...
 
随机推荐
- mysql之数据库操作进阶(三)
			
环境信息 数据库:mysql-5.7.20 操作系统:Ubuntu-16.04.3 查询 条件查询 # 使用where关键字 select * from 表名 where 条件 # 比较运算符 > ...
 - 让我们来一起学习OC吧
			
在本分类中的接下来的将翻译http://rypress.com/tutorials/objective-c/index 通过每一章节的翻译,使得自己的OC基础扎实并分享给大家.
 - JDBC原生数据库连接
			
我们在开发JavaWeb项目时,常会需要连接数据库.我们以MySQL数据库为例,IDE工具为eclipse,讲述数据库连接与基本操作. 第一步,我们在Web项目的WebContent中建一个简单的前端 ...
 - 不断学习UI框架的写法
			
在web开发的过程中,我们会需要用到很多大大小小的插件,比如文本框,下拉树,下拉框等等各种各样的都需要.或许在开发的网页中会用到同一种插件来满足各种各样复杂的业务逻辑,比如简单的一个下拉树,有的地方需 ...
 - Web页面中两个listbox的option的转移
			
Html: <div><span>所选时间:</span><select id="xuanyongTimelb" style=" ...
 - AndroidStudio运行项目出现DELETE_FAILED_INTERNAL_ERROR和INSTALL_CANCELED_BY_USER
			
以上的错误为:无法将AS中的代码放到手机上 解决:File->Settings->Build,Execuion,Deployment->Instant Run然后把Enable In ...
 - POJ 1062 昂贵的聘礼 【带限制的最短路/建模】
			
年轻的探险家来到了一个印第安部落里.在那里他和酋长的女儿相爱了,于是便向酋长去求亲.酋长要他用10000个金币作为聘礼才答应把女儿嫁给他.探险家拿不出这么多金币,便请求酋长降低要求.酋长说:" ...
 - python 简单日志框架 自定义logger
			
转载请注明: 仰望高端玩家的小清新 http://www.cnblogs.com/luruiyuan/ 通常我们在构建 python 系统时,往往需要一个简单的 logging 框架.python 自 ...
 - 问题记载——keil中写for循环嵌套
			
还是上次的工程,LED灯闪烁.我今天回想一下感觉上次调试的时候还是有点问题,LED0 1和0的翻转时间很奇怪. 所以今天又打开看了看,单步调试,发现for循环嵌套只执行前一个循环,后一个循环根本不执行 ...
 - oracle中clob字段怎么查询非空列_20180517
			
select * from uap_groupsynlogvo a where a.log_msg is not null ; 附加demo的建表脚本跟业务数据. 链接:https://pan.bai ...