Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- import java.util.concurrent.atomic.AtomicInteger;
- public class MoreEx2 {
- public static void main(String[] args) {
- Scanner scan = new Scanner(System.in);
- // A map to store the contests and their participants with points
- Map<String , Map<String, Integer>> contentUserPointsMap = new LinkedHashMap<>();
- // A map to store the participants and their total points
- Map<String , Integer> individualStatistic = new LinkedHashMap<>();
- // A list to store the participants in order of input
- List<String>
- orderOfInput = new ArrayList<>();
- String input = scan.nextLine();
- while(!input.equals("no more time")){
- // Split the input by " -> " to get the username, contest name and points
- String[] tokens = input.split(" -> ");
- String username = tokens[0];
- String contest = tokens[1];
- int points = Integer.parseInt(tokens[2]);
- // If the participant is not in the list, add them
- if (!orderOfInput.contains(username)) {
- orderOfInput.add(username);
- }
- // If the contest is not in the map, add it with an empty map as value
- contentUserPointsMap.putIfAbsent(contest,new HashMap<>());
- // Get the map of participants for this contest
- Map<String, Integer> contestParticipants = contentUserPointsMap.get(contest);
- // If the participant is already in the map, update their points if they are higher
- if (contestParticipants.containsKey(username)) {
- int currentPoints = contestParticipants.get(username);
- if (points > currentPoints) {
- // Update the contest score
- contestParticipants.put(username, points);
- // Update the total score by adding the difference
- individualStatistic.put(username, individualStatistic.get(username) + (points - currentPoints));
- }
- } else {
- // Otherwise, add the participant with their points
- contestParticipants.put(username, points);
- // Update the total score by adding the points
- individualStatistic.put(username, individualStatistic.getOrDefault(username, 0) + points);
- }
- input=scan.nextLine();
- }
- // For each contest in the map, print its name and number of participants
- contentUserPointsMap.forEach((contestName, contestParticipants) -> {
- System.out.printf("%s: %d participants%n",contestName,contestParticipants.size());
- // Sort the participants by points in descending order and by order of input in ascending order
- List<String> sortedParticipants = new ArrayList<>(contestParticipants.keySet());
- sortedParticipants.sort((a, b) -> {
- int pointsA = contestParticipants.get(a);
- int pointsB = contestParticipants.get(b);
- if (pointsA != pointsB) {
- return Integer.compare(pointsB, pointsA); // descending order of points
- } else {
- return Integer.compare(orderOfInput.indexOf(a), orderOfInput.indexOf(b)); // ascending order of input
- }
- });
- AtomicInteger num = new AtomicInteger();
- // Print the position, username and points of each participant
- sortedParticipants.forEach(username -> System.out.printf("%d. %s <::> %d%n", num.incrementAndGet(), username,
- contestParticipants.get(username)));
- });
- System.out.println("Individual standings:");
- // Sort the participants by total points in descending order and by order of input in ascending order
- List<Map.Entry<String , Integer>> sortedIndividualStatistics = new ArrayList<>(individualStatistic.entrySet());
- sortedIndividualStatistics.sort((a,b) -> {
- if (a.getValue() != b.getValue()) {
- return Integer.compare(b.getValue(), a.getValue()); // descending order of total points
- } else {
- return Integer.compare(orderOfInput.indexOf(a.getKey()), orderOfInput.indexOf(b.getKey())); // ascending order of input
- }
- });
- AtomicInteger num = new AtomicInteger();
- // Print the position, username and total points of each participant
- sortedIndividualStatistics.forEach(entry -> System.out.printf("%d. %s -> %d%n", num.incrementAndGet(), entry.getKey(),
- entry.getValue()));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement