Codeforces 628F Bear and Fair Set
题意:
给定若干个上限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的更多相关文章
- 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 ... 
- 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 ... 
随机推荐
- 里氏替换原则中is和as分别的作用
			is 是用于检查对象是否指定类型兼容 if(empls[i] is SE){ ((SE)empls).SayHi(); } as 不用强转可以直接转换 if(empls[i] is SE){ SE s ... 
- AJPFX理解反射及反射的应用
			怎么理解反射,反射的应用 反射就是把Java类中的各种成分映射成相应的Java类. 一般情况下我们要解决某个问题,先找到相关的类,创建该类的对象,然后通过该对象调用对应的方 ... 
- Unity笔记(4)自学第四、五天
			主要是移动脚本和2个技能的脚本编写. 首先是移动的脚本: using System.Collections; using System.Collections.Generic; using Unity ... 
- 1.了解Objective-C语言
			了解Objective-C语言 ** Objective-C 语言是"消息结构"(messaging structure) 类似C++ .Java 是"函数调用" ... 
- table鼠标滑过变颜色
			table鼠标滑过变颜色 添加 table tr:hover{background-color: #eee;} 设置鼠标滑过行背景变色,重新刷新浏览器页面. 一般设置灰色,eee 
- js 代码优化
- 自动交互脚本之expect使用记录
			之前一直没怎么用这个命令,意外用了一下,还不错,那这个是干嘛的呢 我们或多或少会远程登录其他服务器,需要执行某项任务,通常需要手动接入,输入密码啊,等等 那我们如何有效的自动执行呢,expect可以解 ... 
- 用闭包方式实现点击a标签弹也索引值
			var self = document.getElementsByTagName("a"); for(var i=0;i<self.length;i++){ self[i]. ... 
- js模拟输入支付密码
			html <div class="content"> <!--<div class="title">支付宝支付密码:</di ... 
- [C#] 对List进行分组排序后输出
			Student 类: public class Student { public int ID { get; set; } public string Name { get; set; } publi ... 
