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. Qt exe图标

    1.首先准备一张ico照片,也可以通过http://www.ico.la/生成: 2.把ico照片拷贝到项目工程下,比如:“pic.ico” 3.在工程下,创建一个文件“myapp.rc”,用txt打 ...

  2. 《Java从入门到精通》src0-8

    public class HelloWorld { public static void main(String[] args) { System.out.println("Hello wo ...

  3. 基于visual Studio2013解决面试题之0303数组求和

     题目

  4. hdu 4706

    注意一点 空的地方打空格而不是空字符,我因为这wa了一次... #include<cstdio> #include<cstring> #include<cstdlib&g ...

  5. 【安卓】eclipse中不可错过的几个秘密、!

    1.PackageExplorer显示文件层次的默认方式是平行列出全部包,事实上也可显示成多级,并且效果比navigator好多了. PackageExplorer视图中,"右上角箭头→pa ...

  6. dialog开发

    dialog开发屏幕编程:ok_code在程序里用sy-ucomm接受 调用其他事物代码:call transaction ‘SE38’. 1:50 选择屏幕之屏幕按钮: selection-scre ...

  7. JSP中两种include的区别

    首先说明这两种都是什么: <%@ include file=”relativeURI”%> 可以叫作静态include(静态包含),是jsp指令中的一种,(JSP指令控制JSP编译器如何去 ...

  8. MySQL内存表(MEMORY)说明 | 一个PHP程序员的备忘录

    MySQL内存表(MEMORY)说明 | 一个PHP程序员的备忘录 MySQL内存表(MEMORY)说明

  9. 一场刺激的游戏——很文艺的山东省第四届ACM赛总结(菜鸟版)

               人生就像一个个节点,节点中或许有成功,失败,满足,遗憾,但是只要它是不可复制的,在日后,便是美好.                                         ...

  10. 在使用supervisord 管理tomcat时遇到的小问题

    使用 supervisord  监控管理的进程必须以 nodaemon 启动,而 tomcat 的 startup.sh 脚本是daemon方式的,假设不做改动的话,supervisord 会一直报错 ...