zoukankan      html  css  js  c++  java
  • 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;
    }
  • 相关阅读:
    C++小知识点
    C++类的const成员函数、默认的构造函数、复制形参调用函数
    原码、反码、补码,计算机中负数的表示
    linux学习笔记(一)
    涉及string、char *、char[ ]、逗号表达式 的代码
    软件皮肤 IrisSkin
    DataGridView 闪烁
    C# String StringBuider 解惑
    JTA事务总结
    世界真的存在吗
  • 原文地址:https://www.cnblogs.com/chenruijiang/p/8252104.html
Copyright © 2011-2022 走看看