Codeforces Round #277.5 (Div. 2)B——BerSU Ball
1 second
256 megabytes
standard input
standard output
The Berland State University is hosting a ballroom dance in celebration of its 100500-th anniversary!
n boys and m girls are already busy rehearsing waltz, minuet, polonaise and quadrille moves.
We know that several boy&girl pairs are going to be invited to the ball. However, the partners' dancing skill in each pair must differ by at most one.
For each boy, we know his dancing skills. Similarly, for each girl we know her dancing skills. Write a code that can determine the largest possible number of pairs that can be formed from
n boys and m girls.
The first line contains an integer n (1 ≤ n ≤ 100) — the number of boys. The second line contains sequence
a1, a2, ..., an (1 ≤ ai ≤ 100),
where ai is the
i-th boy's dancing skill.
Similarly, the third line contains an integer m (1 ≤ m ≤ 100) — the number of girls. The fourth line contains sequence
b1, b2, ..., bm (1 ≤ bj ≤ 100),
where bj is the
j-th girl's dancing skill.
Print a single number — the required maximum possible number of pairs.
4
1 4 6 2
5
5 1 5 7 9
3
4
1 2 3 4
4
10 11 12 13
0
5
1 1 1 1 1
3
1 2 3
2
二分匹配模板题
#include <map>
#include <set>
#include <list>
#include <queue>
#include <stack>
#include <vector>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm> using namespace std; const int N = 110; int mark[N];
bool vis[N];
int head[N];
int tot;
int n, m;
int b[N];
int g[N]; struct node
{
int next;
int to;
}edge[N * N]; void addedge(int from, int to)
{
edge[tot].to = to;
edge[tot].next = head[from];
head[from] = tot++;
} bool dfs(int u)
{
for (int i = head[u]; ~i; i = edge[i].next)
{
int v = edge[i].to;
if (!vis[v])
{
vis[v] = 1;
if (mark[v] == -1 || dfs(mark[v]))
{
mark[v] = u;
return 1;
}
}
}
return 0;
} int hungry()
{
memset(mark, -1, sizeof(mark));
int ans = 0;
for (int i = 1; i <= n; ++i)
{
memset(vis, 0, sizeof(vis));
if (dfs(i))
{
ans++;
}
}
return ans;
} int main()
{
while (~scanf("%d", &n))
{
for (int i = 1; i <= n; ++i)
{
scanf("%d", &b[i]);
}
scanf("%d", &m);
for (int i = 1; i <= m; ++i)
{
scanf("%d", &g[i]);
}
memset (head, -1, sizeof(head));
tot = 0;
for (int i = 1; i <= n; ++i)
{
for (int j = 1; j <= m; ++j)
{
if(abs(b[i] - g[j]) <= 1)
{
addedge(i, j);
}
}
}
printf("%d\n", hungry());
}
return 0;
}
版权声明:本文博主原创文章。博客,未经同意不得转载。
Codeforces Round #277.5 (Div. 2)B——BerSU Ball的更多相关文章
- Codeforces Round #277.5 (Div. 2)-B. BerSU Ball
http://codeforces.com/problemset/problem/489/B B. BerSU Ball time limit per test 1 second memory lim ...
- Codeforces Round #277.5 (Div. 2)---B. BerSU Ball (贪心)
BerSU Ball time limit per test 1 second memory limit per test 256 megabytes input standard input out ...
- Codeforces Round #277.5 (Div. 2) B. BerSU Ball【贪心/双指针/每两个跳舞的人可以配对,并且他们两个的绝对值只差小于等于1,求最多匹配多少对】
B. BerSU Ball time limit per test 1 second memory limit per test 256 megabytes input standard input ...
- Codeforces Round #277.5 (Div. 2) ABCDF
http://codeforces.com/contest/489 Problems # Name A SwapSort standard input/output 1 s, 256 ...
- Codeforces Round #277.5 (Div. 2)
题目链接:http://codeforces.com/contest/489 A:SwapSort In this problem your goal is to sort an array cons ...
- Codeforces Round #277.5 (Div. 2) A,B,C,D,E,F题解
转载请注明出处: http://www.cnblogs.com/fraud/ ——by fraud A. SwapSort time limit per test 1 seco ...
- Codeforces Round #277.5 (Div. 2)部分题解
A. SwapSort time limit per test 1 second memory limit per test 256 megabytes input standard input ou ...
- Codeforces Round #277.5 (Div. 2) --E. Hiking (01分数规划)
http://codeforces.com/contest/489/problem/E E. Hiking time limit per test 1 second memory limit per ...
- Codeforces Round #277.5 (Div. 2)-D. Unbearable Controversy of Being
http://codeforces.com/problemset/problem/489/D D. Unbearable Controversy of Being time limit per tes ...
随机推荐
- mysql 密码过期问题 password_expired
mysql> select user,host,password_expired from mysql.user; +-----------------+----------------+--- ...
- 【Cocos2d-X开发学习笔记】第28期:游戏中音乐和音效的使用
本系列学习教程使用的是cocos2d-x-2.1.4(最新版为3.0alpha0-pre) ,PC开发环境Windows7,C++开发环境VS2010 UI在游戏中占有很重要的地位,但吸引玩家的除了这 ...
- oradebug推进scn
有时候我们遇到例如以下错误: ORA-01092: ORACLE instance terminated. Disconnection forced ORA-00600: internal error ...
- ASP.NET - 禁用ViewState
默认情况下,ViewState是被启用的,比如提交表单后,表单中输入的值会自动保留.但是如果不需要保留,也可以将其禁用,这样可以节省资源. 下面3种方式就可以分别禁用某一个控件.某一个页面和整个应 ...
- TEXT文本编辑框3 点击按钮添加文本至文本输入框
In this exercise a function that loads the texts of an internal table into the text window, is imple ...
- lastIndexOf方法——获取字符最后的索引
1.2.20 lastIndexOf方法——获取字符最后的索引 2013-08-29 14:55:18 我来说两句 收藏 我要投稿 本文所属图书 > Java程序开发参考手册 ...
- poj 2184 Cow Exhibition(背包变形)
这道题目和抢银行那个题目有点儿像,同样涉及到包和物品的转换. 我们将奶牛的两种属性中的一种当作价值,另一种当作花费.把总的价值当作包.然后对于每一头奶牛进行一次01背包的筛选操作就行了. 需要特别注意 ...
- Delphi颜色的表示(一共5种表示法)
//全以红色举例: //1. RGB 模式:Self.Color := $0000ff; //不过和HTML.PhotoShop.FireWorks中的 #ff0000 是完全反的,应该叫 BGR. ...
- 获取ocx运行路径的另一种方法
在InitInstance里边可以获取 1 2 3 4 5 6 7 8 9 10 11 12 if (bInit) { // TODO: 在此添加您自己的模块初始化 ...
- Cocos2d-x CCProgressTimer
CCProgressTimer,创建使用这个节点可以大致实现两个作用的效果: 其一:在游戏中几乎大部分的游戏启动界面都是游戏加载画面,那么用到的一般是进度条提示加载进度,其使用的就是CCProgres ...