考虑笛卡尔坐标平面上的n个点。给点(X i ,Y i )赋值V i 。如果与y轴平行的线在其左侧的点值的总和等于在其右侧的点值的总和,则被认为是一条很好的分隔线。请注意,如果某点位于分隔线上,则不会在该线的两侧考虑该点。任务是,如果存在良好的分区行,则打印“是” ,否则打印“否” 。
例子:
Input: X[] = {-3, 5, 8}, Y[] = {8, 7, 9}, V[] = {8, 2, 10}
Output: Yes
x = c where 5 < c < 8 is a good partition
line as sum of values on both sides is 10.
Input: X[] = {-2, 5, 8, 9}, Y[] = {7, 7, 9, 8}, V[] = {6, 2, 1, 8}
Output: Yes
方法:
- 计算每个x坐标处的值。这意味着,如果有多个具有相同x坐标的点,则将它们视为单个点,并将其值相加。
- 从最小x坐标开始,并检查X i是否满足以下条件之一:
- 直到i – 1的值之和等于从i +1到n的值之和。
- 直到i的值之和等于从i +1到n的值之和。
- 直到i – 1的值之和等于从i到n的值之和。
- 如果对于任何给定点均满足上述任何条件,则该线存在。
下面是上述方法的实现:
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