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;
    }
  • 相关阅读:
    matlab学习笔记第十章——曲线拟合
    matlab学习笔记第九章——变换
    matlab学习笔记第八章——积分
    matlab学习笔记第七章——常微分方程(ODE)的数值解
    matlab学习笔记第六章——基本符号演算和微分方程
    matlab学习笔记第五章——代数方程求解和其他符号工具
    javaScript中奇葩的假值
    对于javaScript闭包,立即执行函数的用法的一些理解
    Jquery——简单的视差滚动效果,兼容PC移动端
    网站前端开发与动画相关常见问题解答
  • 原文地址:https://www.cnblogs.com/chenruijiang/p/8252104.html
Copyright © 2011-2022 走看看