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 ...
随机推荐
- 工厂方法模式及php实现
工厂方法模式: 工厂方法模式(Factory Method Pattern)又称为工厂模式,也叫虚拟构造器(Virtual Constructor)模式或者多态工厂(Polymorphic Facto ...
- Angular 组件之间的传值
第一种方法(传单个或者多个参数): 主页面方法: 先添加引用:private _routes: Router, Details(PBSCode) { this._routes.navigate(['p ...
- Thinkphp3.23 连接MSSQL方法
Thinkphp 3.23要连接MSSQL,必须配置下,以下是主要的步骤. 1.要安装Microsoft Drivers for PHP for SQL Server驱动 下载驱动以前,要查看一下ph ...
- VS2012创建WebForm项目提示错误: 此模板尝试加载组件程序集 “NuGet.VisualStudio.Interop, Version=1.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a”。
解决方案: 使用VS2012开发,都要装NuGet插件(官网:http://nuget.org),进官网点安装就进入了微软的下载页面, 选择vs2012版本的NuGet.Tools.vsix文件,双击 ...
- 10.3 Implementing pointers and objects and 10.4 Representing rooted trees
Algorithms 10.3 Implementing pointers and objects and 10.4 Representing rooted trees Allocating an ...
- ios开发介绍
iOS开发概述 •什么是IOS •什么是IOS开发 •为什么要选择IOS开发 •学习IOS开发的准备 1.什么是iOS •iOS是一款由苹果公司开发的操作系统(OS是Operating Sys ...
- InChatter系统之服务器开发(一)
服务器端是整个消息系统的中枢,类似与人类的大脑.没有他,根本无法实现客户端之间的交流,为什么呢?这也涉及到我们的系统涉及,在服务器端,每个客户端的标识数据都会在服务器端进行保存,在这种情况下,当某一个 ...
- JS获取服务器端控件ID
很多时候我们需要在JS中对服务器端控件进行一些简单处理,但是这个时候没有必要回发到服务器,让服务器去处理,这个时候就又要用到JS了 那么怎么去获取这个服务器端控件呢?我们知道服务器最终返回到用户界面的 ...
- -webkit/IE/Firefox的一些样式
仅限于-webkit的样式特效:-webkit-overflow-scrolling:touch;滚动时回弹效果:如果出现偶尔卡住不动的情况,那么在使用该属性的元素上不设置定位或者手动设置定位为sta ...
- 迅为IMX6Q四核核心板商业级|工业级|IMX6Plus版本|IMX6DL双核核心板
IMX6Q处理器:兼容单核,双核,工业级,汽车级,IMX6Q最新Plus版本,共用同一底板,高端产品无忧. i.MX6系列针对消费电子.工业控制和汽车应用领域,它将ARM Cortex-A9架构的高功 ...