Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package AssociativeArraysMoreExercise;
- import java.util.*;
- public class Ranking {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- Map<String, String> contestPass = new HashMap<>();
- Map<String, Map<String, Integer>> contestsWithPoints = new TreeMap<>();
- Map<String, Integer> countPoints = new HashMap<>();
- String input = "";
- while (!"end of contests".equals(input = scanner.nextLine())) {
- String[] data = input.split(":");
- String contest = data[0];
- String password = data[1];
- contestPass.put(contest, password);
- }
- String input2 = "";
- while (!"end of submissions".equals(input2 = scanner.nextLine())) {
- String[] data2 = input2.split("=>");
- String contest = data2[0];
- String password = data2[1];
- String userName = data2[2];
- int points = Integer.parseInt(data2[3]);
- if (contestPass.containsKey(contest) && contestPass.get(contest).equals(password)) {
- if (!contestsWithPoints.containsKey(userName)) {
- contestsWithPoints.put(userName, new LinkedHashMap<>());
- contestsWithPoints.get(userName).put(contest, points);
- countPoints.put(userName, points);
- } else {
- if (!contestsWithPoints.get(userName).containsKey(contest)) {
- contestsWithPoints.get(userName).put(contest, points);
- countPoints.put(userName, countPoints.get(userName) + points);
- } else {
- if (contestsWithPoints.get(userName).get(contest) < points) {
- int diff = points - contestsWithPoints.get(userName).get(contest);
- countPoints.put(userName, countPoints.get(userName) + diff);
- contestsWithPoints.get(userName).put(contest, points);
- }
- }
- }
- }
- }
- countPoints.entrySet().stream()
- .sorted(Map.Entry.<String, Integer>comparingByValue().reversed())
- .limit(1)
- .forEach(e -> System.out.println(String.format("Best candidate is %s with total %d points.", e.getKey(), e.getValue())));
- System.out.println("Ranking: ");
- contestsWithPoints.forEach((key, value) -> {
- System.out.println(key);
- value.entrySet().stream()
- .sorted((e1, e2) -> {
- int sort = Integer.compare(e2.getValue(), e1.getValue());
- return sort;
- })
- .forEach(e-> System.out.println(String.format("# %s -> %d",e.getKey(),e.getValue())));
- });
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement