Codeforces Round #277.5 (Div. 2)-B. BerSU Ball
http://codeforces.com/problemset/problem/489/B
1 second
256 megabytes
standard input
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.
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.
Print a single number — the required maximum possible number of pairs.
4
1 4 6 2
5
5 1 5 7 9
3
4
1 2 3 4
4
10 11 12 13
0
5
1 1 1 1 1
3
1 2 3
2
解题思路: 将男孩和女孩升序排列, 分两种情况,1.当前男孩和女孩可以组成一组,那就组一组,2,当前南海比女孩的水平高1以上,查询下一个女孩,3,否则就查询下一个男孩,知道男孩或者女孩都选完
1 #include <iostream>
2 #include <stdio.h>
3 #include <stdlib.h>
4 #include <string.h>
5 #include <time.h>
6 #include <math.h>
7
8 using namespace std;
9
int n, m, a[], b[];
int cmp(const void *a, const void *b){
return *(int *)a - *(int *)b;
}
void solve(){
int ans = , i = , j = ;
while(i < n &&j < m){
if(a[i] > b[j] + ){
j++;
}
else if(abs(a[i] - b[j]) == || a[i] == b[j]){
ans++, i++, j++;
}
else{
i++;
}
}
printf("%d\n", ans);
}
int main(){
int i;
while(scanf("%d", &n) != EOF){
for(i = ; i < n; i++){
scanf("%d", &a[i]);
}
qsort(a, n, sizeof(a[]), cmp);
scanf("%d", &m);
for(i = ; i < m; i++){
scanf("%d", &b[i]);
}
qsort(b, m, sizeof(b[]), cmp);
solve();
}
return ;
47 }
Codeforces Round #277.5 (Div. 2)-B. BerSU Ball的更多相关文章
- 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 ...
- 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 ...
- 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 ...
- Codeforces Round #277.5 (Div. 2) ABCDF
http://codeforces.com/contest/489 Problems # Name A SwapSort standard input/output 1 s, 256 ...
- Codeforces Round #277.5 (Div. 2)
题目链接:http://codeforces.com/contest/489 A:SwapSort In this problem your goal is to sort an array cons ...
- 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 ...
- Codeforces Round #277.5 (Div. 2)部分题解
A. SwapSort time limit per test 1 second memory limit per test 256 megabytes input standard input ou ...
- 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 ...
- 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 ...
随机推荐
- 《剑指offer》面试题5—从尾到头打印链表
重要思路: 这个问题肯定要遍历链表,遍历链表的顺序是从头到尾,而要输出的顺序却是从尾到头,典型的“后进先出”,可以用栈实现. 注意stl栈的使用,遍历stack的方法. #include <io ...
- Linux 获取文件属性
使用stat/lstat获取文件属性 头文件:#include <sys/types.h> #include <sys/stat.h> int stat(const char ...
- win7 mongod不是内部命令
1.下载MongoDB 1.1 MongoDB下载 1.2 选择Server下面的 Community 2.安装MongoDB 2.1 注意事项:一直下一步就行了,但是遇到下面这个界面,注意一定要去掉 ...
- HDU 3499【最短路】
题意: 给你一幅图,然后起点终点,然后有一个条件是可以使某条边的花费减半,求最短路的最小花费. 思路: (来自大哥) 最短路的时候多一维,途中是否有花费减半的边: 然后转移,如果上一条有减半的,这一条 ...
- Matlab图像处理相关
相关函数: 读取:imread() %参数为文件名(路径)或url,格式等 写入:imwrite() %参数为写入数据矩阵,写入文件名(路径),格式等 显示:imshow() %显示由输入决定,属性自 ...
- python错误之UnicodeEncodeError: 'ascii' codec can't encode characters in position 7-8: ordinal not in range(128)
# coding = ascii import jsonimport pickleimport sysimport os decode()和encode方法中第一个参数为编码格式,第二个为出现无法转换 ...
- python 基础(四) 函数
函数 一.什么是函数? 函数是可以实现一些特定功能的 小方法 或者是小程序 优点: 提高 了代码的后期维护 增加了代码的重复使用率 减少了代码量 提高了代码可读性 二.函数的定义 使用 def关键+函 ...
- Codeforces 1138B(列方程枚举)
构造模拟要分情况讨论感觉不是够本质,然后官解是因为只有四个量所以可以根据限制条件列两个方程,再枚举一下解就可以了. const int maxn = 5000 + 5; int n, c[maxn], ...
- 牛客练习赛42B(异或的性质)
传送门 b^ c >= b - c,这个结论应该记住,我还在这里证过…… 这个题就用到了这个结论,假如当前答案集合为S,和为a,异或和为b,当前答案为a+b了.这时又读入个c,该不该加进来?a ...
- 记录一下filter
filter是什么,如它的字面意思,就是拦截器.它可以在request到达相关资源之前,比如servlet之前先处理requeset,也可以拦截或处理从某个资源比如servlet发出的response ...