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 ...
随机推荐
- JAVA高级特性反射和注解
反射: 枚举反射泛型注解.html34.3 KB 反射, 主要是指通过类加载, 动态的访问, 检测和修改类本身状态或行为的一种能力, 并能根据自身行为的状态和结果, 调整或修改应用所描述行为的状态和相 ...
- 2017.5.20欢(bei)乐(ju)赛解题报告
预计分数:100+20+50=first 实际分数:20+0+10=gg 水灾(sliker.cpp/c/pas) 1000MS 64MB 大雨应经下了几天雨,却还是没有停的样子.土豪CCY刚从外地 ...
- 简洁大方的wordpress主题,不容错过的主题,附带主题源码下载
cu主题是由疯狂的大叔设计,界面简洁大方是它最大的特点之一. 手残君也比较喜爱这款主题,在使用的过程中,根据手残君的个人习惯,对其进行了优化. 标题优化 标题居中显示 增加标题div背景色 标题div ...
- 用unsigned char 表示字节
在C中,默认的基础数据类型均为signed,现在我们以char为例,说明(signed) char与unsigned char之间的区别 首先在内存中,char与unsigned char没有什么不同 ...
- Xcode 9 打印信息解决
Xcode 9 打印信息解决 打印信息 1 nw_proxy_resolver_create_parsed_array PAC evaluation error: kCFErrorDomainCFNe ...
- C++学习_继承覆盖重载
今天通过对实验二继承,重载,覆盖的学习,让我更深一步理解了这些概念的区别. 首先来明确一个概念,函数名即地址,也就是说函数名就是个指针. 编译阶段,编译器为每个函数的代码分配一个地址空间并编译函数代码 ...
- Bootstrap modal使用及点击外部不消失的解决方法
这篇文章主要为大家详细介绍了Bootstrap modal使用及点击外部不消失的解决方法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下 本文实例为大家分享了Bootstrap modal使用及点击 ...
- Flex 布局 (两个div居中自适应 宽度变小变一列,宽度够就是两列)
https://www.runoob.com/w3cnote/flex-grammar.html display: flex; justify-content: center; align-items ...
- java将很长的一条sql语句,自动换行输出(修改版)2019-06-01(bug未修复)
package org.jimmy.autosearch2019.test; import java.util.HashMap; public class AutoLinefeedSql { publ ...
- yum升级php版本
查看当前 PHP 版本 1 php -v 查看当前 PHP 相关的安装包,删除之 1 2 3 4 5 yum list installed | grep php yum remove php ...