关于lower_bound( )和upper_bound( )的常见用法
ower_bound( )和upper_bound( )都是利用二分查找的方法在一个排好序的数组中进行查找的。
在从小到大的排序数组中,
lower_bound( begin,end,num):从数组的begin位置到end-1位置二分查找第一个大于或等于num的数字,找到返回该数字的地址,不存在则返回end。通过返回的地址减去起始地址begin,得到找到数字在数组中的下标。
upper_bound( begin,end,num):从数组的begin位置到end-1位置二分查找第一个大于num的数字,找到返回该数字的地址,不存在则返回end。通过返回的地址减去起始地址begin,得到找到数字在数组中的下标。
在从大到小的排序数组中,重载lower_bound()和upper_bound()
lower_bound( begin,end,num,greater() ):从数组的begin位置到end-1位置二分查找第一个小于或等于num的数字,找到返回该数字的地址,不存在则返回end。通过返回的地址减去起始地址begin,得到找到数字在数组中的下标。
upper_bound( begin,end,num,greater() ):从数组的begin位置到end-1位置二分查找第一个小于num的数字,找到返回该数字的地址,不存在则返回end。通过返回的地址减去起始地址begin,得到找到数字在数组中的下标。
给定三个整数数组
A=[A1,A2,…AN],
B=[B1,B2,…BN],
C=[C1,C2,…CN],
请你统计有多少个三元组 (i,j,k) 满足:
1≤i,j,k≤N
Ai<Bj<Ck
输入格式
第一行包含一个整数 N。
第二行包含 N 个整数 A1,A2,…AN。
第三行包含 N 个整数 B1,B2,…BN。
第四行包含 N 个整数 C1,C2,…CN。
输出格式
一个整数表示答案。
数据范围
1≤N≤105,
0≤Ai,Bi,Ci≤105
输入样例:
3
1 1 1
2 2 2
3 3 3
输出样例:
27
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;typedef long long lld;
const int N = 100005;
int a[N], b[N], c[N];
int n;
lld sum;int main()
{scanf("%d", &n);for (int i = 1; i <= n; i++)scanf("%d", &a[i]);for (int i = 1; i <= n; i++)scanf("%d", &b[i]);for (int i = 1; i <= n; i++)scanf("%d", &c[i]);//由于二分的前提是单调序列 所以预先对a b c排序 直接sortsort(a + 1, a + 1 + n);sort(b + 1, b + 1 + n);sort(c + 1, c + 1 + n);for (int i = 1; i <= n; i++){//直接用STL中的两个二分函数解决lld x = (lower_bound(a + 1, a + 1 + n, b[i]) - a) - 1; //在数组a中找比b[i]小的数lld y = n - (upper_bound(c + 1, c + 1 + n, b[i]) - c) + 1; //在数组c中找比b[i]大的数sum += x * y;}printf("%lld", sum);return 0;
}作者:fchunchun
链接:https://www.acwing.com/solution/content/7689/
来源:AcWing
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
关于lower_bound( )和upper_bound( )的常见用法相关推荐
- upperbound找不到_关于lower_bound( )和upper_bound( )的常见用法
头文件:#include lower_bound( )和upper_bound( )都是利用二分查找的方法在一个排好序的数组中进行查找的. 在从小到大的排序数组中, lower_bound( begi ...
- lower_bound( )和upper_bound( )的常见用法
ower_bound( )和upper_bound( )都是利用二分查找的方法在一个排好序的数组中进行查找的. 在从小到大的排序数组中, lower_bound( begin,end,num):从数组 ...
- lower_bound( )和upper_bound( )的基本用法
lower_bound( )和upper_bound( )是C++ STL模板库中的函数,其作用可以看作是一个简单的二分模板. 基本使用方法: lower_bound: 参数:lower_bound( ...
- 【ACM】Uva 1152 (4 Values whose Sum is 0) 二分查找lower_bound() 和upper_bound()的使用
[问题描述] The SUM problem can be formulated as follows: given four lists A, B, C, D of integer values, ...
- lower_bound 和 upper_bound的区别
转载至某个比较好看的博客:关于lower_bound( )和upper_bound( )的常见用法 下面的内容: lower_bound( )和upper_bound( )都是利用二分查找的方法在一个 ...
- lower_bound和 upper_bound 用法(STL)
lower_bound和 upper_bound的头文件是#include<algorithm> lower_bound 返回第一个大于等于 x 的数的地址/迭代器 upper_bound ...
- lower_bound和upper_bound的用法
二分查找算法可以解决最简单的二分查找问题:a数组单调递增,并且其中没有重复的数值.我们遇到的实际问题可能就没有这么简单,可能会有重复的数值.比如a数组里有3个5.这时我们查找5就有一个问题:到底返回哪 ...
- lower_bound( )和upper_bound( )的返回值及用法
lower_bound( )和upper_bound( )都是利用二分查找的方法在一个排好序的数组中进行查找的 在从小到大的排序数组中, lower_bound( begin,end,num):从数组 ...
- STL剖析(二):容器底层数据结构及常见用法
一.概述 本文主要聚焦于STL容器,STL完整的容器分类体系如下所示,下文将逐一对各个容器底层的数据结构以及常见用法进行介绍. 测试环境:Ubuntu 22.04 g++ 11.3.0 二.顺序容器 ...
最新文章
- 读书笔记 effective c++ Item 5 了解c++默认生成并调用的函数
- 按钮右对齐_9张图,学会Excel中的对齐技巧
- Python基础day02【if结构、if elif 结构、while循环、for循环、Break和continue、Debug 调试、三目运算、循环 else 结构】
- Linux和windows中的换行符差异问题 LINUX的换行符在Windows记事本打开不换行或出现黑点
- 02 李俊杰 20160221-S1笔试
- web页面制作-环游记(一)
- 构建一个可靠的分布式计数器--memcached之incr/decr操作实战分析
- 【语义相似度】ESIM:语义相似度领域小模型的尊严
- linux 多线程(一)条件变量
- python 人工智能项目代码_7个Python实战项目代码,让你分分钟晋级大神!
- 云计算与云原生 — ETCD 数据库完全解析
- c语言中tmod,单片机C语言请教——有关TMOD
- python numpy逆_python-使用numpy的矩阵逆
- ios中Date.prase()兼容问题
- 【山外笔记-计算机网络·第7版】第10章:计算机网络重要概念
- 网络编程“惊群“问题
- 案例 | 沃尔玛 x 腾讯云 Serverless 应用实践,全力保障消费者购物体验
- 神经网络算法应用场景,视觉算法具体是做什么
- 10个Scratch热门作品(2)
- 翻转组织——通用医疗敏捷转型案例