B. BerSU Ball
time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

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.

Input

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.

Output

Print a single number — the required maximum possible number of pairs.

Sample test(s)
Input
4
1 4 6 2
5
5 1 5 7 9
Output
3
Input
4
1 2 3 4
4
10 11 12 13
Output
0
Input
5
1 1 1 1 1
3
1 2 3
Output
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的更多相关文章

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

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

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

  4. Codeforces Round #277.5 (Div. 2) ABCDF

    http://codeforces.com/contest/489 Problems     # Name     A SwapSort standard input/output 1 s, 256 ...

  5. Codeforces Round #277.5 (Div. 2)

    题目链接:http://codeforces.com/contest/489 A:SwapSort In this problem your goal is to sort an array cons ...

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

  7. Codeforces Round #277.5 (Div. 2)部分题解

    A. SwapSort time limit per test 1 second memory limit per test 256 megabytes input standard input ou ...

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

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

随机推荐

  1. 新发现的Cyberduck(映射网盘)和zsuncloud(硬件产品很新潮),群辉nas的确好用(购买链接)

    https://cyberduck.io/?l=en http://www.zsuncloud.com/ 群辉nas的确好用啊在哪里可以买到?官网 淘宝也可以自己做黑群晖 先用xpenoboot is ...

  2. OpenGL框架+QT版

    原地址:http://blog.chinaunix.net/uid-25799257-id-3498005.html 之前一直做地图的算法,没什么时间学习opengl,之前看nehe_OpenGL.c ...

  3. eclipse升级后Android使用JAR报错

    升级ADT22以后,老项目编译时后遇到 NoDefFoundClassError  这个错误,因为项目中使用了jar文件. 遇到此问题的解决步骤: 1.项目根目录下建立 libs ,并将jar文件移入 ...

  4. UVALive 2519 Radar Installation 雷达扫描 区间选点问题

    题意:在坐标轴中给出n个岛屿的坐标,以及雷达的扫描距离,要求在y=0线上放尽量少的雷达能够覆盖全部岛屿. 很明显的区间选点问题. 代码: /* * Author: illuz <iilluzen ...

  5. cocos2d-x 3.0来做一个简单的游戏教程 win32平台 vs2012 详解献给刚開始学习的人们!

    原代码来自于网络,因为cocos2d-x 3.0的资料,的确不多,与曾经版本号的接口非常难对上, 所以网上非常多样例都无法调试,对于新学习cocos2d-x 的同学,难度添加了,所以出一个超具体的样例 ...

  6. 基本HTML5文件结构

    作者 : Dolphin 原文地址:http://blog.csdn.net/qingdujun/article/details/28129039 基本HTML5文件结构: <!--<!D ...

  7. 如何在Linux上检查SSH的版本(转)

    SSH协议规范存在一些小版本的差异,但是有两个主要的大版本:SSH1 (版本号 1.XX) 和 SSH2 (版本号 2.00). 事实上,SSH1和SSH2是两个完全不同互不兼容的协议.SSH2明显地 ...

  8. ios23-文件上传

    1.上传图片 3. // //  ios23_uploadViewController.h //  ios23-upload // //  Created by  on 13-6-17. //  Co ...

  9. unity3d ngui-TweenRotation-TweenPosition-TweenScale

    using UnityEngine; using System.Collections; public class TweenFlipCARDS : MonoBehaviour { private f ...

  10. 一种H.264高清视频的无参考视频质量评价算法(基于QP和跳过宏块数)

    本文记录一种无参考视频质量评价算法.这是我们自己实验室前两年一个师姐做的,算法还是比较准确的,在此记录一下. 注意本算法前提是高清视频.而且是H.264编码方式. 该方法主要使用两个码流里面的参数进行 ...