codeforces 498B

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,第二行输入各个男生会的舞数,第三行输入女生数m,第四行输入各个女生会的舞数,舞数差距在1以内可以配对,最后输出最大配对数。
先进行排序,然后进行比较。
#include<iostream>
#include<algorithm>
using namespace std;
int main()
{
    int n, m, i, j, pairs=0;
    int male[102], female[102];
    cin >> n;
    for (i = 0;i < n;i++)
        cin >> male[i];
    cin >> m;
    for (j = 0;j < m;j++)
        cin >> female[j];
    sort(male, male+n);
    sort(female, female+m);
    n--;
    m--;
    while (n >= 0 && m >= 0)
    {
        if ((male[n] - female[m] >= 0 && male[n] - female[m] < 2) ||
            (male[n] - female[m] < 0 && male[n] - female[m] > -2))
        {
            n--;
            m--;
            pairs++;
        }
        else if (male[n] > female[m])
            n--;
        else if (female[m] > male[n])
            m--;
    }
    cout << pairs << endl;
    return 0;
}
原文地址:https://www.cnblogs.com/chenruijiang/p/8252104.html