CF 600B

  题目大意:给定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 --- 二分查找的更多相关文章

  1. 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 ...

  2. CodeForces - 600B Queries about less or equal elements (二分查找 利用stl)

    传送门: http://codeforces.com/problemset/problem/600/B Queries about less or equal elements time limit ...

  3. 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 ...

  4. 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 ...

  5. 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 ...

  6. Educational Codeforces Round 2 B. Queries about less or equal elements

    打开题目连接 题意:给2个数组(无序的)啊a,b,判断b数组中的每一个元素大于a数组中个数. ACcode: #include <iostream> #include <vector ...

  7. CF 1405E Fixed Point Removal【线段树上二分】

    CF 1405E Fixed Point Removal[线段树上二分]  题意: 给定长度为\(n\)的序列\(A\),每次操作可以把\(A_i = i\)(即值等于其下标)的数删掉,然后剩下的数组 ...

  8. HDU 3280 Equal Sum Partitions(二分查找)

    Equal Sum Partitions Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Oth ...

  9. AtCoder Regular Contest 100 (ARC100) D - Equal Cut 二分

    原文链接https://www.cnblogs.com/zhouzhendong/p/9251420.html 题目传送门 - ARC100D 题意 给你一个长度为 $n$ 的数列,请切 $3$ 刀, ...

随机推荐

  1. 《BI那点儿事》数据流转换——条件性拆分

    根据条件分割数据是一个在数据流中添加复杂逻辑的方法,它允许根据条件将数据输出到其他不同的路径中.例如,可以将TotalSugar< 27.4406的输出到一个路径,TotalSugar > ...

  2. socket初级使用(客户端)

    在国庆这段时间里用零星的一些时间看了一下socket的学习资料,由于笔者偏向学习实用方面的内容,因此此篇文章涉及理论知识较少,主要是以实现思路(怎么做)为主,但在实现之前还是需要了解一些基础的理论知识 ...

  3. java中重载、覆盖和隐藏三者的区别分析

    重载:方法名相同,但参数不同的多个同名函数 注意:1.参数不同的意思是参数类型.参数个数.参数顺序至少有一个不同 2.返回值和异常以及访问修饰符,不能作为重载的条件(因为对于匿名调用,会出现歧义,eg ...

  4. JQuery Cross Domain

    frontend: first :add $.support.cors=true; in front of the Ajax code. seconde: add the crossDomain:tr ...

  5. appcan.windw.open appcan.frame.open appcan.window.openPopOver evaluateScript

    在模拟器上,几种发方式产生的窗口,其实都是iframe. 在根窗口(app打开的第一个)执行JS: try{ appcan.window.evaluateScript( { name: 'root', ...

  6. php的特性

    PHP的特性包括: 1. PHP 独特的语法混合了 C.Java.Perl 以及 PHP 自创新的语法. 2. PHP可以比CGI或者Perl更快速的执行动态网页——动态页面方面,与其他的编程语言相比 ...

  7. UOJ Test Round 1

    第一题: 题目大意: 给出N个字符串,字符串的前面部分都是字母且都是一样的,后面部分是数字,按照后面的数字排序.N<=10000 解题过程: 1.第一题是真良心,一开始的做法是把后面的数字分离出 ...

  8. Oracle 分析函数之 lag和lead

    Lag和Lead分析函数可以在同一次查询中取出同一字段的前N行的数据(Lag)和后N行的数据(Lead)作为独立的列. 这种操作可以代替表的自联接,并且LAG和LEAD有更高的效率. /*语法*/   ...

  9. python leetcode 日记 --Contains Duplicate --217

    题目 Given an array of integers, find if the array contains any duplicates. Your function should retur ...

  10. matlab 非平稳变化时域分析

    对于非平稳信号,由于傅立叶变换核心函数-正弦函数具有无限性,因此选用短时窗来分析局域信号: 需要注意的时,选取完滑动的时间窗一般是中心对称而且为奇数,这时被分析的时间点正好是滑动窗的中点. 因此,时域 ...