-
Notifications
You must be signed in to change notification settings - Fork 0
/
Number_of_pairs.cpp
66 lines (51 loc) · 1.09 KB
/
Number_of_pairs.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#include<bits/stdc++.h>
using namespace std;
// } Driver Code Ends
// Function to count number of pairs such that x^y is greater than y^x
// X[], Y[]: input arrau
// m, n: size of arrays X[] and Y[] respectively
long long countUtil(int x, int NoOfY[], int Y[], int n)
{
long long a = 0;
if(x == 0) return 0;
if(x == 1) return NoOfY[0];
a += NoOfY[0] + NoOfY[1];
int* idx = upper_bound(Y,Y+n,x);
a += (Y+n - idx);
if(x == 2) a -= (NoOfY[3] + NoOfY[4]);
if(x == 3) a += NoOfY[2];
return a;
}
long long countPairs(int X[], int Y[], int m, int n)
{
sort(Y, Y + n);
long long ans = 0;
int NoOfY[5]{0};
for (int i = 0; i < n; ++i)
if(Y[i] < 5)
NoOfY[Y[i]]++;
for (int i = 0; i < m; ++i)
ans += countUtil(X[i], NoOfY, Y, n);
return ans;
}
// { Driver Code Starts.
int main()
{
int T;
cin>>T;
while(T--)
{
int M,N;
cin>>M>>N;
int i,a[M],b[N];
for(i=0;i<M;i++)
{
cin>>a[i];
}
for(i=0;i<N;i++)
{
cin>>b[i];
}
cout<<countPairs(a, b, M, N)<<endl;
}
} // } Driver Code Ends