upper_bound()与lower_bound()使用方法
来源:互联网 发布:中国源码 编辑:程序博客网 时间:2024/05/04 14:14
upper_bound()与lower_bound()使用方法
#include<iostream>#include <algorithm> //必须包含的头文件using namespace std;int main(){ int point[10] = {2,3,7,7,8}; int tmp = upper_bound(point, point + 5, 7) -point; //按从小到大,7最多能插入数组point的哪个位置 printf("%d\n",tmp); tmp = lower_bound(point, point + 5, 7) -point; //按从小到大,7最少能插入数组point的哪个位置 printf("%d\n",tmp); return 0;}
output:
4
2
lower_bound:
返回>=对象的第一个位置(位置就是数组存储时的下标),lower_bound(2)=3, lower_bound(3)=3
目标对象存在即为目标对象的位置,不存在则为后一个位置.
upper_bound:
返回>对象的第一个位置, upper_bound(2)=3,upper_bound(3)=4
无论是否存在都为后一个位置.
hdu 5101 select (upper_bound()的应用)
题意: 在多个班级里找出两个队友,且两个队友的IQ 的和大于自己,所找的两个队友不能在同一个班级。
思路: 要逆向思维 才不超时。
对于一个单调长的序列,可以很容易地统计两数之和大于K的对数(枚举第一个数,二分找到满足条件的最小的数的位置,然后它和它后面的数都是满足条件的)
故对于每个班内,从小到大排序,统计。
然后对于所有人,从小到大排序,统计。
后者统计值减去前者统计值即为答案。
#include<iostream>#include<cstdio>#include<cstring>#include<cmath># include<algorithm>using namespace std;int VV[100005];int v[105];int main(){ int t; cin>>t; while(t--) { int m,n,k; int i,j; long long sum1=0,sum=0,ss=0; scanf("%d%d",&n,&k); for(i=0;i<n;i++) { scanf("%d",&m); for(j=0;j<m;j++) { scanf("%d",&v[j]); VV[ss++]=v[j];} sort(v,v+m); for(j=0;j<m;j++) sum+=m-(upper_bound(v+j,v+m,k-v[j])-v); } sort(VV,VV+ss); for(j=0;j<ss;j++) sum1+=ss-(upper_bound(VV+j,VV+ss,k-VV[j])-VV); printf("%lld\n",sum1-sum); } return 0;}
hdu 5199 Gunner
#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>#include<string>#include<cmath>#include<set>using namespace std;const int maxn=1000000+5;int vis[maxn];int A[maxn];int n,m,i,j;int By(int s){ int l=0; int r=n; int mid; while(l<r) { mid=l+(r-l)/2; if(A[mid]<s) l=mid+1; else if(A[mid]>s) r=mid; else return mid; } return -1;}int main(){ #ifndef ONLINE_JUDGE freopen("in.cpp","r",stdin); #endif while(scanf("%d%d",&n,&m)!=EOF) { memset(vis,0,sizeof(vis)); for(i=0;i<n;i++) scanf("%d",&A[i]); sort(A,A+n); while(m--) { int s; int cnt=0; scanf("%d",&s); int r= upper_bound(A, A + n, s) -A; if(!vis[r]) { int l= lower_bound(A, A + n, s) -A; cnt=r-l; vis[r]=1; } printf("%d\n",cnt); } } return 0;}
1 0
- upper_bound()与lower_bound()使用方法
- upper_bound() 与 lower_bound() 使用方法
- upper_bound()与lower_bound()使用方法
- upper_bound()与lower_bound()使用方法
- upper_bound()与lower_bound()使用方法
- upper_bound()与lower_bound()使用方法
- (转)upper_bound()与lower_bound()使用方法
- STL 二分法 upper_bound()与lower_bound()使用方法
- lower_bound/upper_bound使用方法
- upper_bound()与lower_bound…
- lower_bound与upper_bound
- C++ lower_bound与upper_bound
- lower_bound 与 upper_bound
- lower_bound()与upper_bound()
- lower_bound与upper_bound
- lower_bound与upper_bound
- STL lower_bound与upper_bound
- lower_bound()与upper_bound()
- unity学习demo
- Error handling and Go go 优雅的错误处理
- 写四个线程,二个加1,二个对一个变量减一
- JavaScript事件汇总
- mysql---修改字符集
- upper_bound()与lower_bound()使用方法
- hdu 1588 - Gauss Fibonacci
- 一个JavaScript数据筛选器(三)——格式化筛选器
- 简单的JAVA语言
- IT族室内锻炼身体的小妙招
- hdu 1711(KMP的简单应用)
- 【前端笔记】项目中运用到的前端操作
- poj 1613 Cave Raider
- Paypal以及SandBox虚拟环境的使用