📌  相关文章
📜  找到分区线,使左右值的总和相等

📅  最后修改于: 2021-10-23 08:13:49             🧑  作者: Mango

考虑笛卡尔坐标平面上的n个点。让点(X i , Y i )被赋予一个值V i 。如果一条平行于y 轴的线在其左侧点值的总和等于其右侧点值的总和,则称其为良好的分割线。请注意,如果一个点位于分割线上,则不考虑在分割线上的任何一侧。任务如果存在良好的分区线则打印Yes否则打印No
例子:

方法:

  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


Javascript


输出:
Yes