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, rideStartTime + ride.Distance); } }
public void CalculateClosestRide(List <Ride> rides) { int closest = int.MaxValue; for (int i = 0; i < rides.Count; i++) { Ride other = rides[i]; if (this.ID == other.ID) { continue; } int distance = Utils.CalculateDistance(this.EndR, this.EndC, other.StartR, other.StartC); if (this.TimeStart + this.Distance + distance > other.TimeLatestStart) { continue; } closest = Math.Min(closest, distance); } ClosestRideDistance = closest; }
public void CalculateClosestRide(List <Ride> rides) { int closest = int.MaxValue; for (int i = 0; i < rides.Count; i++) { Ride other = rides[i]; if (this.ID == other.ID) { continue; } int distance = this.TimeToPosition(other.StartR, other.StartC); if (this.TimeDriveEnd + distance > other.TimeLatestStart) { continue; } closest = Math.Min(closest, distance); } ClosestRideDistance = closest; }
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, 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()); 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, rideStartTime + bestRide.Distance); Rides.Remove(bestRide); } } }
public override void Solve() { foreach (Ride ride in Rides) { ride.CalculateClosestRide(Rides); } while (true) { bool rideAdded = false; foreach (Vehicle car in Vehicles) { Ride bestRide = null; int bestCompleteTime = 0; if (UseBonusMethod == false) { FindBestRideForCar(car, out bestRide, out bestCompleteTime); } else { FindBestRideForCarBonus(car, out bestRide, out bestCompleteTime); } if (bestRide == null) { continue; } rideAdded = true; car.AddRide(bestRide, bestCompleteTime); Rides.Remove(bestRide); } if (rideAdded == false) { break; } } // Optimization phase while (true) { if (TryCarsX2Reallocate(AllocateRidesToCar_FindEarlyStartSimple, 1)) { continue; } if (TryCarsX2Reallocate(AllocateRidesToCar_FindEarlyStartClosest, 1)) { continue; } if (TryCarsX2Reallocate(AllocateRidesToCar_FindEarlyStartClosestV2, 1)) { continue; } if (TryCarsPushRide()) { continue; } //int score = this.CalculateScore(); //this.WriteOutput(InputFileName + "." + score + ".out.txt"); if (TryCarsX2Reallocate(AllocateRidesToCar_FindEarlyStartSimple, 2)) { continue; } if (TryCarsX2Reallocate(AllocateRidesToCar_FindEarlyStartClosest, 2)) { continue; } if (TryCarsX2Reallocate(AllocateRidesToCar_FindEarlyStartClosestV2, 2)) { continue; } /* * if (TryCarsX2Reallocate(AllocateRidesToCar_FindEarlyStartSimple, 3)) * continue; * * if (TryCarsX2Reallocate(AllocateRidesToCar_FindEarlyStartClosest, 3)) * continue; * * if (TryCarsX2Reallocate(AllocateRidesToCar_FindEarlyStartClosestV2, 3)) * continue; */ break; } }
protected void AllocateRidesToCar_FindEarlyStartClosestV2(Vehicle car, List <Ride> rides, int bonusValue, out Ride bestRide, out int bestCompleteTime) { bestRide = null; bestCompleteTime = 0; double bestStartTime = 0; foreach (Ride ride in rides) { int timeToDrive = car.TimeToPosition(ride.StartR, ride.StartC); int carToStart = car.TimeDriveEnd + timeToDrive; if (carToStart > ride.TimeEnd) { continue; } double startTime = Math.Max(carToStart, ride.TimeStart); int completeTime = (int)startTime + ride.Distance; if (completeTime > ride.TimeEnd) { continue; } if ((double)completeTime <= 0.98 * this.Steps) { startTime += (int)((double)ride.ClosestRideDistance / 16.0); } if (bestRide == null) { bestCompleteTime = completeTime; bestRide = ride; bestStartTime = startTime; } else if (startTime < bestStartTime) { bestCompleteTime = completeTime; bestRide = ride; bestStartTime = startTime; } } }
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, bestGlobalCompleteTime); // Remove ride from list for (int i = 0; i < Rides.Count; i++) { if (Rides[i].ID == bestGlobalRide.ID) { Rides.RemoveAt(i); break; } } } }