原文:

给定两个分别由 nm 整数组成的数组arr 1【】arr 2【】,任务是分别从arr 1【】arr 2【】中找出随机选择这两个数字的概率,使得第一个选择的元素严格小于第二个选择的元素。

示例:

输入: arr1[] = {3,2,1,1},arr2[] = {1,2,5,2} 输出: 0.5 解释: 以下是从两个数组中选择数组元素的方法第一个数小于第二个数:

  1. 选择 arr1[0],有 1 种方法可以选择 arr2[]中的元素。
  2. 选择 arr1[1],在 arr2[]中有 1 种选择元素的方式。
  3. 选择 arr1[2],在 arr2[]中有 3 种选择元素的方式。
  4. 选择 arr1[3],在 arr 2[3]中有 3 种选择元素的方式

因此,总共有(3 3 1 1 = 8)种从满足条件的两个数组中选择元素的方式。因此,概率为(8/(4*4)) = 0.5。

输入: arr1[] = {5,2,6,1},arr2[] = {1,6,10,1 } t3】输出: 0.4375

天真方法:给定的问题可以基于以下观察来解决:

  • 想法是使用从数组arr 1【】中选择元素的概率为 1/n.
  • 现在假设 xarr 2【】中的元素数大于arr 1【】中的所选元素,那么从arr 2【】中选择一个这样的元素的概率是 x/m
  • 因此,选择两个元素使得第一个元素小于第二个所选元素的概率是arr 1【】中每个元素的 (1/n)*(x/m) 之和。

按照以下步骤解决问题:

  • 初始化一个变量,比如说设为 0 来存储结果概率。
  • 遍历给定的数组arr 1【】并执行以下步骤:
    • res 的值更新为 res = res/n*m
  • 完成以上步骤后,打印 res 的值作为合成概率。

下面是上述方法的实现:

c

// c   program for the above approach
#include 
using namespace std;
// function to find  probability
// such that x < y and x belongs to
// arr1[] and y belongs to arr2[]
double probability(vector arr1,vector arr2)
{
    // stores the length of arr1
    int n = arr1.size();
    // stores the length of arr2
    int m = arr2.size();
    // stores the result
    double res = 0;
    // traverse the arr1[]
    for (int i = 0; i < n; i  ) {
        // stores the count of
        // elements in arr2 that
        // are greater than arr[i]
        int y = 0;
        // traverse the arr2[]
        for (int j = 0; j < m; j  ) {
            // if arr2[j] is greater
            // than arr1[i]
            if (arr2[j] > arr1[i])
                y  ;
        }
        // increment res by y
        res  = y;
    }
    // update the value of res
    res = (double)res / (double)(n * m);
    // return resultant probability
    return res;
}
// driver code
int main()
{
    vector arr1 = { 5, 2, 6, 1 };
    vector arr2 = { 1, 6, 10, 1 };
    cout<

java 语言(一种计算机语言,尤用于创建网站)

// java program for the above approach
import java.util.*;
class gfg {
    // function to find  probability
    // such that x < y and x belongs to
    // arr1[] and y belongs to arr2[]
    static double probability(int[] arr1,
                              int[] arr2)
    {
        // stores the length of arr1
        int n = arr1.length;
        // stores the length of arr2
        int m = arr2.length;
        // stores the result
        double res = 0;
        // traverse the arr1[]
        for (int i = 0; i < n; i  ) {
            // stores the count of
            // elements in arr2 that
            // are greater than arr[i]
            int y = 0;
            // traverse the arr2[]
            for (int j = 0; j < m; j  ) {
                // if arr2[j] is greater
                // than arr1[i]
                if (arr2[j] > arr1[i])
                    y  ;
            }
            // increment res by y
            res  = y;
        }
        // update the value of res
        res = (double)res / (double)(n * m);
        // return resultant probability
        return res;
    }
    // driver code
    public static void main(string[] args)
    {
        int[] arr1 = { 5, 2, 6, 1 };
        int[] arr2 = { 1, 6, 10, 1 };
        system.out.println(
            probability(arr1, arr2));
    }
}

python 3

# python 3 program for the above approach
# function to find  probability
# such that x < y and x belongs to
# arr1[] and y belongs to arr2[]
def probability(arr1, arr2):
    # stores the length of arr1
    n = len(arr1)
    # stores the length of arr2
    m = len(arr2)
    # stores the result
    res = 0
    # traverse the arr1[]
    for i in range(n):
        # stores the count of
        # elements in arr2 that
        # are greater than arr[i]
        y = 0
        # traverse the arr2[]
        for j in range(m):
            # if arr2[j] is greater
            # than arr1[i]
            if (arr2[j] > arr1[i]):
                y  = 1
        # increment res by y
        res  = y
    # update the value of res
    res = res / (n * m)
    # return resultant probability
    return res
# driver code
if __name__ == "__main__":
    arr1 = [5, 2, 6, 1]
    arr2 = [1, 6, 10, 1]
    print(probability(arr1, arr2))
    # this code is contributed by ukasp.

c

//c# program for the above approach
using system;
class gfg {
    // function to find  probability
    // such that x < y and x belongs to
    // arr1[] and y belongs to arr2[]
    static double probability(int[] arr1, int[] arr2)
    {
        // stores the length of arr1
        int n = arr1.length;
        // stores the length of arr2
        int m = arr2.length;
        // stores the result
        double res = 0;
        // traverse the arr1[]
        for (int i = 0; i < n; i  ) {
            // stores the count of
            // elements in arr2 that
            // are greater than arr[i]
            int y = 0;
            // traverse the arr2[]
            for (int j = 0; j < m; j  ) {
                // if arr2[j] is greater
                // than arr1[i]
                if (arr2[j] > arr1[i])
                    y  ;
            }
            // increment res by y
            res  = y;
        }
        // update the value of res
        res = (double)res / (double)(n * m);
        // return resultant probability
        return res;
    }
    // driver code
    static void main()
    {
        int[] arr1 = { 5, 2, 6, 1 };
        int[] arr2 = { 1, 6, 10, 1 };
        console.writeline(probability(arr1, arr2));
    }
}
// this code is contributed by soumikmondal.

java 描述语言


output: 

0.4375

时间复杂度: o(n * m) 辅助空间: o(1)

高效途径:使用可以优化上述途径。按照以下步骤解决问题:

  • 初始化一个变量,说 res0 ,存储合成概率。
  • 遍历给定的数组arr 1【】并执行以下步骤:
    • res 的值更新为res = res/n * m
  • 完成以上步骤后,打印 res 作为合成概率。

下面是上述方法的实现:

c

// c   program for the above approach
#include 
using namespace std;
int countgreater(int* arr, int k);
// function to find  probability
// such that x < y and x belongs
// to arr1[] & y belongs to arr2[]
float probability(int* arr1,
                          int* arr2)
{
    // stores the length of arr1
    int n = 4;
    // stores the length of arr2
    int m = 4;
    // stores the result
    float res = 0;
    // sort the arr2[] in the
    // ascending order
    sort(arr2, arr2   m);
    // traverse the arr1[]
    for (int i = 0; i < n; i  ) {
        // stores the count of
        // elements in arr2 that
        // are greater than arr[i]
        int y = countgreater(
            arr2, arr1[i]);
        // increment res by y
        res  = y;
    }
    // update the resultant
    // probability
    res = res / (n * m);
    // return the result
    return res;
}
// function to return the count
// of elements from the array
// which are greater than k
int countgreater(int* arr,
                        int k)
{
    int n = 4;
    int l = 0;
    int r = n - 1;
    // stores the index of the
    // leftmost element from the
    // array which is at least k
    int leftgreater = n;
    // finds number of elements
    // greater than k
    while (l <= r) {
        int m = l   (r - l) / 2;
        // if mid element is at least
        // k, then update the value
        // of leftgreater and r
        if (arr[m] > k) {
            // update leftgreater
            leftgreater = m;
            // update r
            r = m - 1;
        }
        // if mid element is
        // at most k, then
        // update the value of l
        else
            l = m   1;
    }
    // return the count of
    // elements greater than k
    return (n - leftgreater);
}
// driver code
int main()
{
    int arr1[] = { 5, 2, 6, 1 };
    int arr2[] = { 1, 6, 10, 1 };
    cout << probability(arr1, arr2);
    return 0;
}
// this code is contributed by shubhamsingh10

java 语言(一种计算机语言,尤用于创建网站)

// java program for the above approach
import java.util.*;
class gfg {
    // function to find  probability
    // such that x < y and x belongs
    // to arr1[] & y belongs to arr2[]
    static double probability(int[] arr1,
                              int[] arr2)
    {
        // stores the length of arr1
        int n = arr1.length;
        // stores the length of arr2
        int m = arr2.length;
        // stores the result
        double res = 0;
        // sort the arr2[] in the
        // ascending order
        arrays.sort(arr2);
        // traverse the arr1[]
        for (int i = 0; i < n; i  ) {
            // stores the count of
            // elements in arr2 that
            // are greater than arr[i]
            int y = countgreater(
                arr2, arr1[i]);
            // increment res by y
            res  = y;
        }
        // update the resultant
        // probability
        res = (double)res / (double)(n * m);
        // return the result
        return res;
    }
    // function to return the count
    // of elements from the array
    // which are greater than k
    static int countgreater(int[] arr,
                            int k)
    {
        int n = arr.length;
        int l = 0;
        int r = n - 1;
        // stores the index of the
        // leftmost element from the
        // array which is at least k
        int leftgreater = n;
        // finds number of elements
        // greater than k
        while (l <= r) {
            int m = l   (r - l) / 2;
            // if mid element is at least
            // k, then update the value
            // of leftgreater and r
            if (arr[m] > k) {
                // update leftgreater
                leftgreater = m;
                // update r
                r = m - 1;
            }
            // if mid element is
            // at most k, then
            // update the value of l
            else
                l = m   1;
        }
        // return the count of
        // elements greater than k
        return (n - leftgreater);
    }
    // driver code
    public static void main(string[] args)
    {
        int[] arr1 = { 5, 2, 6, 1 };
        int[] arr2 = { 1, 6, 10, 1 };
        system.out.println(
            probability(arr1, arr2));
    }
}

python 3

# python3 program for the above approach
# function to find  probability
# such that x < y and x belongs
# to arr1[] & y belongs to arr2[]
def probability(arr1, arr2):
    # stores the length of arr1
    n = len(arr1)
    # stores the length of arr2
    m = len(arr2)
    # stores the result
    res = 0
    # sort the arr2[] in the
    # ascending order
    arr2.sort()
    # traverse the arr1[]
    for i in range(n):
        # stores the count of
        # elements in arr2 that
        # are greater than arr[i]
        y = countgreater(arr2, arr1[i])
        # increment res by y
        res  = y
    # update the resultant
    # probability
    res /= (n * m)
    # return the result
    return res
# function to return the count
# of elements from the array
# which are greater than k
def countgreater(arr, k):
    n = len(arr)
    l = 0
    r = n - 1
    # stores the index of the
    # leftmost element from the
    # array which is at least k
    leftgreater = n
    # finds number of elements
    # greater than k
    while l <= r:
        m = (l   r) // 2
        # if mid element is at least
        # k, then update the value
        # of leftgreater and r
        if (arr[m] > k):
            # update leftgreater
            leftgreater = m
            # update r
            r = m - 1
        # if mid element is
        # at most k, then
        # update the value of l
        else:
            l = m   1
    # return the count of
    # elements greater than k
    return n - leftgreater
# driver code
if __name__ == '__main__':
    arr1 = [ 5, 2, 6, 1 ]
    arr2 = [ 1, 6, 10, 1 ]
    print(probability(arr1, arr2))
# this code is contributed by muskankalra1

c

// c# program for the above approach
using system;
class gfg {
   // function to find  probability
    // such that x < y and x belongs
    // to arr1[] & y belongs to arr2[]
    static double probability(int[] arr1,
                              int[] arr2)
    {
        // stores the length of arr1
        int n = arr1.length;
        // stores the length of arr2
        int m = arr2.length;
        // stores the result
        double res = 0;
        // sort the arr2[] in the
        // ascending order
        array.sort(arr2);
        // traverse the arr1[]
        for (int i = 0; i < n; i  ) {
            // stores the count of
            // elements in arr2 that
            // are greater than arr[i]
            int y = countgreater(
                arr2, arr1[i]);
            // increment res by y
            res  = y;
        }
        // update the resultant
        // probability
        res = (double)res / (double)(n * m);
        // return the result
        return res;
    }
    // function to return the count
    // of elements from the array
    // which are greater than k
    static int countgreater(int[] arr,
                            int k)
    {
        int n = arr.length;
        int l = 0;
        int r = n - 1;
        // stores the index of the
        // leftmost element from the
        // array which is at least k
        int leftgreater = n;
        // finds number of elements
        // greater than k
        while (l <= r) {
            int m = l   (r - l) / 2;
            // if mid element is at least
            // k, then update the value
            // of leftgreater and r
            if (arr[m] > k) {
                // update leftgreater
                leftgreater = m;
                // update r
                r = m - 1;
            }
            // if mid element is
            // at most k, then
            // update the value of l
            else
                l = m   1;
        }
        // return the count of
        // elements greater than k
        return (n - leftgreater);
    }
    // driver code
    public static void main()
    {
       int[] arr1 = { 5, 2, 6, 1 };
        int[] arr2 = { 1, 6, 10, 1 };
        console.write(
            probability(arr1, arr2));
    }
}
// this code is contributed by sanjoy_62.

java 描述语言


output: 

0.4375

时间复杂度: o(n * log m) 辅助空间: o(1)