在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:(Center1-L / 2,Center1 + L / 2)
- 线段2:(Center2-L / 2,Center2 + L / 2)
- 线段3:(Center3-L / 2,Center3 + L / 2)
方法:最初对中心进行排序,因为中间段将永远不会移动,因为左右段始终可以接近其中心以增加相交长度。将处理三种情况:
- 情况1:在这种情况下,当左右中心之间的距离大于或等于2 * K + L时,交叉点将始终为零。不可能重叠,因为即使两个中心仍以K距离移动,它们也将相距L或更大的距离。
- 情况2:在这种情况下,左右中心的距离大于或等于2 * K时,存在一个等于(2 * k –(center [2] – center [0] –长度]的交点。 )) ,可以使用简单的数学计算得出。
- 情况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
?>
输出:
1