2018.3.4【 AtCoder - 3620 】解题报告(前缀和,dp)Upc J: Snuke Festival

C - Snuke Festival


Time limit : 2sec / Memory limit : 256MB

Score : 300 points

Problem Statement

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≤iN)
  • 1≤Bi≤109(1≤iN)
  • 1≤Ci≤109(1≤iN)
  • All input values are integers.

Input

Input is given from Standard Input in the following format:

N
A1 … AN
B1 … BN
C1 … CN

Output

Print the number of different altars that Ringo can build.


Sample Input 1

Copy

2
1 5
2 4
3 6

Sample Output 1

Copy

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

Sample Input 2

Copy

3
1 1 1
2 2 2
3 3 3

Sample Output 2

Copy

27

Sample Input 3

Copy

6
3 14 159 2 6 53
58 9 79 323 84 6
2643 383 2 79 50 288

Sample Output 3

Copy

87

【题目大意】

搭祭坛,给出n个上层,中层,下层的尺寸。搭建要求由上到下尺寸严格递增。问可能的情况有多少(同层同尺寸的编号算作不同的,一开始以为需要去重,想了半天去重是去了,但是WA,实在想不出为什么,最后看了数据才知道,没有去重这一说,只要编号不同就算作一种情况);

题解:

我先用递归交了一下结果TEL,想想也是,1e5递归一下要很多次,显然需要优化一下,于是就用前缀和(真是没想到,没想到,以前用过一次求矩阵的和,用过前缀和),因为前缀和只需要一次就能将过程寄存,不需要重复循环计算了。

#include<bits/stdc++.h>
using namespace std;
#define rep(i,a,b)  for(int i=a;i<=b;i++)
int n,m;
typedef long long ll;
ll sum;
ll e[5][100005];
//void altars(int step)
//{
//    if(step==4)
//    {
//        //if(vis[])
//        sum++;
//        return ;
//    }
//    rep(i,1,n)
//    {
//       if(e[step][i]>b[step-1])
//       {
//           b[step]=e[step][i];
//           altars(step+1);
//           b[step]=0;
//           //int t=i;
////        while(e[step][i]==e[step][i+1]&&i<=n)
////                i++;
//       }
//    }
//    //return ;
//}
ll a[100005];//表示编号为i的中层所满足的的上层的个数;
int main()
{
    cin>>n;
    rep(i,1,3)
    rep(j,1,n){
    cin>>e[i][j];
    }
    rep(i,1,3)
    sort(e[i]+1,e[i]+n+1);
    for(int i=1,j=1;j<=n;j++)
    {
        while(e[1][i]<e[2][j]&&i<=n) 
            i++;
        a[j]=i-1+a[j-1];//这里不加用下面的rep循环也可以的,因为已经将全部的数进行从小到大的
    }//排列了,所以满足前a[j-1]的一定是满足a[j]的,所以直接加上;
//    rep(i,1,n-1)
//    a[i+1]+=a[i];
    for(int i=1,j=1;j<=n;j++)
    {
        while(e[2][i]<e[3][j]&&i<=n) i++;
        sum+=a[i-1];
    }
    cout<<sum<<endl;
    return 0;
}

猜你喜欢

转载自blog.csdn.net/c___c18/article/details/81391363