Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.ArrayDeque;
- import java.util.Deque;
- import java.util.Scanner;
- public class Help {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- // Read packages
- String[] packageInput = scanner.nextLine().split(" ");
- Deque<Integer> packages = new ArrayDeque<>();
- for (String weight : packageInput) {
- packages.add(Integer.parseInt(weight));
- }
- // Read couriers
- String[] couriersInput = scanner.nextLine().split(" ");
- Deque<Integer> couriers = new ArrayDeque<>();
- for (String capacity : couriersInput) {
- couriers.add(Integer.parseInt(capacity));
- }
- int totalWeight = 0;
- // Process deliveries
- while (!packages.isEmpty() && !couriers.isEmpty()) {
- int currentPackage = packages.peekLast();
- int currentCourier = couriers.peekFirst();
- if (currentCourier >= currentPackage) {
- // Courier can deliver the package
- totalWeight += currentPackage;
- packages.pollLast();
- couriers.pollFirst();
- int newCapacity = currentCourier - 2 * currentPackage;
- if (newCapacity > 0) {
- couriers.addLast(newCapacity);
- }
- } else {
- // Courier cannot deliver the package fully
- totalWeight += currentCourier;
- int remainingPackageWeight = currentPackage - currentCourier;
- packages.pollLast();
- packages.addLast(remainingPackageWeight);
- couriers.pollFirst();
- }
- }
- // Output results
- System.out.println("Total weight: " + totalWeight + " kg");
- if (packages.isEmpty() && couriers.isEmpty()) {
- System.out.println("Congratulations, all packages were delivered successfully by the couriers today.");
- } else if (!packages.isEmpty() && couriers.isEmpty()) {
- System.out.print("Unfortunately, there are no more available couriers to deliver the following packages: ");
- printDeque(packages);
- } else if (packages.isEmpty() && !couriers.isEmpty()) {
- System.out.print("Couriers are still on duty: ");
- printDeque(couriers);
- System.out.println(" but there are no more packages to deliver.");
- }
- scanner.close();
- }
- // Helper method to print elements of a deque
- private static void printDeque(Deque<Integer> deque) {
- boolean first = true;
- while (!deque.isEmpty()) {
- if (!first) {
- System.out.print(", ");
- }
- System.out.print(deque.pollFirst());
- first = false;
- }
- System.out.println();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement