Advertisement
CR7CR7

JudgeProblem

Jul 2nd, 2023
747
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.96 KB | None | 0 0
  1. import java.util.*;
  2. public class Main {
  3.     public static void main(String[] args) {
  4.         Scanner scanner = new Scanner(System.in);
  5.  
  6.     // A map to store the contests and their participants with points
  7.     Map<String, Map<String, Integer>> contests = new LinkedHashMap<>();
  8.     // A map to store the participants and their total points
  9.     Map<String, Integer> participants = new HashMap<>();
  10.  
  11.     String input = scanner.nextLine();
  12.     while (!input.equals("no more time")) {
  13.         // Split the input by " -> " to get the username, contest name and points
  14.         String[] tokens = input.split(" -> ");
  15.         String username = tokens[0];
  16.         String contestName = tokens[1];
  17.         int points = Integer.parseInt(tokens[2]);
  18.  
  19.         // If the contest is not in the map, add it with an empty map as value
  20.         contests.putIfAbsent(contestName, new HashMap<>());
  21.         // Get the map of participants for this contest
  22.         Map<String, Integer> contestParticipants = contests.get(contestName);
  23.  
  24.         // If the participant is already in the map, update their points if they are higher
  25.         if (contestParticipants.containsKey(username)) {
  26.             int currentPoints = contestParticipants.get(username);
  27.             if (points > currentPoints) {
  28.                 // Update the contest score
  29.                 contestParticipants.put(username, points);
  30.                 // Update the total score by adding the difference
  31.                 participants.put(username, participants.get(username) + (points - currentPoints));
  32.             }
  33.         } else {
  34.             // Otherwise, add the participant with their points
  35.             contestParticipants.put(username, points);
  36.             // Update the total score by adding the points
  37.             participants.put(username, participants.getOrDefault(username, 0) + points);
  38.         }
  39.  
  40.         input = scanner.nextLine();
  41.     }
  42.  
  43.     // For each contest in the map, print its name and number of participants
  44.     contests.forEach((contestName, contestParticipants) -> {
  45.         System.out.println(contestName + ": " + contestParticipants.size() + " participants");
  46.         // Sort the participants by points in descending order and by username in ascending order
  47.         List<String> sortedParticipants = new ArrayList<>(contestParticipants.keySet());
  48.         sortedParticipants.sort((a, b) -> {
  49.             int pointsA = contestParticipants.get(a);
  50.             int pointsB = contestParticipants.get(b);
  51.             if (pointsA != pointsB) {
  52.                 return Integer.compare(pointsB, pointsA); // descending order of points
  53.             } else {
  54.                 return a.compareTo(b); // ascending order of usernames
  55.             }
  56.         });
  57.         // Print the position, username and points of each participant
  58.         for (int i = 0; i < sortedParticipants.size(); i++) {
  59.             String username = sortedParticipants.get(i);
  60.             int points = contestParticipants.get(username);
  61.             System.out.println((i + 1) + ". " + username + " <::> " + points);
  62.         }
  63.     });
  64.  
  65.     System.out.println("Individual standings:");
  66.     // Sort the participants by total points in descending order and by username in ascending order
  67.     List<Map.Entry<String, Integer>> sortedParticipants = new ArrayList<>(participants.entrySet());
  68.     sortedParticipants.sort((a, b) -> {
  69.         if (a.getValue() != b.getValue()) {
  70.             return Integer.compare(b.getValue(), a.getValue()); // descending order of total points
  71.         } else {
  72.             return a.getKey().compareTo(b.getKey()); // ascending order of usernames
  73.         }
  74.     });
  75.     // Print the position, username and total points of each participant
  76.     for (int i = 0; i < sortedParticipants.size(); i++) {
  77.         String username = sortedParticipants.get(i).getKey();
  78.         int totalPoints = sortedParticipants.get(i).getValue();
  79.         System.out.println((i + 1) + ". " + username + " -> " + totalPoints);
  80.     }
  81.  
  82.     scanner.close();
  83. }
  84. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement