Snuke Festival(数学、遍历)

问题 J: Snuke Festival
时间限制: 1 Sec 内存限制: 128 MB
提交: 904 解决: 201
[提交] [状态] [讨论版] [命题人:admin]
题目描述

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,b,c中的元素排列组合。但是数据范围是10^5,三层循环或两层循环都会超时。所以采用折中的思路,仅遍历中间层b组的数据,遍历时候顺便找出a组在bi情况下符合的数据的个数和c中符合的数据个数进行排列组合。遍历完整个b数组的数据加和之后的结果就是最终结果。在这里为了方便,在c组数据查找的时候采用对立事件的解法。

#include<bits/stdc++.h>
#define LL long long
using namespace std;
int main()
{
    LL n; cin>>n;
    int a[n],b[n],c[n];
    for(int i=1;i<=n;i++)
        cin>>a[i];
    for(int i=1;i<=n;i++)
        cin>>b[i];
    for(int i=1;i<=n;i++)
        cin>>c[i];
    sort(a+1,a+n+1);
    sort(b+1,b+n+1);
    sort(c+1,c+n+1);
    LL cnt=0;//最终结果可能会超过int型的数据
    LL ja=0,jb=0,jc=0;
    for(int i=1;i<=n;i++)
    {
         while(a[ja+1]<b[i]&&ja<n)
            ja++;
         while(c[jc+1]<=b[i]&&jc<n)
            jc++;
        //printf("%lld %lld\n",ja,n-jc);
         cnt+=ja*(n-jc);
    }
    cout<<cnt<<endl;
    return 0;
}

猜你喜欢

转载自blog.csdn.net/a17865569022/article/details/81393182