CF 600B Queries about less or equal elements --- 二分查找
题目大意:给定n,m,数组a(n个数),数组b(m个数),对每一个数组b中的元素,求数组a中小于等于数组该元素的个数。
解题思路:对数组a进行排序,然后对每一个元素b[i],在数组a中进行二分查找第一个大于b[i]的位置即为结果
/* CF 600B Queries about less or equal elements --- 二分查找 */
#include <cstdio>
#include <algorithm>
using namespace std; const int maxn = ;
int a[maxn], b[maxn]; /*
@function:在数组A的区间[x,y)中查找第一个大于key的位置(二分查找求上界)
@param1:A 待查找的数组名
@param2: [x,y)表示左闭右开区间
@param3: 查找的值
@return: 返回第一个大于key的位置
*/
int BinarySearch(int *A, int x, int y, int key){
while (x < y){
int mid = x + (y - x) / ;
if (A[mid] <= key){
x = mid + ;
}
else{
y = mid;
}
}//while(x<y)
return x;
} int main()
{
int n, m;
//n为数组a长度,m为数组b元素个数
while (scanf("%d%d", &n, &m) == ){
for (int i = ; i < n; ++i){
scanf("%d", a + i);
}//for(i)
for (int i = ; i < m; ++i){
scanf("%d", b + i);
}//for(i)
sort(a, a + n);
a[n] = << ; //对b中的每一个数b[i],在A中查找小于等于b[i]的数的个数
for (int i = ; i < m; ++i){
int k = BinarySearch(a, , n, b[i]);
printf(i == m - ? "%d\n" : "%d ", k);
}//for(i)
} return ;
}
还可以直接利用STL中的upper_bound大大简化代码:
/* CF 600B Queries about less or equal elements --- 二分查找 */
#include <cstdio>
#include <algorithm>
using namespace std; const int maxn = ;
int a[maxn];
int b[maxn]; int main()
{
int n, m;
while (scanf("%d%d", &n, &m) == ){
for (int i = ; i < n; ++i){
scanf("%d", a + i);
}//for(i)
sort(a, a + n);
for (int i = ; i < m; ++i){
scanf("%d", b + i);
int k = (upper_bound(a, a + n, b[i]) - a);
printf(i == m - ? "%d\n" : "%d ", k);
}//for(i)
} return ;
}
CF 600B Queries about less or equal elements --- 二分查找的更多相关文章
- Codeforces 600B Queries about less or equal elements(二分查找)
Description You are given two arrays of integers a and b. For each element of the second array bj yo ...
- CodeForces - 600B Queries about less or equal elements (二分查找 利用stl)
传送门: http://codeforces.com/problemset/problem/600/B Queries about less or equal elements time limit ...
- Educational Codeforces Round 2 B. Queries about less or equal elements 水题
B. Queries about less or equal elements Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforc ...
- Educational Codeforces Round 2_B. Queries about less or equal elements
B. Queries about less or equal elements time limit per test 2 seconds memory limit per test 256 mega ...
- Queries about less or equal elements CodeForces - 600B(二分)
You are given two arrays of integers a and b. For each element of the second arraybj you should find ...
- Educational Codeforces Round 2 B. Queries about less or equal elements
打开题目连接 题意:给2个数组(无序的)啊a,b,判断b数组中的每一个元素大于a数组中个数. ACcode: #include <iostream> #include <vector ...
- CF 1405E Fixed Point Removal【线段树上二分】
CF 1405E Fixed Point Removal[线段树上二分] 题意: 给定长度为\(n\)的序列\(A\),每次操作可以把\(A_i = i\)(即值等于其下标)的数删掉,然后剩下的数组 ...
- HDU 3280 Equal Sum Partitions(二分查找)
Equal Sum Partitions Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Oth ...
- AtCoder Regular Contest 100 (ARC100) D - Equal Cut 二分
原文链接https://www.cnblogs.com/zhouzhendong/p/9251420.html 题目传送门 - ARC100D 题意 给你一个长度为 $n$ 的数列,请切 $3$ 刀, ...
随机推荐
- akka笔记
Actor UntypedActor actor的基类,继承并实现onReceive方法就可以得到一个Actor. Props 配置类,用Props.create可以创建一个按指定配置生成的Actor ...
- openssl evp 哈希算法(md5,sha1,sha256)
1. 简述 openssl提供了丰富密码学工具,一些常用的哈希算法 比如md5,sha 可以直接用提供的md5.h ,sha.h 接口使用: 为了方便开发者使用,openssl 又提供了一个EVP, ...
- 转 Android开发笔记(成长轨迹)
http://blog.csdn.net/mad1989/article/details/9142557
- Cisco IOS IP Service Level Agreementv (IP SLA)
Responder and Control Protocol 1.Responder内嵌在思科目标路由器中的一个组件,用来对IP SLA请求包做应答,通过对应达包添加时间戳属性,以提高测量计算的准确性 ...
- Python中 filter | map | reduce | lambda的用法
1.filter(function, sequence):对sequence中的item依次执行function(item),将执行结果为True的item组成一个List/String/Tupl ...
- 介绍开源的.net通信框架NetworkComms框架之五 心跳检测
原文网址: http://www.cnblogs.com/csdev Networkcomms 是一款C# 语言编写的TCP/UDP通信框架 作者是英国人 以前是收费的 目前作者已经开源 许可是 ...
- iOS开发UI篇—APP主流UI框架结构
iOS开发UI篇—APP主流UI框架结构 一.简单示例 说明:使用APP主流UI框架结构完成简单的界面搭建 搭建页面效果: 二.搭建过程和 ...
- asp.net导出word(word2007)
1.只能导出成word2007格式(.docx),可直接导出到客户端 2.服务器上不需要装任何东西,也没有权限限制,比较适合导出表格(支持图片) 3.需要一个国外的DocX.dll插件 4.需要添加引 ...
- 学习Nim语言.rar(nim语言中文教程下载)
学习Nim语言 nim 语法上类似python ,是一门静态编译型语言,nim 使用空格缩进标示语句块的开始和结束, 喜欢python风格的程序员应该也会很容易适应和喜欢nim的风格. nim语言官方 ...
- jQueryMobile控件之ListView
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...