public override void Solve() { Rides.Sort(new Ride.CompareByStartTime()); Vehicle.CompareByTimeDriveEnd carSortTimeDriveEnd = new Vehicle.CompareByTimeDriveEnd(); while (Rides.Count > 0) { Ride ride = Rides[0]; Rides.RemoveAt(0); // Find fastest car for the ride Vehicles.Sort(carSortTimeDriveEnd); Vehicle bestCar; int bestStartTime; FindBestCarForRide(ride, out bestCar, out bestStartTime); if (bestCar == null) { continue; } // Check if valid // Complete ride on time int rideStartTime = Math.Max(ride.TimeStart, bestStartTime); if (rideStartTime + ride.Distance >= ride.TimeEnd) { continue; } if (rideStartTime + ride.Distance >= Steps) { continue; } // Add ride to car bestCar.AddRide(ride, ride.EndR, ride.EndC, rideStartTime + ride.Distance); } }
public override void Solve() { Rides.Sort(new Ride.CompareByStartTime()); Rides.Reverse(); Vehicle.CompareByTimeDriveEnd carSortTimeDriveEnd = new Vehicle.CompareByTimeDriveEnd(); Vehicles.Sort(carSortTimeDriveEnd); int currentTime = -1; while (Rides.Count > 0) { currentTime++; if (Vehicles[0].TimeDriveEnd > currentTime) { currentTime = Vehicles[0].TimeDriveEnd; } if (currentTime >= this.Steps) { break; } for (int ridePos = Rides.Count - 1; ridePos >= 0; ridePos--) { Ride ride = Rides[ridePos]; // When too far in list if (ride.TimeStart > currentTime) { break; } // If ride not possible if (ride.TimeLatestStart < currentTime) { Rides.RemoveAt(ridePos); continue; } Vehicle bestCar; int bestStartTime; FindBestCarForRideMaxTime(ride, currentTime, out bestCar, out bestStartTime); if (bestCar == null) { continue; } // Check if valid // Complete ride on time int rideStartTime = Math.Max(ride.TimeStart, bestStartTime); if (rideStartTime + ride.Distance >= ride.TimeEnd) { continue; } if (rideStartTime + ride.Distance >= Steps) { continue; } // Add ride to car bestCar.AddRide(ride, ride.EndR, ride.EndC, rideStartTime + ride.Distance); Rides.RemoveAt(ridePos); // Optimization - if not vehicle is free at this time - not need to continue checking Vehicles.Sort(carSortTimeDriveEnd); if (Vehicles[0].TimeDriveEnd > currentTime) { break; } } } }
public override void Solve() { Rides.Sort(new Ride.CompareByStartTime()); Vehicle.CompareByTimeDriveEnd carSortTimeDriveEnd = new Vehicle.CompareByTimeDriveEnd(); while (Rides.Count > 0) { Vehicles.Sort(carSortTimeDriveEnd); Vehicle bestGlobalCar = null; int bestGlobalCompleteTime = int.MaxValue; Ride bestGlobalRide = null; foreach (Ride ride in Rides) { // All other rides will start later if (ride.TimeStart > bestGlobalCompleteTime) { break; } int rideTime = ride.Distance; // Find fastest car for the ride Vehicle bestCar; int bestCompleteTime; FindBestCarForRideComplete(ride, out bestCar, out bestCompleteTime); // Check if valid // Complete ride on time if (bestCompleteTime >= ride.TimeEnd) { continue; } if (bestCompleteTime >= Steps) { continue; } if (bestGlobalCar == null) { bestGlobalCar = bestCar; bestGlobalCompleteTime = bestCompleteTime; bestGlobalRide = ride; } else if (bestCompleteTime < bestGlobalCompleteTime) { bestGlobalCar = bestCar; bestGlobalCompleteTime = bestCompleteTime; bestGlobalRide = ride; } } if (bestGlobalRide == null) { return; } // Add ride to car bestGlobalCar.AddRide(bestGlobalRide, bestGlobalRide.EndR, bestGlobalRide.EndC, bestGlobalCompleteTime); // Remove ride from list for (int i = 0; i < Rides.Count; i++) { if (Rides[i].ID == bestGlobalRide.ID) { Rides.RemoveAt(i); break; } } } }
public override void Solve() { Rides.Sort(new Ride.CompareByStartTime()); Rides.Reverse(); Vehicle.CompareByTimeDriveEnd carSortTimeDriveEnd = new Vehicle.CompareByTimeDriveEnd(); Vehicles.Sort(carSortTimeDriveEnd); int currentTime = -1; while (Rides.Count > 0) { Vehicles.Sort(carSortTimeDriveEnd); currentTime++; if (Vehicles[0].TimeDriveEnd > currentTime) { currentTime = Vehicles[0].TimeDriveEnd; } if (currentTime >= this.Steps) { break; } // Clean rides list for (int ridePos = Rides.Count - 1; ridePos >= 0; ridePos--) { Ride ride = Rides[ridePos]; // If ride not possible if (ride.TimeLatestStart < currentTime) { Rides.RemoveAt(ridePos); continue; } } for (int i = 0; i < Vehicles.Count; i++) { Vehicle car = Vehicles[i]; // This car (and all after) can't do rides in time if (car.TimeDriveEnd > currentTime) { break; } Ride bestRide; int bestStartTime; FindBestRideForCarMaxTime(car, currentTime, out bestRide, out bestStartTime); if (bestRide == null) { continue; } // Check if valid // Complete ride on time int rideStartTime = Math.Max(bestRide.TimeStart, bestStartTime); if (rideStartTime + bestRide.Distance >= bestRide.TimeEnd) { continue; } if (rideStartTime + bestRide.Distance >= Steps) { continue; } // Add ride to car car.AddRide(bestRide, bestRide.EndR, bestRide.EndC, rideStartTime + bestRide.Distance); Rides.Remove(bestRide); } } }