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.

Examples
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个boy,m个girl,每个人都有自己的舞蹈技术等级,现规定只有boy和girl的等级相差不大于1才能构成一对舞伴,在每个人都不重复的情况下,问最多能构成多少对?
【分析】:贪心。把两个数组都从小到大排序,再依次用当前最小的去跟对方比,若符合条件,则双方下标都++;若自己太低,则自己下标++,否则对方下标++。
【代码】:
#include<bits/stdc++.h>
using namespace std;
int n,m,cnt;
int a[],b[];
int main()
{
cin>>n;
for(int i=;i<n;i++) cin>>a[i]; sort(a,a+n);
cin>>m;
for(int i=;i<m;i++) cin>>b[i]; sort(b,b+m); for(int i=,j=;i<n&&j<m;)
{
if(abs(a[i]-b[j])<=)
{
i++;
j++;
cnt++;
}
else if(a[i]>b[j])
{
j++;
}
else
{
i++;
}
}
cout<<cnt<<endl;
return ;
}

Codeforces Round #277.5 (Div. 2) B. BerSU Ball【贪心/双指针/每两个跳舞的人可以配对,并且他们两个的绝对值只差小于等于1,求最多匹配多少对】的更多相关文章

  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

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

  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) 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. 风格指南--C++

    0.避免多重包含是学编程时基本的要求; 1. 前置声明是为了降低编译依赖,防止修改一个头文件引发多米诺效应; 2. 内联函数的合理使用可提高代码执行效率; 3. ‐inl.h 可提高代码可读性 (一般 ...

  2. Centos7中查看IP地址命令ifconfig无法识别如何处理

    问题描述: 在虚拟机中已安装好Centos7系统,查看IP地址使用命令ifconfig时,提示找不到此命令,使用ip addr命令则可查询当前系统的IP地址(如图1.2): 图1 图2 解决问题步骤: ...

  3. operator、explicit与implicit

    说这个之前先说下什么叫隐式转换和显示转换 1.所谓隐式转换,就是系统默认的转换,其本质是小存储容量数据类型自动转换为大存储容量数据类型. 例如:float f = 1.0: double d=f:这样 ...

  4. Python全栈工程师(列表、拷贝)

    ParisGabriel     感谢 大家的支持  你们的阅读评价就是我最好的更新动力  我会坚持吧排版做的越来越好      每天坚持 一天一篇 点个订阅吧  灰常感谢    当个死粉也阔以 Py ...

  5. 系统编程--高级IO

    1.非阻塞I/O 非阻塞I/O使我们可以调用不会永远阻塞的I/O操作,例如open,read和write.如果这种操作不能完成,则立即出错返回,表示该操作如继续执行将继续阻塞下去.对于一个给定的描述符 ...

  6. 安卓context(一)

    最近看了一下深入安卓内核,毫无安卓基础的我一头深入不可自拔,看的是云里雾里,第一遍看到一半左右似乎开始失去了效率. 现在开始第二遍,并对看过的重要知识点以个人的理解进行梳理(不免有错的地方,好心人请告 ...

  7. MVC4.0 JSON JsonResult 序列化之后 对JSON 进行修改 EXTJS4.0 分页

    事情是这样的:我在MVC 下 前后台交互 用JsonResult 返回给前台使用. public JsonResult AjaxFindHospitalInfo() { List<T> l ...

  8. Java 命令行编译项目

    如果是用Exlipse, 第三方的包可以放在eclipse文件夹的jre包的lib文件夹中! (初学者的一些总结-高手们勿喷哈-) 原因: 以前一直用Eclispe编程环境运行Java.非常舒服,就像 ...

  9. 少年Pi的奇幻漂流

    选择怀疑作为生活哲学就像选择静止作为交通方式.   的确,我们遇见的人可能改变我们,有时候改变如此深刻,在那之后我们成了完全不同的人,甚至我们的名字都不一样了. 声音会消失,但伤害却留了下来,像小便蒸 ...

  10. HDU 4585 Shaolin(Treap找前驱和后继)

    Shaolin Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others) Total Su ...