Filip_Markoski

[ADS] Black Friday

Dec 14th, 2017
570
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.76 KB | None | 0 0
  1. import java.text.ParseException;
  2. import java.text.SimpleDateFormat;
  3. import java.time.LocalTime;
  4. import java.time.format.DateTimeFormatter;
  5. import java.util.*;
  6.  
  7.  
  8. import java.util.Comparator;
  9.  
  10. class Heap<E extends Comparable<E>> {
  11.  
  12.     private E elements[];
  13.  
  14.     private Comparator<? super E> comparator;
  15.  
  16.     private int compare(E k1, E k2) {
  17.         return (comparator == null ? k1.compareTo(k2) : comparator.compare(k1, k2));
  18.     }
  19.  
  20.     int getParent(int i) {
  21.         return (i + 1) / 2 - 1;
  22.     }
  23.  
  24.     public E getAt(int i) {
  25.         return elements[i];
  26.     }
  27.  
  28.     int getLeft(int i) {
  29.         return (i + 1) * 2 - 1;
  30.     }
  31.  
  32.     int getRight(int i) {
  33.         return (i + 1) * 2;
  34.     }
  35.  
  36.     void setElement(int index, E elem) {
  37.         elements[index] = elem;
  38.     }
  39.  
  40.     void swap(int i, int j) {
  41.         E tmp = elements[i];
  42.         elements[i] = elements[j];
  43.         elements[j] = tmp;
  44.     }
  45.  
  46.     void adjust(int i, int n) {
  47.  
  48.         while (i < n) {
  49.  
  50.             int left = getLeft(i);
  51.             int right = getRight(i);
  52.             int largest = i;
  53.  
  54.             if ((left < n) && (elements[left].compareTo(elements[largest]) > 0))
  55.                 largest = left;
  56.             if ((right < n) && (elements[right].compareTo(elements[largest]) > 0))
  57.                 largest = right;
  58.  
  59.             if (largest == i)
  60.                 break;
  61.  
  62.             swap(i, largest);
  63.             i = largest;
  64.  
  65.         }
  66.  
  67.     }
  68.  
  69.     void buildHeap() {
  70.         int i;
  71.         for (i = elements.length / 2 - 1; i >= 0; i--)
  72.             adjust(i, elements.length);
  73.     }
  74.  
  75.     public void heapSort() {
  76.         int i;
  77.         buildHeap();
  78.         for (i = elements.length; i > 1; i--) {
  79.             swap(0, i - 1);
  80.             adjust(0, i - 1);
  81.         }
  82.     }
  83.  
  84.     @SuppressWarnings("unchecked")
  85.     public Heap(int size) {
  86.         elements = (E[]) new Comparable[size];
  87.     }
  88.  
  89.  
  90. }
  91.  
  92.  
  93. class Buyer implements Comparable<Buyer> {
  94.     LocalTime arrival;
  95.     LocalTime departure;
  96.     int minutesToStay;
  97.  
  98.     public Buyer(LocalTime arival, LocalTime departure) {
  99.         this.arrival = arival;
  100.         this.departure = departure;
  101.     }
  102.  
  103.     public Buyer(LocalTime arrival, int minutesToStay) {
  104.         this.arrival = arrival;
  105.         this.departure = arrival.plusMinutes(minutesToStay);
  106.  
  107.         this.minutesToStay = minutesToStay;
  108.     }
  109.  
  110.     @Override
  111.     public int compareTo(Buyer that) {
  112.         if (this.departure.isAfter(that.arrival)) {
  113.             return 1;
  114.         } else if (this.departure.isBefore(that.arrival)) {
  115.             return -1;
  116.         }
  117.         return 0;
  118.     }
  119.  
  120.     @Override
  121.     public String toString() {
  122.         return "\nBuyer{" +
  123.                 "arrival=" + arrival +
  124.                 ", departure=" + departure +
  125.                 ", minutesToStay=" + minutesToStay +
  126.                 "}";
  127.     }
  128. }
  129.  
  130. public class BlackFriday {
  131.     public static void main(String args[]) throws ParseException {
  132.         Scanner scanner = new Scanner(System.in);
  133.         int numBuyers = Integer.parseInt(scanner.nextLine());
  134.  
  135.         /* Unnecessary */
  136.         SimpleDateFormat simpleDateFormat = new SimpleDateFormat("HH:mm");
  137.         DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("HH:mm");
  138.  
  139.         //Queue<Buyer> heap = new PriorityQueue<>(numBuyers);
  140.         //ArrayList<Buyer> arrayList = new ArrayList<>(numBuyers);
  141.        
  142.         for (int i = 0; i < numBuyers; i++) {
  143.             String parts[] = scanner.nextLine().split(" ");
  144.             LocalTime arrival = LocalTime.parse(parts[0]);
  145.             int minutesToStay = Integer.parseInt(parts[1]);
  146.  
  147.             Buyer buyer = new Buyer(arrival, minutesToStay);
  148.            
  149.         }
  150.  
  151.         System.out.println(numBuyers);
  152.  
  153.     }
  154.  
  155.  
  156. }
Advertisement
Add Comment
Please, Sign In to add comment