Advertisement
Lyubohd

04. Ranking

Jun 11th, 2018
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.03 KB | None | 0 0
  1. import java.io.BufferedReader;
  2. import java.io.IOException;
  3. import java.io.InputStreamReader;
  4. import java.util.HashMap;
  5. import java.util.LinkedHashMap;
  6. import java.util.Map;
  7. import java.util.TreeMap;
  8.  
  9. public class P04_RankingProblemDescription {
  10.     public static void main(String[] args) throws IOException {
  11.         BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
  12.  
  13.         Map<String, String> contests = new HashMap<>();
  14.         while (true) {
  15.             String input = reader.readLine();
  16.             if ("end of contests".equals(input)) {
  17.                 break;
  18.             }
  19.             String[] tokens = input.split(":");
  20.             String contest = tokens[0];
  21.             String pass = tokens[1];
  22.             contests.putIfAbsent(contest, pass);
  23.         }
  24.  
  25.         Map<String, Map<String, Integer>> people = new TreeMap<>();
  26.  
  27.         while (true) {
  28.             String input = reader.readLine();
  29.             if ("end of submissions".equals(input)) {
  30.                 break;
  31.             }
  32.             String[] tokens = input.split("=>");
  33.             String contest = tokens[0];
  34.             String pass = tokens[1];
  35.             String candidate = tokens[2];
  36.             Integer points = Integer.parseInt(tokens[3]);
  37.             if (contests.containsKey(contest) && contests.get(contest).equals(pass)) {
  38.                 if (!people.containsKey(candidate)) {
  39.                     people.put(candidate, new LinkedHashMap<>());
  40.                     people.get(candidate).put(contest, points);
  41.                 } else {
  42.                     if (!people.get(candidate).containsKey(contest)) {
  43.                         people.get(candidate).put(contest, points);
  44.                     } else {
  45.                         if (points > people.get(candidate).get(contest)) {
  46.                             people.get(candidate).put(contest, points);
  47.                         }
  48.                     }
  49.                 }
  50.             }
  51.         }
  52.         String bestPerson = "";
  53.         int bestScore = Integer.MIN_VALUE;
  54.         for (Map.Entry<String, Map<String, Integer>> person : people.entrySet()) {
  55.             String currentPerson = person.getKey();
  56.             int personScore = 0;
  57.             for (Map.Entry<String, Integer> personStats : person.getValue().entrySet()) {
  58.                 personScore += personStats.getValue();
  59.             }
  60.             if (personScore > bestScore) {
  61.                 bestScore = personScore;
  62.                 bestPerson = currentPerson;
  63.             }
  64.         }
  65.         System.out.println(String.format("Best candidate is %s with total %d points.", bestPerson, bestScore));
  66.         System.out.println("Ranking:");
  67.  
  68.         people.forEach((key, value) -> {
  69.             System.out.println(key);
  70.             value.entrySet()
  71.                     .stream()
  72.                     .sorted((first, second) -> -first.getValue().compareTo(second.getValue()))
  73.                     .forEach(contest -> System.out.printf("#  %s -> %d%n", contest.getKey().trim(), contest.getValue()));
  74.         });
  75.     }
  76. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement