B. BerSU Ball

来源:互联网 发布:淘宝卖家app 编辑:程序博客网 时间:2024/06/05 18:41

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 mgirls 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 sequencea1, 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 sequenceb1, 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
41 4 6 255 1 5 7 9
output
3
input
41 2 3 4410 11 12 13
output
0
input
51 1 1 1 131 2 3
output
2

解题说明:此题要求男女配对时舞技相差1,求出最多的配对数,最简单的方法是分别对两组数进行排序,然后顺序遍历,每次把相差最小的数进行配对,遍历结束后自然得到最多的配对数。


#include<iostream>#include<cstdio>#include<algorithm>#include<cstring>#include<string>#include<cmath>#include<cstdlib>using namespace std;int main(){int n,m,a[1000],b[1000],dem=0;int i,j;cin>>n;for( i=0;i<n;i++){cin>>a[i];}cin>>m;for( i=0;i<m;i++){cin>>b[i];}sort(a,a+n);sort(b,b+m);for( i=0,j=0;i<n && j<m;){if(abs(a[i]-b[j])<=1){dem++;i++;j++;}else if(a[i]<b[j]){i++;}else {j++;}}cout<<dem<<endl;return 0;}


0 0
原创粉丝点击