题意:

给定若干个上限upto以及集合中在[1,upto]中的元素个数,问是否存在这样的集合使得集合中的元素除以5的余数的个数相等。

分析:

首先可以想到区间的数与其除以5的余数和区间编号分别一一对应,这样我们就可以在他们之间建立容量为1的边,而由于规定某个区间的元素个数,所以我们在源点和对应区间编号之间建立容量为元素个数的边,这样就满足题目中的限制条件。而要求余数个数相等,即均为n/5,在余数和汇点之间建立容量为n/5的边,直接用最大流求解即可~

代码:

#include<cstdio>
#include<iostream>
#include<queue>
#include<cmath>
#include<algorithm>
#include<vector>
#include<cstring>
using namespace std;
typedef pair<int, int>pii;
#define fi first
#define se second
struct edge{int to, cap, rev;};
const int maxn = 10005, maxm = 20100, INF = 0x3fffffff;
int d[maxm], iter[maxm];
int s, t;
int n, b, q;
vector<edge>G[maxm];
pii u[maxn];
void add_edge(int from, int to, int cap)
{
G[from].push_back((edge){to, cap, G[to].size()});
G[to].push_back((edge){from, 0, G[from].size()-1});
}
void bfs()
{
memset(d, -1, sizeof(d));
queue<int>q;
d[s] = 0;
q.push(s);
while(!q.empty()){
int v = q.front();q.pop();
for(int i = 0; i <G[v].size(); i++){
edge &e = G[v][i];
if(e.cap>0&&d[e.to]<0){
d[e.to] = d[v] + 1;
q.push(e.to);
}
}
}
}
int dfs(int v, int f)
{
if(v == t) return f;
for(int &i = iter[v]; i < G[v].size(); i++){
edge &e = G[v][i];
if(e.cap > 0 && d[v] < d[e.to]){
int tf = dfs(e.to, min(f, e.cap));
if(tf > 0){
e.cap -= tf;
G[e.to][e.rev].cap +=tf;
return tf;
}
}
}
return 0;
}
int max_flow()
{
int flow = 0;
for(;;){
bfs();
if(d[t]<0) return flow;
memset(iter, 0, sizeof(iter));
int f;
while((f = dfs(s, INF))>0){
flow += f;
}
}
}
int solve()
{
for(int i = 0; i <= q; i++){
int tmp = u[i].se - u[i - 1].se;
if(tmp < 0) return 0;
if(tmp > u[i].fi - u[i-1].fi) return 0;
add_edge(s, i, tmp);
for(int j = u[i - 1].fi + 1; j <= u[i].fi; j++){
add_edge(i, q + j, 1);
add_edge(q + j, j % 5 + b + q + 1, 1);
}
}
for(int i = 0; i < 5; i++){
add_edge(i + b + q + 1, t, n / 5); }
return max_flow() == n;
}
int main (void)
{
scanf("%d%d%d",&n, &b, &q);
int upto, num;
for (int i = 0; i < q; i++){
scanf("%d%d", &upto, &num);
u[i] = pii(upto,num);
}
sort(u, u + q);
u[q] = pii(b, n);
s = q + b + 6, t = s + 1;
if(solve()) printf("fair\n");
else printf("unfair\n");
return 0;
}

Codeforces 628F Bear and Fair Set的更多相关文章

  1. 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 ...

  2. 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 ...

  3. 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 ...

  4. Codeforces 385C Bear and Prime Numbers

    题目链接:Codeforces 385C Bear and Prime Numbers 这题告诉我仅仅有询问没有更新通常是不用线段树的.或者说还有比线段树更简单的方法. 用一个sum数组记录前n项和, ...

  5. Codeforces 385B Bear and Strings

    题目链接:Codeforces 385B Bear and Strings 记录下每一个bear的起始位置和终止位置,然后扫一遍记录下来的结构体数组,过程中用一个变量记录上一个扫过的位置,用来去重. ...

  6. Codeforces 680D Bear and Tower of Cubes 贪心 DFS

    链接 Codeforces 680D Bear and Tower of Cubes 题意 求一个不超过 \(m\) 的最大体积 \(X\), 每次选一个最大的 \(x\) 使得 \(x^3\) 不超 ...

  7. Codeforces 385C Bear and Prime Numbers(素数预处理)

    Codeforces 385C Bear and Prime Numbers 其实不是多值得记录的一道题,通过快速打素数表,再做前缀和的预处理,使查询的复杂度变为O(1). 但是,我在统计数组中元素出 ...

  8. [Codeforces 639F] Bear and Chemistry (Tarjan+虚树)(有详细注释)

    [Codeforces 639F] Bear and Chemistry(Tarjan+虚树) 题面 给出一个n个点,m条边的无向图(不保证连通,可能有自环和重边),有q次询问,每次询问给出p个点和q ...

  9. 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 ...

随机推荐

  1. mysql之通过cmd连接远程数据库

    ---恢复内容开始--- 目录 前提 连接远程数据库 前提: 本地安装了mysql数据库 本地和远程网络是连通的,通过命令ping ip (即ping 192.168.0.333),可以ping通 连 ...

  2. 2017广东工业大学程序设计竞赛决赛 H tmk买礼物

    题意: Description 今天是校赛的日子,为了庆祝这么喜庆的日子,TMK打算买些礼物给女票LSH庆祝一下. TMK进入了雪梨超市,然后刚踏入的一瞬间,店主就对TMK说:“恭喜你成为了本店第21 ...

  3. git ---查看工作状态和历史提交

    1.git查看状态 -git status 2.版权声明 版权声明:新建一个   LICENSE.txt   文件 开源协议:MIT   //开源许可里面的最宽松的一个协议,别人可以随便用你的代码,但 ...

  4. 重构31-Replace conditional with Polymorphism(多态代替条件)

    多态(Polymorphism)是面向对象编程的基本概念之一.在这里,是指在进行类型检查和执行某些类型操作时,最好将算法封装在类中,并且使用多态来对代码中的调用进行抽象. public class O ...

  5. yii项目开发配置

    Clone项目 git clone https://gitee.com/s***/dianshang.git 安装yii php ini 选择 [0] Development 安装扩展 copy co ...

  6. Youtube-dl 简短使用总结

    默认下载bestvideo+bestaudio,并通过ffmpeg -c copy output.mp4 简单的封装进mp4格式,而不进行转码. 有时候bestaudio 是opus编码的,但是mp4 ...

  7. CentOS7 Install Shipyard

    # 采集木jj 原文:http://www.cnblogs.com/caoguo/p/5735189.html # CentOS7 Install Shipyard# yum install dock ...

  8. ALTER FUNCTION - 修改一个函数的定义

    SYNOPSIS ALTER FUNCTION name ( [ type [, ...] ] ) RENAME TO newname DESCRIPTION 描述 ALTER FUNCTION 修改 ...

  9. tomcat修改编码格式

    在TOMCAT中的conf文件夹下server.xml中的 <Connector中添加两个设置useBodyEncodingForURI="true" //设置POST和GE ...

  10. ubuntu 18.04 安装.net core

    要求某些软件包保持现状,就是它们破坏了软件包间的依赖关系那就只好在琢磨了,然后就发现了在github有安装的方法因为是18.04 所以 wget -qO- https://packages.micro ...