Skip to main content
Code Review

Return to Question

Rollback to Revision 2
Source Link
janos
  • 112.9k
  • 15
  • 154
  • 396
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
public class MainClass {
 static Segment[]Estacion[] segmentsestaciones = new Segment[]Estacion[]{
 new SegmentEstacion(100,110),
 new SegmentEstacion(115,116),
 new SegmentEstacion(100,111),
 new SegmentEstacion(0,15),
 new SegmentEstacion(50,110),
 new SegmentEstacion(70,120)
 };
 public static void Main (string[] args) {
 Console.WriteLine (ComputeCalcular());
 }
 public static int ComputeCalcular()
 {
 
 var min =segments=estaciones.Min(m=> m.FromDesde);
 var max =segments=estaciones.Max(m=> m.ToHasta);
 var distancerecorrido =0;
 var count=cantidad= max-min;
 var bA = new BitArray(countcantidad,false);
 foreach(var e in segmentsestaciones.OrderByDescending(o=> o.DistanceLongitud))
 {
 var h= ((e.ToHasta-e.FromDesde)/2);
 for(int i= 0;i<= h ; i++)
 {
 var slot=i+e.FromDesde-min;
 if(slot<countslot<cantidad) 
 if(!bA.Get(slot))
 {
 bA.Set(slot,true);
 distance++;recorrido++;
 }
 slot=e.Hasta-i-min-1;
 if(!bA.Get(slot)) 
 {
 bA.Set(slot,true);
 distance++;recorrido++;
 }
 if(distance==recorrido== countcantidad)
 return distance;recorrido;
 }
 }
 return distance;recorrido;
 }
 public class SegmentEstacion
 {
 public Guid Id {get; set;}
 public int FromDesde {get; set;}
 public int ToHasta {get; set;}
 public int DistanceLongitud {get; set;}
 
 public SegmentEstacion(int a, int b)
 {
 FromDesde = a;
 To=Hasta= b;
 Distance=Longitud= ToHasta-From;Desde;
 Id= Guid.NewGuid();
 }
 }
}
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
public class MainClass {
 static Segment[] segments = new Segment[]{
 new Segment(100,110),
 new Segment(115,116),
 new Segment(100,111),
 new Segment(0,15),
 new Segment(50,110),
 new Segment(70,120)
 };
 public static void Main (string[] args) {
 Console.WriteLine (Compute());
 }
 public static int Compute()
 {
 
 var min =segments.Min(m=> m.From);
 var max =segments.Max(m=> m.To);
 var distance =0;
 var count= max-min;
 var bA = new BitArray(count,false);
 foreach(var e in segments.OrderByDescending(o=> o.Distance))
 {
 var h= ((e.To-e.From)/2);
 for(int i= 0;i<= h ; i++)
 {
 var slot=i+e.From-min;
 if(slot<count) 
 if(!bA.Get(slot))
 {
 bA.Set(slot,true);
 distance++;
 }
 slot=e.Hasta-i-min-1;
 if(!bA.Get(slot)) 
 {
 bA.Set(slot,true);
 distance++;
 }
 if(distance== count)
 return distance;
 }
 }
 return distance;
 }
 public class Segment
 {
 public Guid Id {get; set;}
 public int From {get; set;}
 public int To {get; set;}
 public int Distance {get; set;}
 
 public Segment(int a, int b)
 {
 From = a;
 To= b;
 Distance= To-From;
 Id= Guid.NewGuid();
 }
 }
}
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();
 }
 }
}
deleted 59 characters in body
Source Link
Byron
  • 123
  • 5
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
public class MainClass {
 static Estacion[]Segment[] estacionessegments = new Estacion[]Segment[]{
 new EstacionSegment(100,110),
 new EstacionSegment(115,116),
 new EstacionSegment(100,111),
 new EstacionSegment(0,15),
 new EstacionSegment(50,110),
 new EstacionSegment(70,120)
 };
 public static void Main (string[] args) {
 Console.WriteLine (CalcularCompute());
 }
 public static int CalcularCompute()
 {
 
 var min =estaciones=segments.Min(m=> m.DesdeFrom);
 var max =estaciones=segments.Max(m=> m.HastaTo);
 var recorridodistance =0;
 var cantidad=count= max-min;
 var bA = new BitArray(cantidadcount,false);
 foreach(var e in estacionessegments.OrderByDescending(o=> o.LongitudDistance))
 {
 var h= ((e.HastaTo-e.DesdeFrom)/2);
 for(int i= 0;i<= h ; i++)
 {
 var slot=i+e.DesdeFrom-min;
 if(slot<cantidadslot<count) 
 if(!bA.Get(slot))
 {
 bA.Set(slot,true);
 recorrido++;distance++;
 }
 slot=e.Hasta-i-min-1;
 if(!bA.Get(slot)) 
 {
 bA.Set(slot,true);
 recorrido++;distance++;
 }
 if(recorrido==distance== cantidadcount)
 return recorrido;distance;
 }
 }
 return recorrido;distance;
 }
 public class EstacionSegment
 {
 public Guid Id {get; set;}
 public int DesdeFrom {get; set;}
 public int HastaTo {get; set;}
 public int LongitudDistance {get; set;}
 
 public EstacionSegment(int a, int b)
 {
 DesdeFrom = a;
 Hasta=To= b;
 Longitud=Distance= HastaTo-Desde;From;
 Id= Guid.NewGuid();
 }
 }
}
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();
 }
 }
}
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
public class MainClass {
 static Segment[] segments = new Segment[]{
 new Segment(100,110),
 new Segment(115,116),
 new Segment(100,111),
 new Segment(0,15),
 new Segment(50,110),
 new Segment(70,120)
 };
 public static void Main (string[] args) {
 Console.WriteLine (Compute());
 }
 public static int Compute()
 {
 
 var min =segments.Min(m=> m.From);
 var max =segments.Max(m=> m.To);
 var distance =0;
 var count= max-min;
 var bA = new BitArray(count,false);
 foreach(var e in segments.OrderByDescending(o=> o.Distance))
 {
 var h= ((e.To-e.From)/2);
 for(int i= 0;i<= h ; i++)
 {
 var slot=i+e.From-min;
 if(slot<count) 
 if(!bA.Get(slot))
 {
 bA.Set(slot,true);
 distance++;
 }
 slot=e.Hasta-i-min-1;
 if(!bA.Get(slot)) 
 {
 bA.Set(slot,true);
 distance++;
 }
 if(distance== count)
 return distance;
 }
 }
 return distance;
 }
 public class Segment
 {
 public Guid Id {get; set;}
 public int From {get; set;}
 public int To {get; set;}
 public int Distance {get; set;}
 
 public Segment(int a, int b)
 {
 From = a;
 To= b;
 Distance= To-From;
 Id= Guid.NewGuid();
 }
 }
}
added 135 characters in body
Source Link
janos
  • 112.9k
  • 15
  • 154
  • 396

I'm trying to get the distance traveled in a collection of routes inon a straight line. The rule to determine the distance is that a route segment counts only once. For example given the single sections countsegments (50, 70) and (60, 80), if the route more than oncetotal distance covered is 30 instead of 40, onlybecause the first countsegment (60, 70) is covered twice.


╔═══════╦═══════╦═══════╗
║ Route ║ From ║ To ║
╠═══════╬═══════╬═══════╣
║ 1 ║ 100 ║ 110 ║
║ 2 ║ 115 ║ 116 ║
║ 3 ║ 100 ║ 111 ║
║ 4 ║ 0 ║ 15 ║
║ 5 ║ 50 ║ 110 ║
║ 6 ║ 70 ║ 120 ║
╚═══════╩═══════╩═══════╝
╔═══════╦═══════╦═══════╗
║ Route ║ From ║ To ║
╠═══════╬═══════╬═══════╣
║ 1 ║ 100 ║ 110 ║
║ 2 ║ 115 ║ 116 ║
║ 3 ║ 100 ║ 111 ║
║ 4 ║ 0 ║ 15 ║
║ 5 ║ 50 ║ 110 ║
║ 6 ║ 70 ║ 120 ║
╚═══════╩═══════╩═══════╝

I'm trying to get the distance traveled in a collection of routes in a straight line. The rule to determine the distance is that only the single sections count, if the route more than once, only the first count.


╔═══════╦═══════╦═══════╗
║ Route ║ From ║ To ║
╠═══════╬═══════╬═══════╣
║ 1 ║ 100 ║ 110 ║
║ 2 ║ 115 ║ 116 ║
║ 3 ║ 100 ║ 111 ║
║ 4 ║ 0 ║ 15 ║
║ 5 ║ 50 ║ 110 ║
║ 6 ║ 70 ║ 120 ║
╚═══════╩═══════╩═══════╝

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.

╔═══════╦═══════╦═══════╗
║ Route ║ From ║ To ║
╠═══════╬═══════╬═══════╣
║ 1 ║ 100 ║ 110 ║
║ 2 ║ 115 ║ 116 ║
║ 3 ║ 100 ║ 111 ║
║ 4 ║ 0 ║ 15 ║
║ 5 ║ 50 ║ 110 ║
║ 6 ║ 70 ║ 120 ║
╚═══════╩═══════╩═══════╝
Source Link
Byron
  • 123
  • 5
Loading
lang-cs

AltStyle によって変換されたページ (->オリジナル) /