CF 600B Queries about less or equal elements --- 二分查找

  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 = 200005;
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) / 2;
        if (A[mid] <= key){
            x = mid + 1;
        }
        else{
            y = mid;
        }
    }//while(x<y)
    return x;
}

int main()
{
    int n, m;
    //n为数组a长度,m为数组b元素个数
    while (scanf("%d%d", &n, &m) == 2){
        for (int i = 0; i < n; ++i){
            scanf("%d", a + i);
        }//for(i)
        for (int i = 0; i < m; ++i){
            scanf("%d", b + i);
        }//for(i)
        sort(a, a + n);
        a[n] = 1 << 30;

        //对b中的每一个数b[i],在A中查找小于等于b[i]的数的个数
        for (int i = 0; i < m; ++i){
            int k = BinarySearch(a, 0, n, b[i]);
            printf(i == m - 1 ? "%d\n" : "%d ", k);
        }//for(i)
    }

    return 0;
}

  还可以直接利用STL中的upper_bound大大简化代码:

/* CF 600B Queries about less or equal elements --- 二分查找 */
#include <cstdio>
#include <algorithm>
using namespace std;

const int maxn = 200005;
int a[maxn];
int b[maxn];

int main()
{
    int n, m;
    while (scanf("%d%d", &n, &m) == 2){
        for (int i = 0; i < n; ++i){
            scanf("%d", a + i);
        }//for(i)
        sort(a, a + n);
        for (int i = 0; i < m; ++i){
            scanf("%d", b + i);
            int k = (upper_bound(a, a + n, b[i]) - a);
            printf(i == m - 1 ? "%d\n" : "%d ", k);
        }//for(i)
    }

    return 0;
}

时间: 2024-08-02 09:27:24

CF 600B Queries about less or equal elements --- 二分查找的相关文章

cidefirces Educational Codeforces Round 2 B Queries about less or equal elements

B. Queries about less or equal elements You are given two arrays of integers a and b. For each element of the second array bj you should find the number of elements in array a that are less than or equal to the value bj. Input The first line contains

CF600B Queries about less or equal elements 题解 二分

题目链接:http://codeforces.com/problemset/problem/600/B 题目大意: 给你一个长度为 \(n\) 的数组 \(a[]\) 和一个长度为 \(m\) 的数组 \(b[]\) . 对于数组 \(b[]\) 中的每一个元素 \(b_j\) ,你需要计算出 \(a[]\) 中有多少元素 \(a_i\) 是满足 \(a_i \le b_j\) 的. 解题思路: 本题涉及算法:二分. 需要先将数组 \(a[]\) 排序,然后对于每一个 \(b_j\) ,二分查找

CF #262 (DIV2) C . Present (二分答案)

output standard output Little beaver is a beginner programmer, so informatics is his favorite subject. Soon his informatics teacher is going to have a birthday and the beaver has decided to prepare a present for her. He planted n flowers in a row on

HDU 3280 Equal Sum Partitions(二分查找)

Equal Sum Partitions Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 551    Accepted Submission(s): 409 Problem Description An equal sum partition of a sequence of numbers is a grouping of the

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

原文链接https://www.cnblogs.com/zhouzhendong/p/9251420.html 题目传送门 - ARC100D 题意 给你一个长度为 $n$ 的数列,请切 $3$ 刀,形成 $4$ 个连续非空子序列,问这 $4$ 个非空子序列的各自的元素和 的极差为多少. $n\leq 2\times 10 ^5$ 题解 如果切一刀,那么问题就很简单,尽量选中间的就可以了. 可以二分一下在 $O(\log n)$ 的复杂度内解决. 于是本题可以先枚举一下中间那条线,然后对于两边转

2017-5-22-Train:Educational Codeforces Round 2

B. Queries about less or equal elements(二分) You are given two arrays of integers a and b. For each element of the second array b**j you should find the number of elements in array athat are less than or equal to the value b**j. Input The first line c

CF 628A --- Tennis Tournament --- 水题

CF 628A 题目大意:给定n,b,p,其中n为进行比赛的人数,b为每场进行比赛的每一位运动员需要的水的数量, p为整个赛程提供给每位运动员的毛巾数量, 每次在剩余的n人数中,挑选2^k=m(m <=n)个人进行比赛,剩余的n-m个人直接晋级, 直至只剩一人为止,问总共需要的水的数量和毛巾的数量 解题思路:毛巾数很简单: n*p即可 水的数量:1,2,4,8,16,32,64,128,256,512,提前打成一个表, 根据当前剩余的人数n在表中二分查找最大的小于等于n的数,结果即为本次进行比赛

CF 372B Counting Rectangles is Fun [dp+数据维护]

题意,给出一个n行m列的矩阵 里面元素是0或者1 给出q个询问 a,b,c,d 求(a,b)到(c,d)有多少个由0组成的矩形 我们定义 即为求(a,b)到(c,d)有多少个由0组成的矩形 对一个矩形来说 dp[a][b][c][d]=dp[a][b][c][d-1]+dp[a][b][c-1][d]-dp[a][b][c-1][d-1]+包含右下角(当前点)的矩形; 重点就在包含右下角(当前点c,d)的矩形,如何计算这个 我们可以暴力扫描,需要nm的复杂度,乘上原有复杂度,,,已经会超过时限

CF 689D - Friends and Subsequences

689D - Friends and Subsequences 题意: 大致跟之前题目一样,用ST表维护a[]区间max,b[]区间min,找出多少对(l,r)使得maxa(l,r) == minb(l,r) 切题的感觉很爽唉 同样而二分查找,找最小和最大下标满足条件 cf中%I64d, 一般是%lld 代码: #include<bits/stdc++.h> #define ll long long const int maxn=200010; int sta[maxn][18]; int s