Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- public class Main {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- // A map to store the contests and their participants with points
- Map<String, Map<String, Integer>> contests = new LinkedHashMap<>();
- // A map to store the participants and their total points
- Map<String, Integer> participants = new HashMap<>();
- String input = scanner.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 contestName = tokens[1];
- int points = Integer.parseInt(tokens[2]);
- // If the contest is not in the map, add it with an empty map as value
- contests.putIfAbsent(contestName, new HashMap<>());
- // Get the map of participants for this contest
- Map<String, Integer> contestParticipants = contests.get(contestName);
- // 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
- participants.put(username, participants.get(username) + (points - currentPoints));
- }
- } else {
- // Otherwise, add the participant with their points
- contestParticipants.put(username, points);
- // Update the total score by adding the points
- participants.put(username, participants.getOrDefault(username, 0) + points);
- }
- input = scanner.nextLine();
- }
- // For each contest in the map, print its name and number of participants
- contests.forEach((contestName, contestParticipants) -> {
- System.out.println(contestName + ": " + contestParticipants.size() + " participants");
- // Sort the participants by points in descending order and by username 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 a.compareTo(b); // ascending order of usernames
- }
- });
- // Print the position, username and points of each participant
- for (int i = 0; i < sortedParticipants.size(); i++) {
- String username = sortedParticipants.get(i);
- int points = contestParticipants.get(username);
- System.out.println((i + 1) + ". " + username + " <::> " + points);
- }
- });
- System.out.println("Individual standings:");
- // Sort the participants by total points in descending order and by username in ascending order
- List<Map.Entry<String, Integer>> sortedParticipants = new ArrayList<>(participants.entrySet());
- sortedParticipants.sort((a, b) -> {
- if (a.getValue() != b.getValue()) {
- return Integer.compare(b.getValue(), a.getValue()); // descending order of total points
- } else {
- return a.getKey().compareTo(b.getKey()); // ascending order of usernames
- }
- });
- // Print the position, username and total points of each participant
- for (int i = 0; i < sortedParticipants.size(); i++) {
- String username = sortedParticipants.get(i).getKey();
- int totalPoints = sortedParticipants.get(i).getValue();
- System.out.println((i + 1) + ". " + username + " -> " + totalPoints);
- }
- scanner.close();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement