I'm trying to get the distance traveled in a collection of routes on a straight line. The rule to determine the distance is that a route segment counts only once. For example given the segments (50, 70)
and (60, 80)
, the total distance covered is 30 instead of 40, because the segment (60, 70)
is covered twice.
Sample:
╔═══════╦═══════╦═══════╗
║ Route ║ From ║ To ║
╠═══════╬═══════╬═══════╣
║ 1 ║ 100 ║ 110 ║
║ 2 ║ 115 ║ 116 ║
║ 3 ║ 100 ║ 111 ║
║ 4 ║ 0 ║ 15 ║
║ 5 ║ 50 ║ 110 ║
║ 6 ║ 70 ║ 120 ║
╚═══════╩═══════╩═══════╝
In these routes the actual distance traveled was 85m. I made a solution that uses a bit array to control, and a counter to calculate the distance, with this solution the process speed and the memory consumption is good, up to 20250000 meters, after this distance the performance begins to fall. Could they recommend me some algorithm to solve this kind of problem?
Note: These distance are in a straight line, they are not routes that have georeference, they are segments of a straight line.
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
public class MainClass {
static Estacion[] estaciones = new Estacion[]{
new Estacion(100,110),
new Estacion(115,116),
new Estacion(100,111),
new Estacion(0,15),
new Estacion(50,110),
new Estacion(70,120)
};
public static void Main (string[] args) {
Console.WriteLine (Calcular());
}
public static int Calcular()
{
var min =estaciones.Min(m=> m.Desde);
var max =estaciones.Max(m=> m.Hasta);
var recorrido =0;
var cantidad= max-min;
var bA = new BitArray(cantidad,false);
foreach(var e in estaciones.OrderByDescending(o=> o.Longitud))
{
var h= ((e.Hasta-e.Desde)/2);
for(int i= 0;i<= h ; i++)
{
var slot=i+e.Desde-min;
if(slot<cantidad)
if(!bA.Get(slot))
{
bA.Set(slot,true);
recorrido++;
}
slot=e.Hasta-i-min-1;
if(!bA.Get(slot))
{
bA.Set(slot,true);
recorrido++;
}
if(recorrido== cantidad)
return recorrido;
}
}
return recorrido;
}
public class Estacion
{
public Guid Id {get; set;}
public int Desde {get; set;}
public int Hasta {get; set;}
public int Longitud {get; set;}
public Estacion(int a, int b)
{
Desde = a;
Hasta= b;
Longitud= Hasta-Desde;
Id= Guid.NewGuid();
}
}
}