A delivery drone starts at a depot with a fixed battery capacity and must make a sequence of deliveries. The goal is to determine the maximum number of deliveries the drone can complete in a single trip without recharging.
- The drone starts at a depot with battery capacity
B(integer) - Each delivery involves:
- Flying from depot to customer location at distance
d - Returning to the depot
- Total battery consumption:
2*dunits
- Flying from depot to customer location at distance
- After each delivery, the drone can start the next delivery if it has sufficient battery
- The drone stops when its battery is insufficient for any additional round trip
- Line 1: Integer
B(0 ≤ B ≤ 10^9) - total available battery units - Line 2:
Nspace-separated positive integers (0 ≤ N ≤ 10^5, each distance ≤ 10^6) - distances to delivery locations
- A single integer: the maximum number of deliveries the drone can make without exceeding its battery capacity
- Sort distances in ascending order to prioritize closest locations first
- Iterate through sorted distances and calculate round-trip cost (2 × distance)
- Check battery constraint for each delivery:
- If sufficient battery: make delivery and update battery usage
- If insufficient battery: stop (remaining deliveries will cost even more)
- Return the count of completed deliveries
The greedy approach is optimal because:
- To maximize the number of deliveries, we should minimize battery usage per delivery
- Choosing the closest available location first always uses the least battery
- Once we can't afford a delivery, we can't afford any remaining deliveries (since they're farther)
- Time Complexity: O(N log N) due to sorting
- Space Complexity: O(1) additional space (in-place sorting)
- Empty array of distances → return 0
- Zero battery capacity → return 0
- Battery sufficient for all deliveries → return total count
- Large numbers within constraint limits
Input:
B = 10
distances = [3, 1, 4, 2]
Step 1: Sort distances → [1, 2, 3, 4]
Step 2: Calculate round-trip costs → [2, 4, 6, 8]
Step 3: Check deliveries:
- Delivery 1 (distance 1): cost 2, total used = 2 ≤ 10 ✓
- Delivery 2 (distance 2): cost 4, total used = 6 ≤ 10 ✓
- Delivery 3 (distance 3): cost 6, total used = 12 > 10 ✗
Output: 2 deliveries
- Handles up to 10^5 delivery locations efficiently
- Works with battery capacities up to 10^9
- Optimized for competitive programming constraints
- Memory efficient with minimal additional space usage
The solution can be used in two ways:
- Function call:
max_deliveries(B, distances)- returns the maximum number of deliveries - Input parsing:
solve_from_input()- reads from standard input in the specified format
Perfect for competitive programming problems involving optimization with resource constraints.