📜  找到分区线,以使左右的值之和相等

📅  最后修改于: 2021-06-27 01:58:32             🧑  作者: Mango

考虑笛卡尔坐标平面上的n个点。给点(X i ,Y i )赋值V i 。如果与y轴平行的线在其左侧的点值的总和等于在其右侧的点值的总和,则被认为是一条很好的分隔线。请注意,如果某点位于分隔线上,则不会在该线的两侧考虑该点。任务是,如果存在良好的分区行,则打印“是” ,否则打印“否”

例子:

方法:

  1. 计算每个x坐标处的值。这意味着,如果有多个具有相同x坐标的点,则将它们视为单个点,并将其值相加。
  2. 从最小x坐标开始,并检查X i是否满足以下条件之一:
    • 直到i – 1的值之和等于从i +1n的值之和。
    • 直到i的值之和等于从i +1n的值之和。
    • 直到i – 1的值之和等于从in的值之和。
  3. 如果对于任何给定点均满足上述任何条件,则该线存在。

下面是上述方法的实现:

C++
// C++ implementation of the approach
#include 
using namespace std;
  
const int MAX = 1000;
  
// Function that returns true if
// the required line exists
bool lineExists(int x[], int y[],
                int v[], int n)
{
  
    // To handle negative values from x[]
    int size = (2 * MAX) + 1;
    long arr[size] = { 0 };
  
    // Update arr[] such that arr[i] contains
    // the sum of all v[j] such that x[j] = i
    // for all valid values of j
    for (int i = 0; i < n; i++) {
        arr[x[i] + MAX] += v[i];
    }
  
    // Update arr[i] such that arr[i] contains
    // the sum of the subarray arr[0...i]
    // from the original array
    for (int i = 1; i < size; i++)
        arr[i] += arr[i - 1];
  
    // If all the points add to 0 then
    // the line can be drawn anywhere
    if (arr[size - 1] == 0)
        return true;
  
    // If the line is drawn touching the
    // leftmost possible points
    if (arr[size - 1] - arr[0] == 0)
        return true;
  
    for (int i = 1; i < size - 1; i++) {
  
        // If the line is drawn just before
        // the current point
        if (arr[i - 1] == arr[size - 1] - arr[i - 1])
            return true;
  
        // If the line is drawn touching
        // the current point
        if (arr[i - 1] == arr[size - 1] - arr[i])
            return true;
  
        // If the line is drawn just after
        // the current point
        if (arr[i] == arr[size - 1] - arr[i])
            return true;
    }
  
    // If the line is drawn touching the
    // rightmost possible points
    if (arr[size - 2] == 0)
        return true;
  
    return false;
}
  
// Driver code
int main()
{
    int x[] = { -3, 5, 8 };
    int y[] = { 8, 7, 9 };
    int v[] = { 8, 2, 10 };
    int n = sizeof(x) / sizeof(int);
  
    if (lineExists(x, y, v, n))
        cout << "Yes";
    else
        cout << "No";
  
    return 0;
}


Java
// Java implementation of the approach
class GFG 
{
static int MAX = 1000;
  
// Function that returns true if
// the required line exists
static boolean lineExists(int x[], int y[],
                          int v[], int n)
{
  
    // To handle negative values from x[]
    int size = (2 * MAX) + 1;
    long []arr = new long[size];
  
    // Update arr[] such that arr[i] contains
    // the sum of all v[j] such that x[j] = i
    // for all valid values of j
    for (int i = 0; i < n; i++) 
    {
        arr[x[i] + MAX] += v[i];
    }
  
    // Update arr[i] such that arr[i] contains
    // the sum of the subarray arr[0...i]
    // from the original array
    for (int i = 1; i < size; i++)
        arr[i] += arr[i - 1];
  
    // If all the points add to 0 then
    // the line can be drawn anywhere
    if (arr[size - 1] == 0)
        return true;
  
    // If the line is drawn touching the
    // leftmost possible points
    if (arr[size - 1] - arr[0] == 0)
        return true;
  
    for (int i = 1; i < size - 1; i++) 
    {
  
        // If the line is drawn just before
        // the current point
        if (arr[i - 1] == arr[size - 1] - arr[i - 1])
            return true;
  
        // If the line is drawn touching
        // the current point
        if (arr[i - 1] == arr[size - 1] - arr[i])
            return true;
  
        // If the line is drawn just after
        // the current point
        if (arr[i] == arr[size - 1] - arr[i])
            return true;
    }
  
    // If the line is drawn touching the
    // rightmost possible points
    if (arr[size - 2] == 0)
        return true;
  
    return false;
}
  
// Driver code
public static void main(String []args) 
{
    int x[] = { -3, 5, 8 };
    int y[] = { 8, 7, 9 };
    int v[] = { 8, 2, 10 };
    int n = x.length;
  
    if (lineExists(x, y, v, n))
        System.out.printf("Yes");
    else
        System.out.printf("No");
}
}
  
// This code is contributed by 29AjayKumar


Python3
# Python3 implementation of the approach
MAX = 1000; 
  
# Function that returns true if 
# the required line exists 
def lineExists(x, y, v, n) :
  
    # To handle negative values from x[] 
    size = (2 * MAX) + 1; 
    arr = [0] * size ; 
  
    # Update arr[] such that arr[i] contains 
    # the sum of all v[j] such that x[j] = i 
    # for all valid values of j 
    for i in range(n) :
        arr[x[i] + MAX] += v[i]; 
  
    # Update arr[i] such that arr[i] contains 
    # the sum of the subarray arr[0...i] 
    # from the original array 
    for i in range(1, size) :
        arr[i] += arr[i - 1]; 
  
    # If all the points add to 0 then 
    # the line can be drawn anywhere 
    if (arr[size - 1] == 0) :
        return True; 
  
    # If the line is drawn touching the 
    # leftmost possible points 
    if (arr[size - 1] - arr[0] == 0) :
        return True; 
  
    for i in range(1, size - 1) : 
  
        # If the line is drawn just before 
        # the current point 
        if (arr[i - 1] == arr[size - 1] - arr[i - 1]) :
            return True; 
  
        # If the line is drawn touching 
        # the current point 
        if (arr[i - 1] == arr[size - 1] - arr[i]) :
            return True; 
  
        # If the line is drawn just after 
        # the current point 
        if (arr[i] == arr[size - 1] - arr[i]) :
            return True; 
  
    # If the line is drawn touching the 
    # rightmost possible points 
    if (arr[size - 2] == 0) :
        return True; 
  
    return False; 
  
# Driver code 
if __name__ == "__main__" :
  
    x = [ -3, 5, 8 ]; 
    y = [ 8, 7, 9 ]; 
    v = [ 8, 2, 10 ]; 
    n = len(x); 
  
    if (lineExists(x, y, v, n)) :
        print("Yes"); 
    else :
        print("No"); 
  
# This code is contributed by AnkitRai01


C#
// C# implementation of the approach
using System;
      
class GFG 
{
static int MAX = 1000;
  
// Function that returns true if
// the required line exists
static Boolean lineExists(int []x, int []y,
                          int []v, int n)
{
  
    // To handle negative values from x[]
    int size = (2 * MAX) + 1;
    long []arr = new long[size];
  
    // Update arr[] such that arr[i] contains
    // the sum of all v[j] such that x[j] = i
    // for all valid values of j
    for (int i = 0; i < n; i++) 
    {
        arr[x[i] + MAX] += v[i];
    }
  
    // Update arr[i] such that arr[i] contains
    // the sum of the subarray arr[0...i]
    // from the original array
    for (int i = 1; i < size; i++)
        arr[i] += arr[i - 1];
  
    // If all the points add to 0 then
    // the line can be drawn anywhere
    if (arr[size - 1] == 0)
        return true;
  
    // If the line is drawn touching the
    // leftmost possible points
    if (arr[size - 1] - arr[0] == 0)
        return true;
  
    for (int i = 1; i < size - 1; i++) 
    {
  
        // If the line is drawn just before
        // the current point
        if (arr[i - 1] == arr[size - 1] - arr[i - 1])
            return true;
  
        // If the line is drawn touching
        // the current point
        if (arr[i - 1] == arr[size - 1] - arr[i])
            return true;
  
        // If the line is drawn just after
        // the current point
        if (arr[i] == arr[size - 1] - arr[i])
            return true;
    }
  
    // If the line is drawn touching the
    // rightmost possible points
    if (arr[size - 2] == 0)
        return true;
  
    return false;
}
  
// Driver code
public static void Main(String []args) 
{
    int []x = { -3, 5, 8 };
    int []y = { 8, 7, 9 };
    int []v = { 8, 2, 10 };
    int n = x.Length;
  
    if (lineExists(x, y, v, n))
        Console.WriteLine("Yes");
    else
        Console.WriteLine("No");
}
}
  
// This code is contributed by Rajput-Ji


输出:
Yes

如果您希望与行业专家一起参加现场课程,请参阅《 Geeks现场课程》和《 Geeks现场课程美国》。