第三届沈阳航空航天大学校赛(大连海事大学赛)---C: Greater or Lesser (sort)
来源:互联网 发布:压力传感单片机 编辑:程序博客网 时间:2024/06/10 01:21
Greater or Lesser
Time Limit: 2 Sec Memory Limit: 128 MBDescription
Baby A has given you N positive integers, then, he will ask M times. Each query is positive integer x. For each query, please tell Baby A how many positive integers there are lesser than x or greater than x.
Input
Input contains several test cases ending by the end of file. The first line of each test case is two positive integers N and M (1 <= N <= 2 * 10^6, 1 <= M <= 5000). The second line has N positive integers standing for the numbers that baby A has given you. Then M lines follow. Each line has a positive integer standing for a query. Each number will be lesser than 10^8.
Output
For each query, output one line contains two numbers, standing for the number in Baby A's set that is lesser and greater than x.
Sample Input
Sample Output
题意:给n个数,有m次询问,每次询问都有一个x,输出在这个序列中,有多少个数比x小,有多少比x大。
分析:先把数组从小到大排个序,然后用lower_bound(a, a+n, key)和upper_bound(a, a+n, key)进行处理,其中lower_bound(a, a+n, key)返回的a数组中第一个比key小的元素的地址,所以我们用lower_bound(a, a+n, key) - a 就得到了比key小的元素个数,同理,upper_bound(a, a+n, key)返回的是a中第一个比key大的元素位置,则(a + n) - upper_bound(a, a+n, key)就得到了a中比key大的元素个数。而且这个速度还是很可观的。
AC代码:
#include <cstdio>#include <iostream>#include <algorithm>#include <cstring>using namespace std;int a[2000005];int main(){//freopen("in.txt", "r", stdin);int n, m, p;while(scanf("%d%d", &n, &m)!=EOF){memset(a, 0, sizeof(a));int fo = 0;for(int i=0; i<n; i++){scanf("%d", &a[i]);}sort(a, a+n); //从小到大排序for(int i=0; i<m; i++){scanf("%d", &p);printf("%d%d\n", lower_bound(a, a+n, p) - a, (a + n) - upper_bound(a, a+n, p)); //求值}}return 0;}
lower_bound(a, a+n, key)和upper_bound(a, a+n, key)对于我这种比较懒的人,还是很好用的^_^
- 第三届沈阳航空航天大学校赛(大连海事大学赛)---C: Greater or Lesser (sort)
- 第三届沈阳航空航天大学校赛(大连海事大学赛)---I: Spoors (预处理)
- 第三届沈阳航空航天大学校赛(大连海事大学赛)---签到题
- 【HDU 5980 Find Small A】+ 2016ACM/ICPC亚洲区大连站-重现赛(感谢大连海事大学)
- Wrestling Match 2016ACM/ICPC亚洲区大连站-重现赛(感谢大连海事大学)hdu 5971
- HDU 5974 A Simple Math Problem(数学解方程)——2016ACM/ICPC亚洲区大连站-重现赛(感谢大连海事大学)
- 【HDU 5971 Wrestling Match】+ 2016ACM/ICPC亚洲区大连站-重现赛(感谢大连海事大学) + 二分图染色
- 用Latex排版论文(4)《大连海事大学硕博学位论文Latex模板》中算法排版例子(添加中文输入和输出)
- 大连海事大学2017ACM程序设计大赛题解
- 好学校------烂学校(大学)
- 2013 大连海事大学腾讯马拉松 div2第一场部分题解
- 2013大连海事大学腾讯编程马拉松div2第二场部分题解
- 2017年6月4日大连海事大学acm校赛 后记
- 湘潭大学校赛
- 武汉大学校赛(个人总结+补题)
- 北京信息科技大学校赛(全部题解)
- 2016 沈阳区域赛第三题(Recursive sequence)(矩阵快速幂)
- 2017武汉大学校赛网络预选赛c题
- oracle 触发器
- MongoDB 自带JSON类 序列化自定义对象为Json字符串解决方案
- 2Sum closest vs. 2Minus closest
- JAVADOC中文使用说明,参数中文解释
- wt.method.MethodContext的作用和意义
- 第三届沈阳航空航天大学校赛(大连海事大学赛)---C: Greater or Lesser (sort)
- Cracking the coding interview--Q8.3
- Linux内核中的内存屏障(1)
- 增删改查开发规范
- jQuery对象的问题
- onethink钩子
- C++引用
- JAVA中float类型运算出现精度缺失,对于保留小数位解决方案
- apache开启gzip deflate