Files

79 lines
2.2 KiB
Java
Raw Permalink Normal View History

2017-02-24 00:24:47 -06:00
// Author: Rodney Shaghoulian
2018-07-27 16:45:56 -07:00
// Github: github.com/RodneyShag
// HackerRank: hackerrank.com/RodneyShag
2017-02-24 00:24:47 -06:00
import java.util.Scanner;
import java.util.PriorityQueue;
import java.util.Comparator;
2017-04-05 18:42:35 -05:00
class Student {
2017-02-24 00:24:47 -06:00
private String fname;
private double cgpa;
private int token;
public Student(String fname, double cgpa, int token) {
this.fname = fname;
2017-04-05 18:42:35 -05:00
this.cgpa = cgpa;
2017-02-24 00:24:47 -06:00
this.token = token;
}
public int getToken() {
return token;
}
public String getFname() {
return fname;
}
public double getCgpa() {
return cgpa;
}
}
public class Solution {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int n = scan.nextInt();
/* Create PriorityQueue and add/remove entries */
PriorityQueue<Student> pq = new PriorityQueue<>(n, new StudentComparator());
while (n-- > 0) {
String event = scan.next();
switch (event) {
case "ENTER":
String fname = scan.next();
double cgpa = scan.nextDouble();
int token = scan.nextInt();
pq.add(new Student(fname, cgpa, token));
break;
case "SERVED":
pq.poll();
break;
default:
break;
}
}
scan.close();
/* Print Student names */
if (pq.isEmpty()) {
System.out.println("EMPTY");
} else {
2017-04-05 18:42:35 -05:00
while (!pq.isEmpty()) {
2017-02-24 00:24:47 -06:00
Student s = pq.remove();
System.out.println(s.getFname());
}
}
}
}
class StudentComparator implements Comparator<Student> {
2017-04-30 18:30:26 -07:00
double epsilon = 0.001; // since we shouldn't use "==" with doubles
2017-02-24 00:24:47 -06:00
@Override
public int compare(Student s1, Student s2) {
2017-04-30 18:30:26 -07:00
if (Math.abs(s1.getCgpa() - s2.getCgpa()) > epsilon) {
2017-02-24 00:24:47 -06:00
return s1.getCgpa() < s2.getCgpa() ? 1 : -1; // descending order
} else if ( ! s1.getFname().equals(s2.getFname())) {
return s1.getFname().compareTo(s2.getFname());
} else {
return s1.getToken() - s2.getToken();
}
}
}