有三个数组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
}
2、lower_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
}