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;
    }
  • 相关阅读:
    关于迭代器模式的个人理解
    设计模式之迭代器模式详解(foreach的精髓)
    关于备忘录模式的个人理解
    设计模式之备忘录模式详解(都市异能版)
    关于桥接模式的个人理解
    桥接模式详解(都市异能版)
    设计模式之 中介者模式
    关于命令模式个人的理解 撤销 重做的实现
    剑指offer 连续子数组的最大和
    剑指offer 删除链表中重复的结点
  • 原文地址:https://www.cnblogs.com/chenruijiang/p/8252104.html
Copyright © 2011-2022 走看看