给定 X 轴上的三个点,表示三个线段的中心。线段的长度也给定为 L。任务是将给定线段的中心移动 K 的距离,以最大化三条线之间的交点长度。
例子:
Input: c1 = 1, c2 = 2, c3 = 3, L = 1, K = 0
Output: 0
Since, no center can be moved, there is no intersection among three segments {(0.5, 1.5), (1.5, 2.5), (2.5, 3.5)}.
Input: c1 = 1, c2 = 2, c3 = 3, L = 1, K = 1
Output: 1
Center’s 1 and 3 can be shifted 1 unit ahead and forward respectively to overlap with center 2
线段如下:
- 线段 1:(中心 1-L/2,中心 1+L/2)
- 线段 2:(Center2-L/2、Center2+L/2)
- 线段 3:(Center3-L/2,Center3+L/2)
方法:最初对中心进行排序,因为中间段永远不会移动,因为左右段总是可以到达其中心附近以增加交叉点长度。将有以下三种情况需要处理:
- 情况一:当左右中心距离大于等于2*K+L时,此类场景交点将始终为零。没有重叠是可能的,因为即使在将两个中心移动 K 距离后,它们仍将相距 L 或更远的距离。
- 情况二:当左右中心距离大于等于2*K时,存在一个交点等于(2*k-(center[2]-center[0]-length) ))可以使用简单的数学计算。
- 情况 3:当左右中心之间的距离小于 2*K 时,这种情况下两个中心都可以与中间中心重合。因此,交点为L。
下面是上述方法的实现:
C++
#include
using namespace std;
// Function to print the maximum intersection
int max_intersection(int* center, int length, int k)
{
sort(center, center + 3);
// Case 1
if (center[2] - center[0] >= 2 * k + length) {
return 0;
}
// Case 2
else if (center[2] - center[0] >= 2 * k) {
return (2 * k - (center[2] - center[0] - length));
}
// Case 3
else
return length;
}
// Driver Code
int main()
{
int center[3] = { 1, 2, 3 };
int L = 1;
int K = 1;
cout << max_intersection(center, L, K);
}
Java
// Java implementation
// of above approach
import java.util.*;
class GFG
{
// Function to print the
// maximum intersection
static int max_intersection(int center[],
int length, int k)
{
Arrays.sort(center);
// Case 1
if (center[2] - center[0] >= 2 * k + length)
{
return 0;
}
// Case 2
else if (center[2] - center[0] >= 2 * k)
{
return (2 * k - (center[2] -
center[0] - length));
}
// Case 3
else
return length;
}
// Driver Code
public static void main(String args[])
{
int center[] = { 1, 2, 3 };
int L = 1;
int K = 1;
System.out.println( max_intersection(center, L, K));
}
}
// This code is contributed
// by Arnab Kundu
Python3
# Python3 implementation of above approach
# Function to print the maximum intersection
def max_intersection(center, length, k):
center.sort();
# Case 1
if (center[2] - center[0] >= 2 * k + length):
return 0;
# Case 2
elif (center[2] - center[0] >= 2 * k):
return (2 * k - (center[2] - center[0] - length));
# Case 3
else:
return length;
# Driver Code
center = [1, 2, 3];
L = 1;
K = 1;
print(max_intersection(center, L, K));
# This code is contributed
# by mits
C#
// C# implementation
// of above approach
using System;
class GFG
{
// Function to print the
// maximum intersection
static int max_intersection(int []center,
int length, int k)
{
Array.Sort(center);
// Case 1
if (center[2] - center[0] >= 2 * k + length)
{
return 0;
}
// Case 2
else if (center[2] -
center[0] >= 2 * k)
{
return (2 * k - (center[2] -
center[0] - length));
}
// Case 3
else
return length;
}
// Driver Code
public static void Main()
{
int []center = { 1, 2, 3 };
int L = 1;
int K = 1;
Console.WriteLine(max_intersection(center, L, K));
}
}
// This code is contributed
// by Subhadeep Gupta
PHP
= 2 * $k + $length)
{
return 0;
}
// Case 2
else if ($center[2] -
$center[0] >= 2 * $k)
{
return (2 * $k - ($center[2] -
$center[0] - $length));
}
// Case 3
else
return $length;
}
// Driver Code
$center = array(1, 2, 3);
$L = 1;
$K = 1;
echo max_intersection($center, $L, $K);
// This code is contributed
// by mits
?>
Javascript
输出:
1