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

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

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

AC代码:

#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <math.h>
#include <stdlib.h>
#include <time.h>
using namespace std;
#define INF 0x7fffffff int a[105], b[105]; //boy,girl int main()
{
#ifdef sxk
freopen("in.txt","r",stdin);
#endif
int n, m;
while(scanf("%d",&n)!=EOF)
{
for(int i=0; i<n; i++)
scanf("%d", &a[i]);
scanf("%d",&m);
for(int i=0; i<m; i++)
scanf("%d", &b[i]);
sort(a, a+n); //排序
sort(b, b+m);
int ans = 0;
for(int i=0, j=0; i<n && j<m;){
if(abs(a[i]-b[j])<=1) { //符合条件
ans++;
i++;
j++;
}
else if(a[i] > b[j]){ //对方太低
j++;
}
else i++; //自己太低
}
printf("%d\n", ans);
}
return 0;
}

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. display:table 水平居中

    <div style="width:auto; margin:auto;display:table"> <div style="width: 100px ...

  2. pc2日记——有惊无险的第二天2014/08/29

    今天下午如期的用pc2进行了第二场比赛.因为昨天的出错经历和早上充足的准备,下午的比赛尽管在開始的时候出了点小小的问题,但总的来说还是非常成功的. 早上八点过去504開始又一次配置client,由于开 ...

  3. 可以根据柜子内表取出所有的柜子信息的BAPI函数

    DATA: gt_hunumbers TYPE STANDARD TABLE OF bapihunumber,      gt_huitem  TYPE STANDARD TABLE OF bapih ...

  4. mysql 主从同步出问题,重新修复从库 - web架构研究

    mysql 主从同步出问题,重新修复从库 - web架构研究     mysql 主从同步出问题,重新修复从库    0     昨天由于操作失误,在从库上执行一堆sql之后,导致主从同步错误,并且已 ...

  5. 向大家推荐个android的游戏引擎——cocos2d-x

    最近发现单单用android自带的功能函数来编写游戏,往往有很大的局限性,即耗时长,调试繁琐,没有一定的框架.所以博主发现了游戏引擎这个好东西,游戏引擎所拥有的架构和功能函数,使得游戏的编写更加得心应 ...

  6. Unity3d 4.3.4f1执行项目

    今天.本来执行的非常快的一个项目. 忽然打开非常晚.尝试新建一个新的项目,竟然执行速度非常快. 心有不忿的,把整个Unity删除了.又一次安装. 再打开那个执行变慢的项目. 结果.执行速度回来了. 不 ...

  7. Delphi中复制带有String的记录结构时不能使用Move之类的内存操作函数

    请看下面的代码: program TestRecord; {$APPTYPE CONSOLE} uses  SysUtils,  Math; type  TRecordA = record    Na ...

  8. 对TMemoryStream的一些改进(用到了LockFile)

    对TMemoryStream的一些改进 怎么又是关于Stream的,呵呵,应该说只是最近比较关心程序的效率问题,而我对Stream其实并没有什么特别的研究,只是自己发现了一些新的用法,希望能对大家有用 ...

  9. InputStream中read()与read(byte[] b)(转)

    read()与read(byte[] b)这两个方法在抽象类InputStream中前者是作为抽象方法存在的,后者不是,JDK API中是这样描述两者的: 1:read() : 从输入流中读取数据的下 ...

  10. linux 下opensplice的简易安装

    http://www.prismtech.com/opensplice/opensplice-dds-community/software-downloads 下载对应我选择的是: OpenSplic ...