Created
October 11, 2019 22:10
-
-
Save MR-D05/4684a7f7a23143d514b27d9fe8f38229 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.ArrayList; | |
import java.util.List; | |
import java.util.Scanner; | |
public class Student { | |
String name; | |
double cpga; | |
int id; | |
public Student(String name, double cpga, int id) { | |
this.id = id; | |
this.name = name; | |
this.cpga = cpga; | |
} | |
public String getName() { | |
return name; | |
} | |
public double getCpga() { | |
return cpga; | |
} | |
public int getId() { | |
return id; | |
} | |
} | |
public class Priorities { | |
public List<Student> getStudents(List<String> events) { | |
PriorityQueue<Student> priorityQueue0 = new PriorityQueue<Student>(Collections.reverseOrder(Comparator.comparingDouble(Student::getCpga))); | |
for (String event : events) { | |
String[] characteristics = event.split("\\s+"); | |
if (characteristics[0].equals("ENTER")) { | |
String name = characteristics[1]; | |
double cpga = Double.parseDouble(characteristics[2]); | |
int id = Integer.parseInt(characteristics[3]); | |
Student student = new Student(name, cpga, id); | |
priorityQueue0.add(student); | |
} | |
if (characteristics[0].equals("SERVED")) { | |
PriorityQueue<Student> priorityQueue1 = new PriorityQueue<Student>(Comparator.comparing(Student::getName)); | |
PriorityQueue<Student> priorityQueue2 = new PriorityQueue<Student>(Collections.reverseOrder(Comparator.comparing(Student::getId))); | |
for (Student student : priorityQueue0) { | |
Student next = priorityQueue0.remove(); | |
Double nextCgpa = next.getCpga(); | |
if (!priorityQueue0.isEmpty() && priorityQueue0.peek().getCpga() != nextCgpa) { | |
} else { | |
priorityQueue1.add(next); | |
} | |
for (Student student1 : priorityQueue1) { | |
Student next1 = priorityQueue1.remove(); | |
String nextName = next1.getName(); | |
if (!priorityQueue1.isEmpty() && !priorityQueue1.peek().getName().equals(nextName)) { | |
} else { | |
priorityQueue2.add(next1); | |
} | |
for (Student student2 : priorityQueue2) { | |
priorityQueue0.offer(student); | |
} | |
} | |
} | |
} | |
} | |
List<Student> allStudents = new ArrayList<Student>(priorityQueue0); | |
return allStudents; | |
} | |
} | |
public class Solution { | |
private final static Scanner scan = new Scanner(System.in); | |
private final static Priorities priorities = new Priorities(); | |
public static void main(String[] args) { | |
int totalEvents = Integer.parseInt(scan.nextLine()); | |
List<String> events = new ArrayList<>(); | |
while (totalEvents-- != 0) { | |
String event = scan.nextLine(); | |
events.add(event); | |
} | |
List<Student> students = priorities.getStudents(events); | |
if (students.isEmpty()) { | |
System.out.println("EMPTY"); | |
} else { | |
for (Student st: students) { | |
System.out.println(st.getName()); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment