Advertisement
SansPapyrus683

Apple Catching Solution

Mar 29th, 2022
1,041
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.*;
  2. import java.util.*;
  3.  
  4. public final class AppleCatching {
  5.     public static void main(String[] args) throws IOException {
  6.         BufferedReader read = new BufferedReader(new InputStreamReader(System.in));
  7.         int eventNum = Integer.parseInt(read.readLine());
  8.         ArrayList<Event> cows = new ArrayList<>();
  9.         ArrayList<Event> apples = new ArrayList<>();
  10.         for (int e = 0; e < eventNum; e++) {
  11.             StringTokenizer event = new StringTokenizer(read.readLine());
  12.             boolean cow = Integer.parseInt(event.nextToken()) == 1;
  13.             int time = Integer.parseInt(event.nextToken());
  14.             int loc = Integer.parseInt(event.nextToken());
  15.             double[] rotated = rotate45(time, loc);
  16.  
  17.             (cow ? cows : apples).add(new Event(
  18.                     (int) Math.round(rotated[0] * Math.sqrt(2)),
  19.                     (int) Math.round(rotated[1] * Math.sqrt(2)),
  20.                     Integer.parseInt(event.nextToken())
  21.             ));
  22.         }
  23.         Collections.sort(cows);
  24.         Collections.reverse(cows);
  25.         Collections.sort(apples);
  26.  
  27.         int canEat = 0;
  28.         TreeSet<Event> left = new TreeSet<>((a1, a2) -> a1.loc != a2.loc ? a1.loc - a2.loc : a1.time - a2.time);
  29.         int appleAt = apples.size() - 1;
  30.         for (Event c : cows) {
  31.             while (appleAt >= 0 && apples.get(appleAt).time >= c.time) {
  32.                 left.add(apples.get(appleAt));
  33.                 appleAt--;
  34.             }
  35.             Event a;
  36.             while ((a = left.ceiling(c)) != null && c.amt > 0) {
  37.                 int ate = Math.min(c.amt, a.amt);
  38.                 a.amt -= ate;
  39.                 c.amt -= ate;
  40.                 canEat += ate;
  41.                 if (a.amt == 0) {
  42.                     left.remove(a);
  43.                 }
  44.             }
  45.         }
  46.         System.out.println(canEat);
  47.     }
  48.  
  49.     // sauce: https://danceswithcode.net/engineeringnotes/rotations_in_2d/rotations_in_2d.html
  50.     public static double[] rotate45(int x, int y) {
  51.         final double val = Math.sqrt(2) / 2;  // sin(45) & cos(45)
  52.         return new double[] {x * val - y * val, x * val + y * val};
  53.     }
  54. }
  55.  
  56. class Event implements Comparable<Event> {
  57.     public int time;
  58.     public int loc;
  59.     public int amt;
  60.  
  61.     public Event(int time, int loc, int amt) {
  62.         this.time = time;
  63.         this.loc = loc;
  64.         this.amt = amt;
  65.     }
  66.  
  67.     @Override
  68.     public String toString() {
  69.         return String.format("(%d %d %d)", time, loc, amt);
  70.     }
  71.  
  72.     @Override
  73.     public int hashCode() {
  74.         return Objects.hash(time, loc, amt);
  75.     }
  76.  
  77.     @Override
  78.     public boolean equals(Object obj) {
  79.         if (obj.getClass() != getClass()) {
  80.             return false;
  81.         }
  82.         Event o = (Event) obj;
  83.         return time == o.time && loc == o.loc && amt == o.amt;
  84.     }
  85.  
  86.     @Override
  87.     public int compareTo(Event o) {
  88.         return o.time != time ? (time - o.time) : (loc - o.loc);
  89.     }
  90. }
  91.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement