Codeforces Round #277.5 (Div. 2)B——BerSU Ball

时间:2022-01-27 07:02:54
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 &num;277&period;5 &lpar;Div&period; 2&rpar;-B&period; BerSU Ball

    http://codeforces.com/problemset/problem/489/B B. BerSU Ball time limit per test 1 second memory lim ...

  2. Codeforces Round &num;277&period;5 &lpar;Div&period; 2&rpar;---B&period; BerSU Ball (贪心)

    BerSU Ball time limit per test 1 second memory limit per test 256 megabytes input standard input out ...

  3. Codeforces Round &num;277&period;5 &lpar;Div&period; 2&rpar; B&period; BerSU Ball【贪心&sol;双指针&sol;每两个跳舞的人可以配对,并且他们两个的绝对值只差小于等于1,求最多匹配多少对】

    B. BerSU Ball time limit per test 1 second memory limit per test 256 megabytes input standard input ...

  4. Codeforces Round &num;277&period;5 &lpar;Div&period; 2&rpar; ABCDF

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

  5. Codeforces Round &num;277&period;5 &lpar;Div&period; 2&rpar;

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

  6. Codeforces Round &num;277&period;5 &lpar;Div&period; 2&rpar; A&comma;B&comma;C&comma;D&comma;E&comma;F题解

    转载请注明出处: http://www.cnblogs.com/fraud/          ——by fraud A. SwapSort time limit per test    1 seco ...

  7. Codeforces Round &num;277&period;5 &lpar;Div&period; 2&rpar;部分题解

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

  8. Codeforces Round &num;277&period;5 &lpar;Div&period; 2&rpar; --E&period; Hiking &lpar;01分数规划&rpar;

    http://codeforces.com/contest/489/problem/E E. Hiking time limit per test 1 second memory limit per ...

  9. Codeforces Round &num;277&period;5 &lpar;Div&period; 2&rpar;-D&period; Unbearable Controversy of Being

    http://codeforces.com/problemset/problem/489/D D. Unbearable Controversy of Being time limit per tes ...

随机推荐

  1. 简单的css 菜单

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  2. UIPickerView用法&lpar;左右比例,整体大小,字体大小&rpar;

    UIPickerView *pickerView = [[UIPickerView alloc] initWithFrame:CGRectZero]; pickerView.autoresizingM ...

  3. Preference如何增加在activity生命周期监听器

    转载请注明出处:http://blog.csdn.net/droyon/article/details/41313115 本文主要介绍Preference凭什么Activit一些逻辑的生命周期,使. ...

  4. html5 读写sqlite数据库

    var db = openDatabase('MyData','','My Database',102400); //首先它创建一个数据库表,里面有3个字段 db.transaction(functi ...

  5. Arch Linux之pacman调用axel多线程加速下载

    转载自 奶牛博客 本来感觉Arch Linux用个国内的源就很给力了,可是到了学校移动的cmcc-edu超级不稳定,而且单线程速度就二三十k,无奈,开多线程下载.在Ubuntu下面可以用apt-fas ...

  6. Angular 6的新特性介绍

    2018年5月4日,Angular6.0.0版正式发布,新版本主要关注底层框架和工具链,目的在于使其变得更小更快.下面就介绍下新版本的一些主要新特性,供大家参考. ng update ng updat ...

  7. Logback简单使用

    1.     添加jar包/maven配置 <dependency> <groupId>ch.qos.logback</groupId> <artifactI ...

  8. Nancy 返回值详解

    简介 Nancy 是一个轻量级的,简单粗暴的framework用来构建基于HTTP的各种服务,兼容.Net和Mono.它的返回值也是多种多样的,适应各种不同的情况.包括Response.AsFile( ...

  9. SpringBoot整合Jdbc

    (1).添加相关依赖 <dependency> <groupId>org.springframework.boot</groupId> <artifactId ...

  10. redhat7&period;5在H3C机器上黑屏无显

    现象:H3C机器上,PXE安装/ISO安装系统,多用户模式启动,过内核启动界面后,屏幕黑屏无显,但是可以通过SSH登陆系统,服务正常 环境:redhat7.5/H3C R4900G3/Purely平台 ...