Snuke Festival
时间限制: 1 Sec 内存限制: 128 MB
题目描述
The season for Snuke Festival has come again this year. First of all, Ringo will perform a ritual to summon Snuke. For the ritual, he needs an altar, which consists of three parts, one in each of the three categories: upper, middle and lower.
He has N parts for each of the three categories. The size of the i-th upper part is Ai, the size of the i-th middle part is Bi, and the size of the i-th lower part is Ci.
To build an altar, the size of the middle part must be strictly greater than that of the upper part, and the size of the lower part must be strictly greater than that of the middle part. On the other hand, any three parts that satisfy these conditions can be combined to form an altar.
How many different altars can Ringo build? Here, two altars are considered different when at least one of the three parts used is different.
Constraints
1≤N≤105
1≤Ai≤109(1≤i≤N)
1≤Bi≤109(1≤i≤N)
1≤Ci≤109(1≤i≤N)
All input values are integers.
输入
Input is given from Standard Input in the following format:
N
A1 … AN
B1 … BN
C1 … CN
输出
Print the number of different altars that Ringo can build.
样例输入
2
1 5
2 4
3 6
样例输出
3
提示
The following three altars can be built:
Upper: 1-st part, Middle: 1-st part, Lower: 1-st part
Upper: 1-st part, Middle: 1-st part, Lower: 2-nd part
Upper: 1-st part, Middle: 2-nd part, Lower: 2-nd part
题目大意:A,B,C三组数,每组取一个要求A[i]<B[i]<C[i],有多少种取法?
如果从第一行开始暴力查找O(n^3)肯定超时
只扫描第二行,找出第一行比它小的数的个数,第三行比它大的数的个数,相乘后累加即得结果
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int N = 100005;
int a[N], b[N], c[N];
int main ()
{
int n;
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]);
sort(a, a+n); sort(b, b+n);sort(c, c+n);
LL ans = 0;
for(int i = 0, j = 0, k = 0; i < n; i++)
{
while(b[i]>a[j] && j <= n-1) j++; ///找第一行比第二行小的个数
while(b[i]>=c[k] && k <= n-1) k++; /// 从前往后找
ans += (LL)j * (n-k);
}
printf("%lld\n", ans);
return 0;
}
/*
2
1 5
2 4
3 6
*/