B. BerSU Ball
来源:互联网 发布:淘宝卖家app 编辑:程序博客网 时间:2024/06/05 18:41
The Berland State University is hosting a ballroom dance in celebration of its 100500-th anniversary! n boys and mgirls 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.
The first line contains an integer n (1 ≤ n ≤ 100) — the number of boys. The second line contains sequencea1, 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 sequenceb1, b2, ..., bm (1 ≤ bj ≤ 100), where bj is the j-th girl's dancing skill.
Print a single number — the required maximum possible number of pairs.
41 4 6 255 1 5 7 9
3
41 2 3 4410 11 12 13
0
51 1 1 1 131 2 3
2
解题说明:此题要求男女配对时舞技相差1,求出最多的配对数,最简单的方法是分别对两组数进行排序,然后顺序遍历,每次把相差最小的数进行配对,遍历结束后自然得到最多的配对数。
#include<iostream>#include<cstdio>#include<algorithm>#include<cstring>#include<string>#include<cmath>#include<cstdlib>using namespace std;int main(){int n,m,a[1000],b[1000],dem=0;int i,j;cin>>n;for( i=0;i<n;i++){cin>>a[i];}cin>>m;for( i=0;i<m;i++){cin>>b[i];}sort(a,a+n);sort(b,b+m);for( i=0,j=0;i<n && j<m;){if(abs(a[i]-b[j])<=1){dem++;i++;j++;}else if(a[i]<b[j]){i++;}else {j++;}}cout<<dem<<endl;return 0;}
- B. BerSU Ball
- 【寒江雪】B. BerSU Ball
- CF 277.5 B. BerSU Ball
- CodeForces 489B BerSU Ball
- Codeforces 489B BerSU Ball
- coderforce 489B B. BerSU Ball
- BerSU Ball
- Codeforces Round #277.5 (Div. 2)B——BerSU Ball
- Codeforces #277.5 (Div. 2) B. BerSU Ball(DP:LCS)
- Codeforces Round #277.5 (Div. 2)---B. BerSU Ball (贪心)
- Codeforces Round #277.5 (Div. 2):B BerSU Ball
- Codeforces Round #277.5(Div. 2) B. BerSU Ball【二分匹配】
- codeforces—— 489B —— BerSU Ball
- Gym 100015 B Ball Painting
- Gym 100015B Ball Painting
- ball
- ball
- Ball
- 记录1--s3c2440 GPIO && UART的控制
- 程序项目管理(1)
- 数组大折腾2
- Delphi XE7中使用JSON
- Qlikview配置ODBC连接SQL SERVER/ORACLE
- B. BerSU Ball
- HTTP长连接与短连接
- 第十三周项目一——数组大折腾(4)
- C++笔试面试总结
- 一个记事本程序
- 单词翻转
- CSS制作水平垂直居中对齐
- Python 命名空间
- No space left on device