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$ 刀, ...
随机推荐
- c++ 文件读写模板
#include <fstream> using namespace std; int main() { ifstream fin("in.txt"); ofstrea ...
- 使用Eclipse创建maven项目
前提:Eclipse中安装了maven插件,或者Eclipse版本在Mars以上(自集成maven) 1.new project --maven project 2.默认点击next 3.选择构建类型 ...
- Shell重定向文件描述符
#!/bin/bash 最近在看shell,各种困惑,不过解决困惑的感觉还是很不错的.废话少说,linux中使用文件描述符来标识每个文件对象.文件描述符为一个非负整数,可以唯一标识会话中打开 ...
- .net Web开发学习日志 —C/S和B/S结构区别
查看到<C/S和B/S结构区别整理> B/S结构与C/S结构都是有各自的优缺点: 前者无需安装,只要有浏览器即可,随时随地查询相关的业务,业务扩展强,维护强,共享强.在跨浏览器较难,响应速 ...
- Chinese culture
文房四宝 笔墨纸砚是中国古代文人书房中必备的宝贝,被称为“文房四宝”.用笔墨书写绘画在 中国可追溯到五千年前.秦(前221---前206)时已用不同硬度的毛和竹管制笔:汉代(前206—公元220) ...
- (41) Aeroo 模板设计基础教程
1. 理论基础 注:我采用libreoffice5.2设计讲解 1.1. 定义模板指令 模板指令的语法和Genshi 模板语言相兼容,可以用Libreoffice( Write, Ca ...
- Windows—JDK安装与环境变量配置
本文介绍JDK的安装与环境变量配置. 工具/原料 JDK1.8.0_65 WIN7 32bit jdk-8u65-windows-i586.exe 方法/步骤 安装JDK 选择安装目录 安装过程中会 ...
- [转]CSS,font-family,常用网页字体
http://www.zreading.cn/ican/2014/10/css-font-family/ CSS,font-family,好看常用的中文字体 2014-10-14 例1(小米米官网): ...
- 编写HTML表单提交并接受数据信息(获取用户通过表单提交的内容)
MyInfoForm.java package com.fxl; import java.io.IOException; import java.io.PrintWriter; import java ...
- Struts2中实现Web项目的初始化工作
Struts2中实现Web项目的初始化工作 注:通常web系统在启动时需要做一些初始化的工作,比如初始化系统全局变量,加载自定义配置文件,启动定时任务等. 一.在Struts中实现系统的初始化工作 ...