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;
    }
  • 相关阅读:
    Linux内核参数信息(Oracle相关)
    Android和java平台 DES加密解密互通程序及其不能互通的原因
    [置顶] ※数据结构※→☆线性表结构(queue)☆============循环队列 顺序存储结构(queue circular sequence)(十)
    理解 Thread.Sleep 函数
    引导加载程序:GRUB
    xvfb 初步探究
    [置顶] 中国象棋程序的设计与实现(原始版)(包含源码)
    Ext图表的精彩
    JSTL解析——004——core标签库03
    Servlet API中文版
  • 原文地址:https://www.cnblogs.com/chenruijiang/p/8252104.html
Copyright © 2011-2022 走看看