2018 Lanqiao Cup Increasing Triples Sort Sort

Blue Bridge Cup Incremental Triad

This is the sixth question of the 2018 Blue Bridge Cup C Language Provincial Competition Group B

Question:
Given three integer arrays
A = [A1, A2,… AN],
B = [B1, B2,… BN],
C = [C1, C2,… CN],
please count how many triples there are (i, j, k) satisfies:

  1. 1 <= i, j, k <= N
  2. Ai < Bj < Ck

Input The
first line contains an integer N.
The second line contains N integers A1, A2,… AN.
The third line contains N integers B1, B2,… BN.
The fourth line contains N integers C1, C2,… CN.
1 <= N <= 100000 0 <= Ai, Bi, Ci <= 100000

Output
an integer for the answer

Sample input
3
1 1 1
2 2 2
3 3 3

Sample output
27

OJ link

Idea : First use sort to sort the three arrays. Then traverse the B array, use the lower_bound function to find the number of elements in the A array that is less than the current element of the B array, use the upper_bound function to find the number of the C array greater than the current element of the B array, and multiply the two to calculate the current element of the B array. The number of solutions, the total solution can be obtained by traversing the B array once.
PS : Remember to use long long, it will burst if you use int.

AC code :

#include<bits/stdc++.h>
using namespace std;

int a[100001],b[100001],c[100001];

int main()
{
    
    
	int n;
	scanf("%d",&n);
	for(int now=0;now<n;now++)
	{
    
    
		scanf("%d",&a[now]);
	}
	for(int now=0;now<n;now++)
	{
    
    
		scanf("%d",&b[now]);
	}
	for(int now=0;now<n;now++)
	{
    
    
		scanf("%d",&c[now]);
	}
	
	sort(a,a+n);//排序
	sort(b,b+n);
	sort(c,c+n);
	
	long long ans=0;//记得用long long
	
	for(int now=0;now<n;now++)//b为中间值 遍历
	{
    
    
		long long s1=lower_bound(a,a+n,b[now])-a;//获取a中小于b【now】的个数
		long long s2=n-(upper_bound(c,c+n,b[now])-c);//获取c中大于b【now】的个数
		ans+=s1*s2;
	}
	
	cout<<ans;
	
	return 0;
	
}

Guess you like

Origin blog.csdn.net/qq_45698148/article/details/108213843