BerSU Ball

题目链接:

http://acm.hust.edu.cn/vjudge/contest/121332#problem/E

Description

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 Input

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

题意:

n个男孩,m个女孩;每人各有一个权值;

求最多能组成多少对男女,使得两人权值差不超过1.

题解:

排序后扫描一遍后就可以了,本质是贪心.

代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <queue>
#include <map>
#include <set>
#include <vector>
#define LL long long
#define eps 1e-8
#define maxn 150
#define inf 0x3f3f3f3f
#define IN freopen("in.txt","r",stdin);
using namespace std; int n,m;
int a[maxn], b[maxn]; int main(int argc, char const *argv[])
{
//IN; while(scanf("%d", &n) != EOF)
{
for(int i=1; i<=n; i++)
scanf("%d", &a[i]);
cin >> m;
for(int i=1; i<=m; i++)
scanf("%d", &b[i]);
sort(a+1,a+1+n);
sort(b+1,b+1+m); int i=1, j=1;
int ans = 0;
while(i<=n && j<=m) {
if(abs(a[i]-b[j])<=1) {
ans++;
i++; j++;
continue;
}
if(a[i] > b[j]) j++;
else if(a[i] < b[j]) i++;
} printf("%d\n", ans);
} return 0;
}

CodeForces 489B BerSU Ball (贪心)的更多相关文章

  1. CodeForces 489B BerSU Ball (水题 双指针)

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

  2. codeforces 489B. BerSU Ball 解题报告

    题目链接:http://codeforces.com/problemset/problem/489/B 题目意思:给出 n 个 boys 的 skills 和 m 个 girls 的 skills,要 ...

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

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

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

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

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

  7. codeforces 704B - Ant Man 贪心

    codeforces 704B - Ant Man 贪心 题意:n个点,每个点有5个值,每次从一个点跳到另一个点,向左跳:abs(b.x-a.x)+a.ll+b.rr 向右跳:abs(b.x-a.x) ...

  8. CodeForces - 50A Domino piling (贪心+递归)

    CodeForces - 50A Domino piling (贪心+递归) 题意分析 奇数*偶数=偶数,如果两个都为奇数,最小的奇数-1递归求解,知道两个数都为1,返回0. 代码 #include ...

  9. CodeForces 489B (贪心 或 最大匹配) BerSU Ball

    题意: 有m个男孩和n个女孩,每个人都有一个舞蹈熟练度,用一个不超过100的正整数来表示. 一个男孩和一个女孩能够结为舞伴当且仅当两人的熟练度相差不超过1. 问最多能结成多少对舞伴 分析: 这是一个二 ...

随机推荐

  1. S5P4418iNand清空方法

    01连接串口线 02打开Fastboot的Xshell串口插件 03启动开发板 04一启动就输入回车键 05在uboot处停住,输入:mmc erase blk 10000 (mmc erase bo ...

  2. 【POJ】3134 Power Calculus

    1. 题目描述给定一个正整数$n$,求经过多少次乘法或除法运算可以从$x$得到$x^n$?中间结果也是可以复用的. 2. 基本思路实际结果其实非常小,肯定不会超过20.因此,可以采用IDA*算法.注意 ...

  3. SQLiteParameter不能将TableName作为参数

    http://stackoverflow.com/questions/1274432/sqlite-parameters-not-allowing-tablename-as-parameter Gen ...

  4. [原创]Android中LocationManager的简单使用,获取当前位置

    Android中LocationManager的提供了一系列方法来地理位置相关的问题,包括查询上一个已知位置:注册/注销来自某个 LocationProvider的周期性的位置更新:以及注册/注销接近 ...

  5. 基于XMPP的即时通信系统的建立(四)— 协议详解

    Presence 在XMPP协议中,我们使用presence来获取用户是否已经上线以及是否可以通信的状态. 为了能够知道自己联系人的状态以及让联系人知道自己的状态,用户上线后需要订阅联系人的状态,联系 ...

  6. Android ArrayAdapter 详解

    本文主要讲解ArrayAdapter的创建方法,我把ArrayAdapter分为三种:简单的.样式丰富的但内容简单的.内容丰富的. 默认的,ArrayAdapter期望接受的样式文件里只含有一个tex ...

  7. Android的图片压缩并上传

    Android开发中上传图片很常见,一般为了节省流量会进行压缩的操作,本篇记录一下压缩和上传的方法. 图片压缩的方法 : import java.io.ByteArrayOutputStream; i ...

  8. 如何使用 orachk 工具

    Oracle RAC 安装完毕后的健壮性是一个令人头疼的问题.之前Oracle为之专门推出了raccheck工具,确实方便了我们这些个苦逼的DBA.现在Oracle在raccheck的基础之上又推出了 ...

  9. delphi 如何关闭 Unsafe typecast of 和 Unsafe type 的waring

    有时在Delphi使用指针类型的数据,总是提示如下: [Warning] FGroupFeedBack.pas(796): Unsafe typecast of 'Pointer' to 'TObje ...

  10. c# List<int> 转 string 以及 string [] 转 List<int>

    List<int> 转 string : list<int>: 1,2,3,4,5,6,7  转换成字符串:“1,2,3,4,5,6,7” List<int> li ...