The Incremental Triad of the Blue Bridge Cup Provincial Tournament

Title: Incrementing triples


Given three integer arrays
A = [A1, A2, ... AN], 
B = [B1, B2, ... BN], 
C = [C1, C2, ... CN] ,
please count how many triples (i, j, k) satisfy:
1. 1 <= i, j, k <= N  
2. Ai < Bj < Ck  


[Input format] 
The first line contains an integer N .
The second row contains n integers A1, A2, ..., AN.
The third row contains n integers B1, B2, ... BN.
The fourth row contains n integers C1, C2, ... CN.


For 30% of the data, 1 <= N <= 100  
For 60% of the data, 1 <= N <= 1000 
For 100% of the data, 1 <= N <= 100000 0 <= moxa, bismuth, times <= 100000 


[Output format]
An integer represents the answer


[Sample input]
3
1 1 1
2 2 2
3 3 3


[Sample output]

27 


Idea: At that time, it was a three-layer violence, but for an array size of 100,000 n^2, it would time out, not to mention the cubic

Next, I will sort out the next big guy's idea is O(n);

For sorted three arrays

The b array is the most special because an array is smaller than it, and a C array is larger than it

Then the sum is the number of elements in each b that are less than this element in the a array * in the c array greater than the number of this element in the b array

Cuihua ~ code above

#include<iostream>
#include<stdlib.h>
#include<stdio.h>
#include<algorithm>
#include<string.h>
using namespace std;
int n; // define the size
#define MAXN 100010//Define the maximum data size
long long sum=0;//Define the final result, because it may be n^2 so we can define it as long long
int a[MAXN];//Define the smallest of the three arrays
int b[MAXN];//Define the middle of the three
int c[MAXN];//Define the largest of the three
int t[MAXN];//Define the t array to store the number of elements in the array a that is less than an element in the b array
intmain()
{
    cin>>n;//input n
    for(int i=0; i<n; i++)
    {
        cin>>a[i];
    }
    for(int i=0; i<n; i++)
    {
        cin>>b[i];
    }
    for(int i=0; i<n; i++)
    {
        cin>>c[i];
    }
    //sort the three arrays from smallest to largest
    sort(a,a+n);
    sort(b,b+n);
    sort(c,c+n);
    memset(t,0,sizeof(t));//Initialize the t array to all 0
    int i=n-1;
    int j=n-1;//a and b are all traversed from back to front
    while(i>=0&&j>=0)//If the two arrays have not been traversed, here is a description. If i is <0 at the end, then it means that an element in the b array is smaller than that in the a array. If j First less than 0, then it means that an element in the a array is smaller than all elements in the b array
    {
        if(b[j]>a[i])//If the j-th element of the b array is larger than the i-th element of a, because it has been sorted and traversed from back to front, the front of the a array is all smaller than it
        {
            t[j]=i+1;//The number of less than b[j] in the a array is recorded in t[j]
            j--;//The b array moves forward and finds whether a smaller element of the b array is still smaller than a[i];
        }
        else // if not
        i--;//a array elements become smaller and smaller to see if they are smaller than b[j];
    }
    i=0;
     j=0;//b, c arrays are traversed from front to back
    while(j<n&&i<n)
    {
        if(c[j]>b[i])//If the jth element of c is greater than the ith element of b, then for a certain element b[i] in the b array, there is nj in the c array It may be because the element of the subscript larger than j in the c array must also be larger than b[i]
        {
            sum+=(t[i]*(nj));//A array smaller than b[i] can choose a total of t[i], and the number of c arrays larger than b[i] is nj, There are t[i]*(nj) possibilities for all elements of this b array
            i++;//Iterate over the elements of the next b array to see if they are still satisfied
        }
        Else//If it is less than, find the next larger of c to see if it is greater than this element of the b array
            j++;
    }
    cout<<sum<<endl;//Output the result
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326431826&siteId=291194637