Codeforces Round #277.5 (Div. 2)---B. BerSU Ball (贪心)
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才干构成一对舞伴,在每一个人都不反复的情况下,问最多能构成多少对?
解题思路:贪心。把两个数组都从小到大排序,再依次用当前最小的去跟对方比,若符合条件,则两方下标都++;若自己太低,则自己下标++,否则对方下标++。
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 (贪心)的更多相关文章
- 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【贪心/双指针/每两个跳舞的人可以配对,并且他们两个的绝对值只差小于等于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 ...
随机推荐
- 修改XPMenu让ToolButton在Down=True时正确显示
XPMenu是一个不错的程序界面效果控件,但它也存在不少不足之处.我最近又对它作了一点修改. 原因是我在程序里有一个ToolButton,其Style=tbsButton,当Down=True时,XP ...
- Linux Crontab 定时任务使用总结
任务调度的crond常用命令 crond 是linux用来定期执行程序的命令.当安装完成操作系统之后,默认便会启动此任务调度命令.crond命令每分锺会定期检查是否有要执行的工作,如果有要执行的工作便 ...
- Perl入门(四)Perl的正則表達式
正則表達式是Perl语言的特色.主要的语法不是非常难,可是编写一个符合需求.高效的正則表達式.还是有一些挑战的. Perl的三种匹配模式 1.查找 语法:m/正則表達式内容/; 作用:查找匹配内容中是 ...
- 这里的*号实际表示就是RAC中所有实例都使用
您的位置: ITPUB个人空间 » cc59的个人空间 » 日志 发布新日志 我的日志我的足迹我的收藏 unix/linuxHA随笔backup&restoreperformance tuni ...
- Codeforces 475D CGCDSSQ 求序列中连续数字的GCD=K的对数
题目链接:点击打开链接 #include <cstdio> #include <cstring> #include <algorithm> #include < ...
- Google 搜索的基本语法
★搜索引擎的选择 先简单说一下"搜索引擎的选择". 在咱们天朝,Google 屡屡被 GFW 骚扰,导致百度占了便宜,成为份额最高的搜索引擎.不过今天这篇教程,俺还是继续拿 Goo ...
- Mysql 导入导出数据结构及数据
方式一: mysqldump -ukevin -P3306 --default-character-set=utf8 -p -h10.1.15.123 activity sign_in_user &g ...
- 谈论高并发(三十)解析java.util.concurrent各种组件(十二) 认识CyclicBarrier栅栏
这次谈话CyclicBarrier栅栏,如可以从它的名字可以看出,它是可重复使用. 它的功能和CountDownLatch类别似,也让一组线程等待,然后开始往下跑起来.但也有在两者之间有一些差别 1. ...
- hdu 3405 world islands
求删点后最小的生成树,n<50....数据好弱,直接暴力枚举就行...删点的时候直接g[i][j]=INF就行了. #include<iostream> #include<al ...
- cocos2d-x项目101次相遇: Scenes , Director, Layers, Sprites
cocos2d-x 101次相遇 / 文件夹 1 安装和环境搭建 -xcode 2 Scenes , Director, Layers, Sprites 3 建立图片菜单 4 在 ...