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$ 刀, ...
随机推荐
- javascript Demo
var vm=(function(){ var name="jasper"; var changename=function(v){ name=v; }; return { nam ...
- Flyweight
1 意图:运用共享技术有效地大量支持细粒度的对象 2 动机:flyweight是一个共享对象,可以在多个场景使用. 分为内部状态和外部状态,内部状态存储于flyweight中,包含了独立于flywei ...
- Windows 10:解决开机显示C:\WINDOWS\system32\config\systemprofile\Desktop不可用的方法
开机显示C:\WINDOWS\system32\config\systemprofile\Desktop不可用应该是不少网友都遇到过. 近日在使用Windows 10 Build 9926中,也出 ...
- python 安装模块步骤
1.下载 pyocr-0.4.1.tar.gz tar.gz文件 解压 放到 c:/python27 文件夹下面 C:\Python27\pyocr-0.4.1 直接 cmd 命令 进入 ...
- zoj 1789 The Suspects
好高兴,又AC一道 ,不过是很类似的两道..还是好高兴呀思想跟2833是一样的,不过要重新设计输入和输出.老师上课又重新讲解了一下,因为嫌疑人已知是0,所以加入集中时应该默认让数值小的做树根,即最终让 ...
- hdu ----3695 Computer Virus on Planet Pandora (ac自动机)
Computer Virus on Planet Pandora Time Limit: 6000/2000 MS (Java/Others) Memory Limit: 256000/1280 ...
- Mysql:The table‘xxxx’is full
下午跑程序,在插入mysql时突然报错: "The table'xxxx'is full" 而之前一直没问题的. 上网查了一下,都说临时表的问题,需要设置"tmp_tab ...
- Objective-C( Foundation框架 一 数组(NSArray))
OC数组 NSArray是静态的数组,就是它所指向的内容是不可改变的,它指向一段内存区域,一旦初始化,不能通过它对该内存区域的数据进行修改操作,但是它可以读数据. NSMutableArray是动态的 ...
- 特定场景下SQL的优化
1.大表的数据修改最好分批处理. 1000万行的记录表中删除更新100万行记录,一次只删除或更新5000行数据.每批处理完成后,暂停几秒中,进行同步处理. 2.如何修改大表的表结构. 对表的列的字段类 ...
- 如何在CentOS 7上安装Percona服务器
在这篇文章中我们将了解关于 Percona 服务器,一个开源的MySQL,MariaDB的替代品.InnoDB的数据库引擎使得Percona 服务器非常有吸引力,如果你需要的高性能,高可靠性和高性价比 ...