Codeforces Round #277.5 (Div. 2)-B

来源:互联网 发布:未再网络上注册怎么办 编辑:程序博客网 时间:2024/06/05 15:19

一看就是贪心,想了个策略,先排序再尽可能用两个较小的凑成一对,


代码:


#include<iostream>#include<cstdio>#include<cmath>#include<map>#include<cstring>#include<algorithm>#define rep(i,a,b) for(int i=(a);i<(b);i++)#define rev(i,a,b) for(int i=(a);i>=(b);i--)#define clr(a,x) memset(a,x,sizeof a)typedef long long LL;using namespace std;const int mod=1e9 +7;const int maxn=3005;const int maxm=4005;int da[maxn],db[maxn],u[maxn],v[maxn];int dis(int a,int b){    return a>b?a-b:b-a;}int main(){    int n,m;    while(~scanf("%d",&n))    {        for(int i=0;i<n;i++)            scanf("%d",&da[i]);        scanf("%d",&m);        for(int i=0;i<m;i++)            scanf("%d",&db[i]);        sort(da,da+n);        sort(db,db+m);        int cur=0,cnt=0;        for(int i=0;cur<m&&i<n;)        {            while(cur<m&&da[i]-db[cur]>1)                cur++;            while(i<n&&db[cur]-da[i]>1)                i++;            if(dis(db[cur],da[i])<=1)cnt++,cur++,i++;        }        printf("%d\n",cnt);    }    return 0;}


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
41 4 6 255 1 5 7 9
output
3
input
41 2 3 4410 11 12 13
output
0
input
51 1 1 1 131 2 3
output
2


0 0
原创粉丝点击