Advertisement
Ligh7_of_H3av3n

01.Rapid Courier

Jun 22nd, 2024
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.86 KB | None | 0 0
  1. import java.util.ArrayDeque;
  2. import java.util.Deque;
  3. import java.util.Scanner;
  4.  
  5. public class Help {
  6.  
  7.     public static void main(String[] args) {
  8.  
  9.         Scanner scanner = new Scanner(System.in);
  10.  
  11.  
  12.         // Read packages
  13.         String[] packageInput = scanner.nextLine().split(" ");
  14.         Deque<Integer> packages = new ArrayDeque<>();
  15.         for (String weight : packageInput) {
  16.             packages.add(Integer.parseInt(weight));
  17.         }
  18.  
  19.         // Read couriers
  20.         String[] couriersInput = scanner.nextLine().split(" ");
  21.         Deque<Integer> couriers = new ArrayDeque<>();
  22.         for (String capacity : couriersInput) {
  23.             couriers.add(Integer.parseInt(capacity));
  24.         }
  25.  
  26.         int totalWeight = 0;
  27.  
  28.         // Process deliveries
  29.         while (!packages.isEmpty() && !couriers.isEmpty()) {
  30.             int currentPackage = packages.peekLast();
  31.             int currentCourier = couriers.peekFirst();
  32.  
  33.             if (currentCourier >= currentPackage) {
  34.                 // Courier can deliver the package
  35.                totalWeight += currentPackage;
  36.                 packages.pollLast();
  37.                 couriers.pollFirst();
  38.  
  39.                 int newCapacity = currentCourier - 2 * currentPackage;
  40.                 if (newCapacity > 0) {
  41.                     couriers.addLast(newCapacity);
  42.                 }
  43.             } else {
  44.                 // Courier cannot deliver the package fully
  45.                 totalWeight += currentCourier;
  46.                 int remainingPackageWeight = currentPackage - currentCourier;
  47.                 packages.pollLast();
  48.                 packages.addLast(remainingPackageWeight);
  49.                 couriers.pollFirst();
  50.             }
  51.         }
  52.  
  53.         // Output results
  54.         System.out.println("Total weight: " + totalWeight + " kg");
  55.  
  56.         if (packages.isEmpty() && couriers.isEmpty()) {
  57.             System.out.println("Congratulations, all packages were delivered successfully by the couriers today.");
  58.         } else if (!packages.isEmpty() && couriers.isEmpty()) {
  59.             System.out.print("Unfortunately, there are no more available couriers to deliver the following packages: ");
  60.             printDeque(packages);
  61.         } else if (packages.isEmpty() && !couriers.isEmpty()) {
  62.             System.out.print("Couriers are still on duty: ");
  63.             printDeque(couriers);
  64.             System.out.println(" but there are no more packages to deliver.");
  65.         }
  66.  
  67.         scanner.close();
  68.     }
  69.  
  70.     // Helper method to print elements of a deque
  71.     private static void printDeque(Deque<Integer> deque) {
  72.         boolean first = true;
  73.         while (!deque.isEmpty()) {
  74.             if (!first) {
  75.                 System.out.print(", ");
  76.             }
  77.             System.out.print(deque.pollFirst());
  78.             first = false;
  79.         }
  80.         System.out.println();
  81.     }
  82. }
  83.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement