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
  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

二分匹配模板题

  1. #include <map>
  2. #include <set>
  3. #include <list>
  4. #include <queue>
  5. #include <stack>
  6. #include <vector>
  7. #include <cmath>
  8. #include <cstdio>
  9. #include <cstring>
  10. #include <iostream>
  11. #include <algorithm>
  12.  
  13. using namespace std;
  14.  
  15. const int N = 110;
  16.  
  17. int mark[N];
  18. bool vis[N];
  19. int head[N];
  20. int tot;
  21. int n, m;
  22. int b[N];
  23. int g[N];
  24.  
  25. struct node
  26. {
  27. int next;
  28. int to;
  29. }edge[N * N];
  30.  
  31. void addedge(int from, int to)
  32. {
  33. edge[tot].to = to;
  34. edge[tot].next = head[from];
  35. head[from] = tot++;
  36. }
  37.  
  38. bool dfs(int u)
  39. {
  40. for (int i = head[u]; ~i; i = edge[i].next)
  41. {
  42. int v = edge[i].to;
  43. if (!vis[v])
  44. {
  45. vis[v] = 1;
  46. if (mark[v] == -1 || dfs(mark[v]))
  47. {
  48. mark[v] = u;
  49. return 1;
  50. }
  51. }
  52. }
  53. return 0;
  54. }
  55.  
  56. int hungry()
  57. {
  58. memset(mark, -1, sizeof(mark));
  59. int ans = 0;
  60. for (int i = 1; i <= n; ++i)
  61. {
  62. memset(vis, 0, sizeof(vis));
  63. if (dfs(i))
  64. {
  65. ans++;
  66. }
  67. }
  68. return ans;
  69. }
  70.  
  71. int main()
  72. {
  73. while (~scanf("%d", &n))
  74. {
  75. for (int i = 1; i <= n; ++i)
  76. {
  77. scanf("%d", &b[i]);
  78. }
  79. scanf("%d", &m);
  80. for (int i = 1; i <= m; ++i)
  81. {
  82. scanf("%d", &g[i]);
  83. }
  84. memset (head, -1, sizeof(head));
  85. tot = 0;
  86. for (int i = 1; i <= n; ++i)
  87. {
  88. for (int j = 1; j <= m; ++j)
  89. {
  90. if(abs(b[i] - g[j]) <= 1)
  91. {
  92. addedge(i, j);
  93. }
  94. }
  95. }
  96. printf("%d\n", hungry());
  97. }
  98. return 0;
  99. }

版权声明:本文博主原创文章。博客,未经同意不得转载。

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 (贪心)

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

  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. DEBUG模式下, 内存中的变量地址分析

    测试函数的模板实现 /// @file my_template.h /// @brief 测试数据类型用的模板实现 #ifndef MY_TEMPLATE_H_2016_0123_1226 #defi ...

  2. CSS中的!important属性用法

    关于CSS的运用技巧有很多, 今天主要探讨一下CSS中 !important 这个属性的用法.在CSS的使用中,遇到最多的问题就是不同浏览器之间的兼容问题. 由于IE并不严格执行W3C标准, 而又几乎 ...

  3. 如何获得getElementById的length这个数值?

    a=document.getElementById("a").innerHTML.length;我觉得你应该这么写 如果是文本框的话document.getElementById( ...

  4. ORACLE DATABASE 10G FALSHBACK 知识整理

    1.知识储备 1)    当出现介质损坏时(如数据文件丢失),任何闪回方法都毫无用处,只能执行标准的备份.还原与恢复. 2.SCN记录方法 SQL>variable x_scn number; ...

  5. Android 界面滑动实现---Scroller类 从源码和开发文档中学习(让你的布局动起来)

    在android学习中,动作交互是软件中重要的一部分,其中的Scroller就是提供了拖动效果的类,在网上,比如说一些Launcher实现滑屏都可以通过这个类去实现..   例子相关博文:Androi ...

  6. OpenStreetMap初探(一)——了解OpenStreetMap

    1. 開始关注OpenStreetMap始于此博文:<微软对抗谷歌的秘密武器:开源地图OpenStreetMap>  http://news.csdn.net/a/20120328/313 ...

  7. 友情转发一则Erlang招聘广告

    新锐手游开发公司WalkYY,招聘Erlang游戏服务端开发工程师若干名,要求有半年以上Erlang游戏服务端开发经验,熟悉Erlang OTP和MySQL数据库.公司团队靠谱,发展空间大,有意者请发 ...

  8. FOJ 2170 花生的序列 dp

    题目链接:http://acm.fzu.edu.cn/problem.php? pid=2170 贴个baka爷的代码留念.. 数据出的有问题.输入的字符串长度不超过1000 #include< ...

  9. Delphi默认窗体随想

    Delphi中新建一个Form或者Frame时,它的字体都是西文习惯,这样就有可能造成在其他机器上由于字体的原因,窗体十分不美观.怎样才能为Delphi设置一个默认窗体,让它的字体Font符合中国习惯 ...

  10. Delphi的指针 good

    Pointers are like jumps, leading wildly from one part of the data structure to another. Their introd ...