Advertisement
paykova

Ranking

Jun 11th, 2018
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.46 KB | None | 0 0
  1. import java.io.BufferedReader;
  2. import java.io.IOException;
  3. import java.io.InputStreamReader;
  4. import java.util.*;
  5. import java.util.stream.Collectors;
  6.  
  7. public class p04_Ranking {
  8. public static void main(String[] args) throws IOException {
  9. BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
  10.  
  11. Map<String, String> contestMap = new HashMap<>();
  12. Map<String, Map<String, Long>> map = new TreeMap<>();
  13.  
  14. String input = reader.readLine();
  15.  
  16. while (!"end of contests".equals(input)) {
  17. String[] splitInput = input.split(":");
  18. String contest = splitInput[0];
  19. String password = splitInput[1];
  20.  
  21. if (!contestMap.containsKey(contest)) {
  22. contestMap.put(contest, password);
  23. }
  24. input = reader.readLine();
  25. }
  26. //C# Fundamentals=>fundPass=>Tanya=>350
  27. String line = reader.readLine();
  28.  
  29. while (!"end of submissions".equals(line)) {
  30. String[] splitLine = line.split("=>");
  31.  
  32. String course = splitLine[0];
  33. String pass = splitLine[1];
  34. String name = splitLine[2];
  35. Long points = Long.parseLong(splitLine[3]);
  36.  
  37. if (!(contestMap.containsKey(course))) {
  38. line = reader.readLine();
  39. continue;
  40. }
  41. if (!contestMap.get(course).equals(pass)) {
  42. line = reader.readLine();
  43. continue;
  44. }
  45.  
  46. if (!map.containsKey(name)) {
  47. map.put(name, new HashMap<>());
  48. }
  49. if (!map.get(name).containsKey(course)) {
  50. map.get(name).put(course, 0L);
  51. }
  52. if (points > map.get(name).get(course)) {
  53. map.get(name).put(course, points);
  54. }
  55. line = reader.readLine();
  56. }
  57.  
  58. Map<String, Long> totals = map.entrySet()
  59. .stream()
  60. .map(e -> new AbstractMap.SimpleEntry<>(e.getKey(), e.getValue().entrySet().stream().mapToLong(Map.Entry::getValue).sum()))
  61. .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
  62. //System.out.println(totals);
  63.  
  64. String nameWithBestSum ="";
  65. long n1 =0L;
  66. for (String s : totals.keySet()) {
  67. nameWithBestSum = s;
  68. // System.out.println("This is what you need "+ nameWithBestSum);
  69. for (Long aLong : totals.values()) {
  70. n1 = aLong;
  71. //System.out.println("This is " + n1);
  72. break;
  73. }
  74. break;
  75. }
  76. System.out.printf("Best candidate is %s with total %d points.%n", nameWithBestSum, n1);
  77. System.out.println("Ranking: ");
  78.  
  79.  
  80. map.entrySet().stream()
  81. .forEach(s -> {
  82. System.out.println(s.getKey());
  83. map.entrySet().stream().sorted((k1, k2) -> Long.compare(k2.getValue().values().stream().mapToLong(Long::longValue).sum(),
  84. k1.getValue().values().stream().mapToLong(Long::longValue).sum())) ;
  85. s.getValue().entrySet().stream()
  86. .sorted((k1, k2) -> Long.compare(k2.getValue(),
  87. k1.getValue()))
  88. .forEach(a -> System.out.println("# " + a.getKey() + " -> " + a.getValue()));
  89. });
  90. }
  91. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement