Advertisement
Krassi_Daskalova

Ranking

Aug 5th, 2019
227
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.38 KB | None | 0 0
  1. import java.util.*;
  2.  
  3. public class Ranking {
  4. public static void main(String[] args) {
  5. Scanner scanner = new Scanner(System.in);
  6. Map<String, String> contestsAndPass = new LinkedHashMap<>();
  7. Map<String, Map<String, Integer>> userContestPoints = new TreeMap<>();
  8.  
  9. String input = scanner.nextLine();
  10. while (!input.equals("end of contests")) {
  11. String[] contests = input.split(":");
  12. String contest = contests[0];
  13. String password = contests[1];
  14. contestsAndPass.put(contest, password);
  15.  
  16. input = scanner.nextLine();
  17. }
  18. String submission = scanner.nextLine();
  19. while (!submission.equals("end of submissions")) {
  20. String[] subm = submission.split("=>");
  21. String contestName = subm[0];
  22. String passWord = subm[1];
  23. String username = subm[2];
  24. int points = Integer.parseInt(subm[3]);
  25.  
  26. if (contestsAndPass.containsKey(contestName) && passWord.equals(contestsAndPass.get(contestName))) {
  27. if (!userContestPoints.containsKey(username)) {
  28. userContestPoints.put(username, new TreeMap<>());
  29. userContestPoints.get(username).put(contestName, points);
  30. } else {
  31. if (!userContestPoints.get(username).containsKey(contestName)) {
  32. userContestPoints.get(username).put(contestName, points);
  33. } else {
  34. int currPoints = userContestPoints.get(username).get(contestName);
  35. if (currPoints < points) {
  36. userContestPoints.get(username).put(contestName, points);
  37. }
  38. }
  39. }
  40. }
  41. submission = scanner.nextLine();
  42. }
  43. String bestStudent = "";
  44. int highestPoints = 0;
  45. int currPoints = 0;
  46.  
  47. for (Map.Entry<String, Map<String, Integer>> entry : userContestPoints.entrySet()) {
  48. for (Map.Entry<String, Integer> entry2 : entry.getValue().entrySet()) {
  49. currPoints += entry2.getValue();
  50. }
  51. if (currPoints > highestPoints) {
  52. highestPoints = currPoints;
  53. currPoints = 0;
  54. bestStudent = entry.getKey();
  55. }
  56. }
  57. System.out.printf("Best candidate is %s with total %s points.%n", bestStudent, highestPoints);
  58. System.out.println("Ranking: ");
  59. List<Map.Entry<String, Map<String, Integer>>> listToSort = new ArrayList<>();
  60. for (Map.Entry<String, Map<String, Integer>> entry : userContestPoints.entrySet()) {
  61. listToSort.add(entry);
  62. }
  63. listToSort.sort(Comparator.comparing(Map.Entry::getKey));
  64. for (Map.Entry<String, Map<String, Integer>> entry : listToSort) {
  65. System.out.println(entry.getKey());
  66. List<Map.Entry<String, Integer>> toSort = new ArrayList<>();
  67. for (Map.Entry<String, Integer> a : entry.getValue().entrySet()) {
  68. toSort.add(a);
  69. }
  70. toSort.sort((a, b) -> b.getValue().compareTo(a.getValue()));
  71. for (Map.Entry<String, Integer> a : toSort) {
  72. System.out.printf("# %s -> %d\n", a.getKey(), a.getValue());
  73. }
  74. }
  75. }
  76. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement