Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.company;
- import java.lang.reflect.Array;
- import java.util.*;
- public class Ranking {
- public static void main(String[] args) {
- Scanner sc = new Scanner(System.in);
- HashMap<String, String> contests = new HashMap<>();
- HashMap<String, Integer> bestResults = new HashMap<>();
- String input = sc.nextLine();
- while (!input.toLowerCase().equals("end of contests")) {
- String[] tokens = input.split("\\:");
- contests.putIfAbsent(tokens[0], tokens[1]);
- input = sc.nextLine();
- }
- Map<String, Map<String, Integer>> users = new TreeMap<>();
- String otherInput = sc.nextLine();
- while (!otherInput.toLowerCase().equals("end of submissions")) {
- String[] tokens = otherInput.split("=>");
- String contest = tokens[0];
- String password = tokens[1];
- String username = tokens[2];
- int points = Integer.parseInt(tokens[3]);
- if (contests.containsKey(contest) && contests.get(contest).equals(password)) {
- if (!users.containsKey(username)) {
- users.put(username, new TreeMap<String, Integer>());
- users.get(username).put(contest, points);
- bestResults.put(username, points);
- } else {
- if (users.get(username).containsKey(contest)) {
- int currPoints = users.get(username).get(contest);
- if (points > currPoints) {
- users.get(username).put(contest, points);
- bestResults.put(username, bestResults.get(username) + points - currPoints);
- }
- } else {
- int currPoints2 = bestResults.get(username);
- users.get(username).put(contest, points);
- bestResults.put(username, currPoints2 + points);
- }
- }
- }
- otherInput = sc.nextLine();
- }
- int maxPoints = Collections.max(bestResults.values());
- for (Map.Entry<String, Integer> entry : bestResults.entrySet()) {
- if (entry.getValue() == maxPoints) {
- System.out.printf("Best candidate is %s with total %d points.%n", entry.getKey(), entry.getValue());
- }
- }
- System.out.println("Ranking: ");
- users.entrySet().stream().
- forEach(e -> {
- System.out.println(e.getKey());
- e.getValue().entrySet().stream().sorted((a, b) -> b.getValue().compareTo(a.getValue()))
- .forEach(c -> System.out.printf("# %s -> %d%n", c.getKey(), c.getValue()));
- });
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement