Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package MoreExercise;
- import java.io.BufferedReader;
- import java.io.IOException;
- import java.io.InputStreamReader;
- import java.util.*;
- public class Ranking_01 {
- public static void main(String[] args) throws IOException {
- BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
- Map<String, String> contestsMap = new HashMap<>();
- String strInput = "";
- while (!"end of contests".equals(strInput = reader.readLine())) {
- String[] input = strInput.split(":");
- contestsMap.putIfAbsent(input[0], input[1]);
- }
- Map<String, TreeMap<String, Integer>> data = new TreeMap<>();
- String strCommand = "";
- while (!"end of submissions".equals(strCommand = reader.readLine())) {
- String[] command = strCommand.split("=>");
- String contest = command[0];
- String password = command[1];
- String username = command[2];
- int points = Integer.parseInt(command[3]);
- if (contestsMap.containsKey(contest) && contestsMap.get(contest).equals(password)) {
- if (!data.containsKey(username)) {
- data.put(username, new TreeMap<>());
- data.get(username).putIfAbsent(contest, points);
- } else {
- data.get(username).putIfAbsent(contest, points);
- if (data.get(username).get(contest) < points) {
- data.get(username).put(contest, points);
- }
- }
- }
- }
- Map<String, Integer> max = new LinkedHashMap<>();
- data.forEach((key, value) -> {
- max.putIfAbsent(key, 0);
- value.forEach((key2, value2) -> max.put(key, max.get(key) + value2));
- });
- int maxValueInMap = Collections.max(max.values());
- max.forEach((key, value) -> {
- if (value == maxValueInMap) {
- System.out.println(String.format("Best candidate is %s with total %d points.",key, maxValueInMap));
- }
- });
- System.out.println("Ranking: ");
- data.forEach((key, value) -> {
- System.out.println(key);
- value.entrySet().stream().sorted((e1, e2) -> Integer.compare(e2.getValue(), e1.getValue()))
- .forEach(e -> System.out.println(String.format("# %s -> %d",e.getKey(), e.getValue())));
- });
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement