Advertisement
Viksy

Medivac

Oct 15th, 2022 (edited)
752
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.96 KB | None | 0 0
  1. import java.io.BufferedReader;
  2. import java.io.IOException;
  3. import java.io.InputStreamReader;
  4. import java.util.*;
  5.  
  6. public class MedivacWithClass {
  7.     public static class Unit implements Comparable<Unit>{
  8.         int name;
  9.         int capacity;
  10.         int rating;
  11.  
  12.         Unit(int name, int capacity, int rating) {
  13.             this.name = name;
  14.             this.capacity = capacity;
  15.             this.rating = rating;
  16.         }
  17.  
  18.         @Override
  19.         public int compareTo(Unit other) {
  20.             return this.name - other.name;
  21.         }
  22.     }
  23.  
  24.     public static void main(String[] args) throws IOException {
  25.         BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
  26.  
  27.         int medivacCapacity = Integer.parseInt(reader.readLine());
  28.  
  29.         List<Unit> units = new ArrayList<>();
  30.  
  31.         String currentUnit = reader.readLine();
  32.         while (!currentUnit.equals("Launch")) {
  33.             int[] tokens = Arrays.stream(currentUnit.split("\\s+"))
  34.                     .mapToInt(Integer::parseInt)
  35.                     .toArray();
  36.  
  37.             units.add(new Unit(tokens[0], tokens[1], tokens[2]));
  38.  
  39.             currentUnit = reader.readLine();
  40.         }
  41.  
  42.         int[][] dp = new int[units.size() + 1][medivacCapacity + 1];
  43.         boolean[][] unitsTaken = new boolean[units.size() + 1][medivacCapacity + 1];
  44.  
  45.         for (int unitRow = 1; unitRow <= units.size(); unitRow++) {
  46.             Unit unit = units.get(unitRow - 1);
  47.  
  48.             for (int capacityCol = 0; capacityCol <= medivacCapacity; capacityCol++) {
  49.                 int excluded = dp[unitRow - 1][capacityCol];
  50.  
  51.                 if(capacityCol - unit.capacity < 0) {
  52.                     dp[unitRow][capacityCol] = excluded;
  53.                 } else {
  54.                     int included = dp[unitRow - 1][capacityCol - unit.capacity] + unit.rating;
  55.  
  56.                     if(excluded > included) {
  57.                         dp[unitRow][capacityCol] = excluded;
  58.                     } else {
  59.                         dp[unitRow][capacityCol] = included;
  60.                         unitsTaken[unitRow][capacityCol] = true;
  61.                     }
  62.                 }
  63.             }
  64.         }
  65.  
  66.         int usedCapacity = medivacCapacity;
  67.         int bestRatingAchieved = dp[units.size()][medivacCapacity];
  68.  
  69.         while (dp[units.size()][usedCapacity - 1] == bestRatingAchieved) {
  70.             usedCapacity--;
  71.         }
  72.  
  73.         Set<Unit> unitsSent = new TreeSet<>();
  74.  
  75.         int lastUnit = units.size();
  76.  
  77.         while(lastUnit > 0) {
  78.             if(unitsTaken[lastUnit][medivacCapacity]) {
  79.                 Unit unit = units.get(lastUnit - 1);
  80.                 unitsSent.add(unit);
  81.                 medivacCapacity -= unit.capacity;
  82.             }
  83.             lastUnit--;
  84.         }
  85.  
  86.         System.out.println(usedCapacity);
  87.         System.out.println(bestRatingAchieved);
  88.  
  89.         for (Unit unit : unitsSent) {
  90.             System.out.println(unit.name);
  91.         }
  92.     }
  93. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement