📜  可以包含给定点的最大段数

📅  最后修改于: 2021-06-26 15:39:53             🧑  作者: Mango

给定数组arr [],其中包含N个整数以及两个整数XY。考虑N个线段,其中每个线段的起点和终点分别为arr [i] – Xarr [i] +Y
给定另一个数组b []个,共M个点。任务是将这些点分配给段,以使已分配点的段数最大。请注意,一个点最多可以分配给1个线段。
例子:

方法:对两个输入数组进行排序。现在,对于每个段,我们尝试为其分配可能的第一个未分配点。如果当前线段在当前点之前结束,则意味着我们将无法为其分配任何点,因为其前面的所有点都大于当前点,并且线段已经结束。
下面是上述方法的实现:

C++
// C++ implementation of the approach
#include 
using namespace std;
 
// Function to return the maximum number of segments
int countPoints(int n, int m, vector a,
                vector b, int x, int y)
{
    // Sort both the vectors
    sort(a.begin(), a.end());
    sort(b.begin(), b.end());
 
    // Initially pointing to the first element of b[]
    int j = 0;
    int count = 0;
    for (int i = 0; i < n; i++) {
 
        // Try to find a match in b[]
        while (j < m) {
 
            // The segment ends before b[j]
            if (a[i] + y < b[j])
                break;
 
            // The point lies within the segment
            if (b[j] >= a[i] - x && b[j] <= a[i] + y) {
                count++;
                j++;
                break;
            }
 
            // The segment starts after b[j]
            else
                j++;
        }
    }
 
    // Return the required count
    return count;
}
 
// Driver code
int main()
{
    int x = 1, y = 4;
    vector a = { 1, 5 };
    int n = a.size();
    vector b = { 1, 1, 2 };
    int m = a.size();
    cout << countPoints(n, m, a, b, x, y);
 
   return 0;
}


Java
// Java implementation of the approach
import java.util.*;
 
class GFG
{
 
// Function to return the
// maximum number of segments
static int countPoints(int n, int m, int a[],
                        int[] b, int x, int y)
{
    // Sort both the vectors
    Arrays.sort(a);
    Arrays.sort(b);
 
    // Initially pointing to the first element of b[]
    int j = 0;
    int count = 0;
    for (int i = 0; i < n; i++)
    {
 
        // Try to find a match in b[]
        while (j < m)
        {
 
            // The segment ends before b[j]
            if (a[i] + y < b[j])
                break;
 
            // The point lies within the segment
            if (b[j] >= a[i] - x && b[j] <= a[i] + y)
            {
                count++;
                j++;
                break;
            }
 
            // The segment starts after b[j]
            else
                j++;
        }
    }
 
    // Return the required count
    return count;
}
 
// Driver code
public static void main(String args[])
{
    int x = 1, y = 4;
    int[] a = { 1, 5 };
    int n = a.length;
    int[] b = { 1, 1, 2 };
    int m = a.length;
    System.out.println(countPoints(n, m, a, b, x, y));
}
}
 
// This code is contributed by
// Surendra_Gangwar


Python3
# Python3 implementation of the approach
 
# Function to return the maximum
# number of segments
def countPoints(n, m, a, b, x, y):
 
    # Sort both the vectors
    a.sort()
    b.sort()
 
    # Initially pointing to the first
    # element of b[]
    j, count = 0, 0
    for i in range(0, n):
 
        # Try to find a match in b[]
        while j < m:
 
            # The segment ends before b[j]
            if a[i] + y < b[j]:
                break
 
            # The point lies within the segment
            if (b[j] >= a[i] - x and
                b[j] <= a[i] + y):
                count += 1
                j += 1
                break
 
            # The segment starts after b[j]
            else:
                j += 1
 
    # Return the required count
    return count
 
# Driver code
if __name__ == "__main__":
 
    x, y = 1, 4
    a = [1, 5]
    n = len(a)
    b = [1, 1, 2]
    m = len(b)
    print(countPoints(n, m, a, b, x, y))
     
# This code is contributed by Rituraj Jain


C#
// C# implementation of the approach
using System;
 
class GFG
{
 
    // Function to return the
    // maximum number of segments
    static int countPoints(int n, int m, int []a,
                            int []b, int x, int y)
    {
        // Sort both the vectors
        Array.Sort(a);
        Array.Sort(b);
     
        // Initially pointing to the
        // first element of b[]
        int j = 0;
        int count = 0;
        for (int i = 0; i < n; i++)
        {
     
            // Try to find a match in b[]
            while (j < m)
            {
     
                // The segment ends before b[j]
                if (a[i] + y < b[j])
                    break;
     
                // The point lies within the segment
                if (b[j] >= a[i] - x && b[j] <= a[i] + y)
                {
                    count++;
                    j++;
                    break;
                }
     
                // The segment starts after b[j]
                else
                    j++;
            }
        }
     
        // Return the required count
        return count;
    }
 
    // Driver code
    public static void Main()
    {
        int x = 1, y = 4;
        int[] a = {1, 5};
        int n = a.Length;
        int[] b = {1, 1, 2};
        int m = a.Length;
        Console.WriteLine(countPoints(n, m, a, b, x, y));
    }
}
 
// This code is contributed by Ryuga


PHP
= $a[$i] - $x &&
                $b[$j] <= $a[$i] + $y)
            {
                $count++;
                $j++;
                break;
            }
 
            // The segment starts after b[j]
            else
                $j++;
        }
    }
 
    // Return the required count
    return $count;
}
 
    // Driver code
    $x = 1;
    $y = 4;
    $a = array( 1, 5 );
    $n = count($a);
    $b = array( 1, 1, 2 );
    $m = count($b);
    echo countPoints($n, $m, $a, $b, $x, $y);
 
// This code is contributed by Arnab Kundu
?>


输出:
1

时间复杂度: O(N * log(N))
辅助空间: O(1)