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
  1. 4
  2. 1 4 6 2
  3. 5
  4. 5 1 5 7 9
output
  1. 3
input
  1. 4
  2. 1 2 3 4
  3. 4
  4. 10 11 12 13
output
  1. 0
input
  1. 5
  2. 1 1 1 1 1
  3. 3
  4. 1 2 3
output
  1. 2

题意:有n个boy,m个girl,每一个人都有自己的舞蹈技术等级,现规定仅仅有boy和girl的等级相差不大于1才干构成一对舞伴,在每一个人都不反复的情况下,问最多能构成多少对?

解题思路:贪心。把两个数组都从小到大排序,再依次用当前最小的去跟对方比,若符合条件,则两方下标都++;若自己太低,则自己下标++,否则对方下标++。

AC代码:

  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <iostream>
  4. #include <algorithm>
  5. #include <vector>
  6. #include <queue>
  7. #include <set>
  8. #include <map>
  9. #include <string>
  10. #include <math.h>
  11. #include <stdlib.h>
  12. #include <time.h>
  13. using namespace std;
  14. #define INF 0x7fffffff
  15.  
  16. int a[105], b[105]; //boy,girl
  17.  
  18. int main()
  19. {
  20. #ifdef sxk
  21. freopen("in.txt","r",stdin);
  22. #endif
  23. int n, m;
  24. while(scanf("%d",&n)!=EOF)
  25. {
  26. for(int i=0; i<n; i++)
  27. scanf("%d", &a[i]);
  28. scanf("%d",&m);
  29. for(int i=0; i<m; i++)
  30. scanf("%d", &b[i]);
  31. sort(a, a+n); //排序
  32. sort(b, b+m);
  33. int ans = 0;
  34. for(int i=0, j=0; i<n && j<m;){
  35. if(abs(a[i]-b[j])<=1) { //符合条件
  36. ans++;
  37. i++;
  38. j++;
  39. }
  40. else if(a[i] > b[j]){ //对方太低
  41. j++;
  42. }
  43. else i++; //自己太低
  44. }
  45. printf("%d\n", ans);
  46. }
  47. return 0;
  48. }

Codeforces Round #277.5 (Div. 2)---B. BerSU Ball (贪心)的更多相关文章

  1. Codeforces Round #277.5 (Div. 2)-B. BerSU Ball

    http://codeforces.com/problemset/problem/489/B B. BerSU Ball time limit per test 1 second memory lim ...

  2. Codeforces Round #277.5 (Div. 2)B——BerSU Ball

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

  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 Round #277.5 (Div. 2) ABCDF

    http://codeforces.com/contest/489 Problems     # Name     A SwapSort standard input/output 1 s, 256 ...

  5. Codeforces Round #277.5 (Div. 2)

    题目链接:http://codeforces.com/contest/489 A:SwapSort In this problem your goal is to sort an array cons ...

  6. Codeforces Round #277.5 (Div. 2) A,B,C,D,E,F题解

    转载请注明出处: http://www.cnblogs.com/fraud/          ——by fraud A. SwapSort time limit per test    1 seco ...

  7. Codeforces Round #277.5 (Div. 2)部分题解

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

  8. Codeforces Round #277.5 (Div. 2) --E. Hiking (01分数规划)

    http://codeforces.com/contest/489/problem/E E. Hiking time limit per test 1 second memory limit per ...

  9. Codeforces Round #277.5 (Div. 2)-D. Unbearable Controversy of Being

    http://codeforces.com/problemset/problem/489/D D. Unbearable Controversy of Being time limit per tes ...

随机推荐

  1. Android 保存用户偏好设置

    很多情况下都允许用户根据自己的习惯和爱好去设置软件,而我们需要保存这些设置,可以用一个专业保存用户偏好的类:SharedPreferences. 这个类是实现方法其实也就是创建和修改 XML 文件, ...

  2. 编译x64的应用,要在pro文件里配置

    在pro中使用:contains(QMAKE_TARGET.arch, x86_64) {    TYPE = 64    QTDIR = C:/Qt/5.5/msvc2013_64} else {  ...

  3. catalan 数——卡特兰数(转)

    Catalan数——卡特兰数 今天阿里淘宝笔试中碰到两道组合数学题,感觉非常亲切,但是笔试中失踪推导不出来后来查了下,原来是Catalan数.悲剧啊,现在整理一下 一.Catalan数的定义令h(1) ...

  4. Win7+vs2010下安装boost_1_46_1库

    一.boost库分类: (1)不需要编译库:any.array.asio.conversion.crc.bind/mem_fn.enable_if.function.lambda.mpl.smart_ ...

  5. swift_将UIDatePicker到达的传播之间的时间差在数小时出现页面的事

    今天,写swift demo当它来到了一个非常精彩的问题,我再次 present 使用页面出来 UIDatePicker 选择时间,然后再回到原来的主界面的时间,结果出现的问题:B页面的正常时间,传回 ...

  6. Swift - 文本输入框(UITextField)的用法

    1,文本框的创建,有如下几个样式: UITextBorderStyle.None:无边框 UITextBorderStyle.Line:直线边框 UITextBorderStyle.RoundedRe ...

  7. 【Demo 0005】Java基础-类继承性

    本章学习要点:       1.  了解Java继承特性;       2.  掌握继承实现方法;       3.  掌握override规则: 一.类继承特性       1.  继承定义:使用己 ...

  8. java entry

    我希望要一个ArrayList<Entry>,类似C++中的pair, 可是Map.Entry是个接口,不能实例化,能够像以下这样写 HashMap<Integer, Integer ...

  9. 3TB硬盘的容量已经超出了传统分区标准的支持

    为什么3TB会有接近750G空间不能用? MBR分区格式是瓶颈 其实3TB硬盘之所以会出现各种问题,关键就在于它的容量已经超出了传统分区标准的支持.传统的硬盘采用MBR分区格式,使用LBA寻址,这种寻 ...

  10. poj1639 Picnic Planning 最小度数限制生成树

    题意:若干个人开车要去park聚会,可是park能停的车是有限的,为k.所以这些人要通过先开车到其它人家中,停车,然后拼车去聚会.另外,车的容量是无限的,他们家停车位也是无限的. 求开车总行程最短. ...