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.

Sample test(s)
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

水题
双指针模拟一下就行了,用i和j分别指向两个数组,如果a[i]和b[j]相差小于等于1的话,就ans++ i++ j++,否则就较小那边向前移动一下
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
const int INF = 1e9;
const double eps = 1e-;
const int N = ;
int cas = ; int a[N],b[N];
int n,m; bool ok(int x,int y)
{
return x-y>=- && x-y<=;
} void run()
{
for(int i=;i<n;i++) scanf("%d",a+i);
scanf("%d",&m);
for(int i=;i<m;i++) scanf("%d",b+i);
sort(a,a+n);
sort(b,b+m);
int ans = ;
for(int i=,j=;i<n&&j<m;)
{
// cout<<a[i]<<' '<<b[j]<<'\t';
if(ok(a[i],b[j]))
ans++,i++,j++;
else if(a[i]<b[j])
i++;
else
j++;
// cout<<i<<' '<<j<<endl;
}
cout<<ans<<endl;
} int main()
{
#ifdef LOCAL
freopen("case.txt","r",stdin);
#endif
while(scanf("%d",&n)!=EOF)
run();
return ;
}

CodeForces 489B BerSU Ball (水题 双指针)的更多相关文章

  1. CodeForces 489B BerSU Ball (贪心)

    BerSU Ball 题目链接: http://acm.hust.edu.cn/vjudge/contest/121332#problem/E Description The Berland Stat ...

  2. codeforces 489B. BerSU Ball 解题报告

    题目链接:http://codeforces.com/problemset/problem/489/B 题目意思:给出 n 个 boys 的 skills 和 m 个 girls 的 skills,要 ...

  3. Codeforces Round #277.5 (Div. 2) B. BerSU Ball【贪心/双指针/每两个跳舞的人可以配对,并且他们两个的绝对值只差小于等于1,求最多匹配多少对】

    B. BerSU Ball time limit per test 1 second memory limit per test 256 megabytes input standard input ...

  4. Codeforces Gym 100531G Grave 水题

    Problem G. Grave 题目连接: http://codeforces.com/gym/100531/attachments Description Gerard develops a Ha ...

  5. codeforces 706A A. Beru-taxi(水题)

    题目链接: A. Beru-taxi 题意: 问那个taxi到他的时间最短,水题; AC代码: #include <iostream> #include <cstdio> #i ...

  6. codeforces 569B B. Inventory(水题)

    题目链接: B. Inventory time limit per test 1 second memory limit per test 256 megabytes input standard i ...

  7. Codeforces 489A SwapSort (水题)

    A. SwapSort time limit per test 1 second memory limit per test 256 megabytes input standard input ou ...

  8. codeforces 688A A. Opponents(水题)

    题目链接: A. Opponents time limit per test 1 second memory limit per test 256 megabytes input standard i ...

  9. CodeForces 534B Covered Path (水题)

    题意:给定两个速度,一个一初速度,一个末速度,然后给定 t 秒时间,还每秒速度最多变化多少,让你求最长距离. 析:其实这个题很水的,看一遍就知道怎么做了,很明显就是先从末速度开始算起,然后倒着推. 代 ...

随机推荐

  1. ios Symbol(s) not found for architecture arm64总结 含隐藏错误cocoapods

    一.通用 报错:Desktop/project/ASDF/WEIXIN/libWeChatSDK.a (3 slices) Undefinedsymbols for architecture arm6 ...

  2. go colly proxy 代理ip 动态 ip

    package main import ( "fmt" "github.com/gocolly/colly" "github.com/gocolly/ ...

  3. vue+vuex构建单页应用

    基本 构建工具: webpack 语言: ES6 分号:行首分号规则(行尾不加分好, [ , ( , / , + , - 开头时在行首加分号) 配套设施: webpack 全家桶, vue 全家桶 项 ...

  4. C语言实现 操作系统 银行家算法

    /**************************************************** 银行家算法 算法思想: 1. 在多个进程中,挑选资源需求最小的进程Pmin. 可能存在多类资 ...

  5. 流畅python学习笔记:第十四章:迭代器和生成器

    迭代器和生成器是python中的重要特性,本章作者花了很大的篇幅来介绍迭代器和生成器的用法. 首先来看一个单词序列的例子: import re re_word=re.compile(r'\w+') c ...

  6. ABAP-创建物料主数据

    CALL FUNCTION 'BAPI_MATERIAL_SAVEDATA' *&------------------------------------------------------- ...

  7. php 文件头部(header)

    发布:sunday01   来源:net     [大 中 小] 有关php文件头部信息(header)的详细介绍,是脚本学堂见过的最详细的一篇,有需要的朋友,千万不要错过这么好的文章. php文件头 ...

  8. before-request , after-request

    1 . flask的中间件 1)@app.before_request     # 请求进入视图函数之前,类似于django中间件的request_process 2)@app.after_reque ...

  9. iOS开发中集成Reveal

    [转]http://blog.devzeng.com/blog/ios-reveal-integrating.html 配置方式一简介有效. Reveal 是一个界面调试工具.使用Reveal,我们可 ...

  10. UVA10294 Arif in Dhaka (First Love Part 2) —— 置换、poyla定理

    题目链接:https://vjudge.net/problem/UVA-10294 题解: 白书P146~147. 为什么旋转i个间距,就有gcd(i,n)个循环,且每个循环有n/gcd(i,n)个元 ...