Codeforces Round #277.5 (Div. 2) B. BerSU Ball【贪心/双指针/每两个跳舞的人可以配对,并且他们两个的绝对值只差小于等于1,求最多匹配多少对】
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 【题意】:有n个boy,m个girl,每个人都有自己的舞蹈技术等级,现规定只有boy和girl的等级相差不大于1才能构成一对舞伴,在每个人都不重复的情况下,问最多能构成多少对?
【分析】:贪心。把两个数组都从小到大排序,再依次用当前最小的去跟对方比,若符合条件,则双方下标都++;若自己太低,则自己下标++,否则对方下标++。
【代码】:
#include<bits/stdc++.h>
using namespace std;
int n,m,cnt;
int a[],b[];
int main()
{
cin>>n;
for(int i=;i<n;i++) cin>>a[i]; sort(a,a+n);
cin>>m;
for(int i=;i<m;i++) cin>>b[i]; sort(b,b+m); for(int i=,j=;i<n&&j<m;)
{
if(abs(a[i]-b[j])<=)
{
i++;
j++;
cnt++;
}
else if(a[i]>b[j])
{
j++;
}
else
{
i++;
}
}
cout<<cnt<<endl;
return ;
}
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 ...
- 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) 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 ...
随机推荐
- IIS Express mime type 列表。
C:\Users\Administrator\Documents\IISExpress\config\applicationhost.config -------------------------- ...
- node express 登录拦截器 request接口请求
1.拦截器 拦截器可以根据需要 做权限拦截 登录只是权限的一种, 思路是req.session.user判断用户session是否存在,是否是需要拦截的地址, 如果是就跳转登录页,或其他页, 如果非需 ...
- jvm可视化工具jvisualvm插件——Visual GC
转自:http://blog.csdn.net/xuelinmei_happy/article/details/51090115 Visual GC是一个Java 内存使用分析与GC收集的可视化工具插 ...
- Ubuntu下禁用笔记本自带键盘
想要禁用笔记本自带键盘(Ubuntu)只要2条命令. 1. 打开终端,输入: xinput list ⎡ Virtual core pointer id=2 [master pointer (3)] ...
- [oldboy-django][2深入django]点击刷新验证码
# 点击更新验证码,只要重新在发送一个请求即可 <img src="/check_code/" onclick="updateCode(this);" w ...
- HDU 4671 Backup Plan 构造
负载是否平衡只与前两列有关,剩下的只要与前两列不重复就随便放. 第一列我们按1-n这样循环放,第二列每次找个数最少的那个服务器放. #include <cstdio> #include & ...
- 软工实践Alpha冲刺(4/10)
队名:起床一起肝活队 组长博客:博客链接 作业博客:班级博客本次作业的链接 组员情况 组员1(队长):白晨曦 过去两天完成了哪些任务 描述: 很胖,刚学,照猫画虎做了登录与注册界面. 展示GitHub ...
- 真的讨厌ClickOnce这东西
ClickOnce真的问题多. 升级问题,每次升级后都新建一个文件夹,旧的程序数据全都没有.迁移过来也一堆问题.还有录音问题,Win7下录音报错,最后查来查去就是将文件路径太太长,要控制长度.还有安装 ...
- SQLServer对视图或函数’XXX’的更新或插入失败,因其包含派生域或常量域解决
原因:视图view不允许修改. 解决:重新创建一个相同结构内容的表. 解释:因为所创建的视图对其属性值进行了计算的其他形式上的改变,而对视图的更改最终表现为对表的更改而表中不存在视图的某一属性,或属性 ...
- xpath属性值的模糊匹配
得至:http://bbs.csdn.net/topics/390857942 最后一楼 //div[contains(@class,'Number Skill')]