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. 单据BE构建

    本节来构建单据BE1.新建单据BE实体项目,修改命名空间 2.引入单据基类如下图所示,在UFIDA.U9.Base.BaseBE.MetaData命名空间下将类Doc托至设计BE视图中 3.分别托2个 ...

  2. CoreText原理及基本使用方法

    关于富文本的排版也是现在的一个技术点,以下是近日关于CoreText的学习记录以及个人理解,希望能对正在学习CoreText的朋友起到帮助. 1.框架坐标系 首先让我们先来看看CoreText坐标系和 ...

  3. python 中的input

    渣渣之路. 一. 在python编程初学者指南中的第六章.使用参数和返回值的例子中: # -*- coding: utf-8 -*- def display(message): print messa ...

  4. Java第一个程序

    public class HelloWorld{ public static void main(String[] args){ System.out.println("七期项目二部&quo ...

  5. CMD规范(通用模块定义规范)(翻译)

    最近在使用sea.js.大家知道sea.js遵循CMD规范.该规范的英文说明很简洁,我试着翻译了一下,旨在交流. Common Module Definition 通用模块定义规范 This spec ...

  6. postgres 查询数据库所有表名

    select relname as TABLE_NAME ,col_description(c.oid, 0) as COMMENTS from pg_class cwhere relkind = ' ...

  7. js面向对象选项卡

    window.onload=function() //面向对象 { var tab=new tabSwitch("div1"); var tab=new tabSwitch(&qu ...

  8. spring随手笔记4:ref的属性

    1.local属性 引用在同一个xml的bean           只能引用bean的id <bean id="HelloWord" class="com.ltf ...

  9. JDE 增加合计列

  10. easyui treegrid 分页

    $(function () { $('#maintable').treegrid({ width: '98%', height: 550, nowrap: true, striped: true, f ...