Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package AssociativeArrays;
- import java.util.*;
- import java.util.stream.IntStream;
- public class Rankings {
- public static class Contests{
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- public int getPoints(){
- return points;
- }
- public void setPoints(int points){
- this.points=points;
- }
- String name;
- Integer points;
- Contests(String name,Integer points){
- this.name=name;
- this.points=points;
- }
- }
- public static void main(String[] args) {
- TreeMap<String,String> contestsPasswords=new TreeMap<>();
- Map<String, List<Contests>> usersContests=new TreeMap<>();
- Scanner scanner = new Scanner(System.in);
- String firstLines=scanner.nextLine();
- while (!firstLines.equals("end of contests")){
- if(firstLines.contains(":")){
- String[] input=firstLines.split(":");
- String contest=input[0];
- String password=input[1];
- contestsPasswords.put(contest,password);
- }
- firstLines=scanner.nextLine();
- }
- String secondLine=scanner.nextLine();
- while (!"end of submissions".equals(secondLine)){
- String[] input=secondLine.split("=>");
- String contest=input[0];
- String password=input[1];
- String user=input[2];
- int points=Integer.parseInt(input[3]);
- if(password.equals(contestsPasswords.get(contest))) {
- Contests contestUser = new Contests(contest, points);
- if (!usersContests.containsKey(user)) {
- usersContests.put(user, new ArrayList<>());
- usersContests.get(user).add(contestUser);
- } else{
- boolean isContained=false;
- for (Contests e : usersContests.get(user)) {
- if (e.getName().equals(contest)) {
- isContained = true;
- break;
- }
- }
- if(!isContained){
- usersContests.get(user).add(contestUser);
- }
- }
- for (Contests e : usersContests.get(user)) {
- if (e.getName().equals(contest)) {
- if (e.getPoints() < points) {
- e.setPoints(points);
- }
- }
- }
- }
- secondLine=scanner.nextLine();
- }
- Map<String,Integer>bestStudent=new HashMap<>();
- int max=0;
- int currentMax=0;
- String currentStudent="";
- for (Map.Entry<String, List<Contests>> stringListEntry : usersContests.entrySet()) {
- currentMax=stringListEntry.getValue().stream().map(Contests::getPoints)
- .mapToInt(Integer::intValue)
- .sum();
- if(max<currentMax){
- max=currentMax;
- currentStudent=stringListEntry.getKey();
- }
- }
- bestStudent.put(currentStudent,max);
- bestStudent.forEach((key, value) -> System.out.printf("Best candidate is %s with total %d points.%n",
- key, value));
- System.out.println("Ranking: ");
- usersContests.forEach((key, value) -> {
- System.out.printf("%s%n", key);
- value.stream()
- .sorted((a,b)->b.getPoints()-a.getPoints())
- .forEach(s -> System.out.printf("# %s -> %d%n", s.getName(), s.getPoints()));
- });
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement