kalinikov

02. Judge

Nov 11th, 2019
152
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.13 KB | None | 0 0
  1. import java.util.LinkedHashMap;
  2. import java.util.Map;
  3. import java.util.Scanner;
  4. import java.util.TreeMap;
  5.  
  6. public class Judge {
  7. public static void main(String[] args) {
  8. Scanner scanner = new Scanner(System.in);
  9.  
  10. Map<String, TreeMap<String, Integer>> contests = new LinkedHashMap<>();
  11. Map<String, Integer> users = new TreeMap<>();
  12.  
  13. String input = scanner.nextLine();
  14.  
  15. while (!input.equals("no more time")) {
  16. String[] tokens = input.split(" -> ");
  17. String username = tokens[0];
  18. String contestName = tokens[1];
  19. int points = Integer.parseInt(tokens[2]);
  20.  
  21. if (!contests.containsKey(contestName)) {
  22. contests.put(contestName, new TreeMap<>());
  23. contests.get(contestName).put(username, points);
  24. } else if (contests.get(contestName).containsKey(username)
  25. && points > contests.get(contestName).get(username)) {
  26. contests.get(contestName).put(username, points);
  27. } else {
  28. contests.get(contestName).put(username, points);
  29. }
  30.  
  31. input = scanner.nextLine();
  32. }
  33.  
  34. if (!contests.isEmpty()) {
  35.  
  36.  
  37. contests.entrySet()
  38. .stream()
  39. .forEach(e -> {
  40. System.out.printf("%s: %d participants%n",
  41. e.getKey(), e.getValue().size());
  42. final int[] counter = {0};
  43.  
  44. e.getValue().entrySet()
  45. .stream()
  46. .sorted((f, s) -> s.getValue() - f.getValue())
  47. .forEach(a -> {
  48. counter[0]++;
  49. System.out.printf("%d. %s <::> %d%n",
  50. counter[0], a.getKey(), a.getValue());
  51.  
  52. });
  53. counter[0] = 0;
  54.  
  55. });
  56.  
  57. System.out.println("Individual standings:");
  58.  
  59.  
  60. contests.entrySet()
  61. .stream()
  62. .forEach(e -> {
  63. e.getValue().entrySet()
  64. .stream()
  65. .forEach(b -> {
  66. if (!users.containsKey(b.getKey())) {
  67. users.put(b.getKey(), b.getValue());
  68. } else {
  69. users.put(b.getKey(), users.get(b.getKey()) + b.getValue());
  70. }
  71. });
  72. });
  73.  
  74. final int[] position = {1};
  75. users.entrySet().stream()
  76. .sorted((f, s) -> s.getValue() - f.getValue())
  77. .forEach(x -> {
  78.  
  79. System.out.printf("%d. %s -> %d%n",
  80. position[0], x.getKey(), x.getValue());
  81. position[0]++;
  82. });
  83. }
  84.  
  85. }
  86. }
Advertisement
Add Comment
Please, Sign In to add comment