Problem 6-5

CSharp · Console

/Program.cs

// There are N oil derricks in the area. The main oil pipeline will go through the area
// strictly from east to west, but its northern coordinate is negotiable. Each derrick must be
// connected directly to the main pipeline with its own pipe. Find the y-position of the
// main pipeline that will lead to minimal total length of all pipes coming from the derricks
// to this main pipeline.

using System;
					
public class Program
{
	public static void Main(string[] args)
	{
		int[] x = {1, 2, 3, 4, 5, 8, 13};
		int[] y = {1, 3, 4, 5, 6, 7, 11};
		Console.WriteLine(bestY(x, y));
	}
	
	public static int bestY(int[] x,int[] y)
	{
		Array.Sort(y);
		return y[y.Length/2];
		
		// Time complexity: O(n log n)
	}
}
;