B. BerSU Ball

B. BerSU Ball
B.小松舞会

time limit per test: 1 second
每次测试的时间限制:1秒

memory limit per test: 256 megabytes
每次测试的内存限制:256兆字节

input. standard input
投入。标准输入

output: standard output
产出:标准产出

The Berland State University is hosting a ballroom dance in celebration of its 100500-th
伯兰州立大学正在举办一场舞厅舞会,以庆祝它的第100500届。

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 inited 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 kills.
每个男孩,我们都知道他的舞蹈技巧。同样,我们认识的每一个女孩都会因为跳舞而丧命。

Write a code that can determine the largest possible number of pairs that can be formed from n
编写一段代码,该代码可以确定可由n生成的尽可能多的对。

boys and m girls.
男孩和女孩。

Input
输入

The first line contains an integern(1≤n≤100)- the number of boys. The second line
第一行包含一个整数(1≤n≤100)–男孩的数量。第二行

contains sequence a1, a2, … an (1≤ai≤100), where ai is the i-th boy’s dancing skill.
包含A1,a2,.序列。An(1≤ai≤100),其中艾是第一个男孩的舞蹈技巧.

Similarly, the third line contains an integer m(1≤m≤100)- - the number of girls. The fourth
同样,第三行包含整数m(1≤m≤100)–女孩的数量。第四

line contains sequence b1, b2, … bm (1≤b;≤100), where bj is the j-th girl’s dancing skill.
行包含b1,b2,.序列。BM(1≤b;≤100),其中bj是第j女孩的舞蹈技巧.

Output
输出量

Print a single number - the required maximum possible number of pairs.
打印一个数字-所需的最大可能的配对数。

双指针


#include<iostream>
#include<algorithm>
using namespace std;
typedef long long LL;
LL a[100007];
LL b[100007];
int main(void)
{
    LL n,m;cin>>n;
    for(LL i=1;i<=n;++i)
        cin>>a[i];
    cin>>m;
    for(LL j=1;j<=m;++j)
        cin>>b[j];
    sort(a+1,a+1+n);sort(b+1,b+1+m);
    LL res=0;
    for(LL i=1,j=1;i<=n&&j<=m;)
    {
        if(abs(a[i]-b[j])<=1)
        {
            ++res;++i;++j;
        }
        else if(a[i]>b[j])
        {
            ++j;
        }
        else ++i;
    }
    cout<<res<<endl;
    return 0;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值