Advertisement
Ligh7_of_H3av3n

01.Rapid Courier-popravka

Jun 22nd, 2024
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.25 KB | None | 0 0
  1. import java.util.ArrayDeque;
  2. import java.util.Scanner;
  3.  
  4. public class RapidCourier {
  5.  
  6.     public static void main(String[] args) {
  7.  
  8.         Scanner scanner = new Scanner(System.in);
  9.  
  10.         String[] input = scanner.nextLine().split(" ");
  11.         String[] courier = scanner.nextLine().split(" ");
  12.  
  13.         ArrayDeque<Integer> packages = new ArrayDeque<>();
  14.         ArrayDeque<Integer> couriers = new ArrayDeque<>();
  15.  
  16.         for (String w : input) {
  17.             packages.add(Integer.parseInt(w));
  18.         }
  19.  
  20.         for (String c : courier) {
  21.             couriers.add(Integer.parseInt(c));
  22.         }
  23.  
  24.         int total = 0;
  25.  
  26.         while (!packages.isEmpty() && !couriers.isEmpty()) {
  27.             int weight = packages.peek();
  28.             int capacity = couriers.peek();
  29.  
  30.             if (capacity >= weight) {
  31.                 couriers.poll(); // Remove the courier from the queue
  32.  
  33.                 total += weight;
  34.                 packages.poll(); // Remove the package from the queue
  35.             } else {
  36.                 int remaining = weight - capacity;
  37.  
  38.                 total += capacity;
  39.  
  40.                 packages.poll(); // Remove the package from the queue
  41.                 couriers.poll(); // Remove the courier from the queue
  42.  
  43.                 packages.addFirst(remaining); // Add remaining weight back to the queue
  44.             }
  45.         }
  46.  
  47.         System.out.printf("Total weight: %d kg\n", total);
  48.  
  49.         if (packages.isEmpty()) {
  50.             System.out.println("Congratulations, all packages were delivered successfully by the couriers today.");
  51.         } else if (couriers.isEmpty()) {
  52.             System.out.print("Unfortunately, there are no more available couriers to deliver the following packages: ");
  53.             StringBuilder remainingPackages = new StringBuilder();
  54.  
  55.             while (!packages.isEmpty()) {
  56.                 remainingPackages.append(packages.poll());
  57.                 if (!packages.isEmpty()) {
  58.                     remainingPackages.append(", ");
  59.                 }
  60.             }
  61.             System.out.println(remainingPackages);
  62.         } else {
  63.             System.out.println("Couriers are still on duty: " + couriers.toString() + " but there are no more packages to deliver.");
  64.         }
  65.     }
  66. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement