Advertisement
CR7CR7

JudgeMessage8

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