lower_bound

有三个数组A,B,C,每个数组中有n个数,你可以从每个数组中找一个数,使得Ai<Bj<Ck ,(1<=I,j,k<=n)(1<=n<=100000,1<=Ai,Bj,Ck<=1000000),求最多可以组出多少三元组

Input

有多组输入 
第一行输入n 
接下来三行输入A,B,C三个数组,每个数组n个数 

Output

每行一个整数,表示最多有多少三元组

Sample Input

3
1 1 1
2 2 2
3 3 3
3
1 2 3
1 2 3
1 2 3

Sample Output

27
1

Hint

/*很多二分函数可以直接调用,这个真的很方便,具体使用可以去参考网上的博客哦*/ 
#include <iostream>
#include <algorithm>
using namespace std;
const int Max = 100005;
int n;
int a[Max], b[Max], c[Max];
bool cmp(int x, int y)
{
	return x > y;
}
int main()
{
	while(~scanf("%d",&n))
	{
		for(int i = 0; i < n; i++) scanf("%d",&a[i]);
		for(int i = 0; i < n; i++) scanf("%d",&b[i]);
		for(int i = 0; i < n; i++) scanf("%d",&c[i]);	
		long long ans = 0;
		sort(a,a+n);sort(c,c+n,cmp);
		for(int i = 0; i < n; i++)
		{
			cout<<ans<<endl; 
			//cout << lower_bound(a,a+n,b[i]) - a << " " << lower_bound(c,c+n,b[i],greater<int>()) - c << endl;
			ans = ans + (long long)(lower_bound(a,a+n,b[i]) - a) * (lower_bound(c,c+n,b[i],greater<int>()) - c);
		}                             //lower_bound(a,a+n,b[i]) - a寻找第一个大于等于b[i]的数的位置(因为数组是从小到大排的,所以他的位置就是比b[i]小的个数) 
		cout << ans << endl;           // lower_bound(c,c+n,b[i],greater<int>())寻找第一个小于等于 b[i]的数的位置(因为数组从小到大排的,所以他的位置的数就是比b[i]大的个数) 
	}
	return 0;
 } 

下面是其他例子:

 1、 upper_bound()前闭后开区间进行二分查找查找的关键字的上界 返回容器中第一个值【大于】val的元素的iterator位置。

使用方法( 扩展使用:nlogn的(最长不降子序列),upper_bound() )

    头文件:#include <algorithm>

    使用前提:非降序列

 

#include<iostream>

#include<algorithm>

using namespace std;

     

int main(){

    int number[10]={10,11,12,13,14,15,16,17,18,19};

    int pos=upper_bound(number,number+10,10)-number;    

    cout<<pos<<endl;//pos=1;

    pos=upper_bound(number,number+10,14)-number;

    cout<<pos<<endl;//pos=5

    pos=upper_bound(number,number+10,19)-number;

    cout<<pos<<endl;//pos=10    

}

 

2lower_bound()前闭后开区间进行二分查找 返回容器中第一个值【大于或等于】val的元素的iterator位置。

    头文件:#include <algorithm>

    使用前提:非降序列

 

#include<iostream>

#include<algorithm>

using namespace std;

     

int main(){

    int number[10]={10,11,12,13,14,15,16,17,18,19};

    int pos=lower_bound(number,number+10,10)-number;    

    cout<<pos<<endl;//pos=0;

    pos=lower_bound(number,number+10,14)-number;

    cout<<pos<<endl;//pos=4

    pos=lower_bound(number,number+10,19)-number;

    cout<<pos<<endl;//pos=9

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值