We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
usingSystem;usingSystem.Collections.Generic;usingSystem.IO;usingSystem.Linq;classSolution{staticvoidMain(String[]args){string[]tokens_n=Console.ReadLine().Split(' ');intnCities=Convert.ToInt32(tokens_n[0]);intmSpaceStations=Convert.ToInt32(tokens_n[1]);string[]c_temp=Console.ReadLine().Split(' ');int[]spaceStationArr=Array.ConvertAll(c_temp,Int32.Parse);/*The array has to be sorted. According to MSDN arrays that are sorted by using the Heapsort and Quicksort algorithms, in the worst case, this method is an O(n log n) operation, where n is the Length of array.*/Array.Sort(spaceStationArr);//Set an initial max distance which can sensibly be the distance between City 0 and first space stationintmaxDistance=spaceStationArr[0];/*We are interested in the cities that are in the middle of two space stations as this city will be furthest for that set of two space stations City : 0,1,2,3,5,6,7,8 SpaceStation : .,.,2,.,.,.,7,.In the example above, 5 is in the middle of space station 2 and 7 and the distance to the closest one is 2 and can be calculated as (7-2)/2 = 2 */for(inti=1;i<spaceStationArr.Length;i++){intdistance=(spaceStationArr[i]-spaceStationArr[i-1])/2;if(distance>maxDistance)maxDistance=distance;}//Check the distance of last spaceStation and last city. intlastSpaceStationDistance=nCities-1-spaceStationArr[mSpaceStations-1];if(lastSpaceStationDistance>maxDistance){maxDistance=lastSpaceStationDistance;}Console.WriteLine(maxDistance);}}
Flatland Space Stations
You are viewing a single comment's thread. Return to all comments →
A solution in C# with a bit of explanation.