Advertisement
Tsuki11

Judge

Mar 13th, 2020
335
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.43 KB | None | 0 0
  1. import java.util.*;
  2.  
  3. public class MoreExercise_2_Judge {
  4. public static void main(String[] args) {
  5. Scanner scan = new Scanner(System.in);
  6.  
  7. Map<String, LinkedHashMap<String, Integer>> contest = new LinkedHashMap<>();
  8.  
  9. String input = scan.nextLine();
  10. while (!"no more time".equals(input)) {
  11. String[] tokens = input.split("->");
  12. String userName = tokens[0].trim();
  13. String course = tokens[1].trim();
  14. int userPoints = Integer.parseInt(tokens[2].trim());
  15. LinkedHashMap <String,Integer> currentUser = new LinkedHashMap<>();
  16. currentUser.put(userName,userPoints);
  17.  
  18. if(contest.containsKey(course)){
  19. boolean hasUserName = contest.get(course).containsKey(userName);
  20. if(hasUserName){
  21. int pointsForThisContest = contest.get(course).get(userName);
  22. if(userPoints > pointsForThisContest){
  23. contest.get(course).put(userName,userPoints);
  24. }
  25. }
  26. if(!hasUserName){
  27. contest.get(course).put(userName,userPoints);
  28. }
  29. }
  30.  
  31. contest.putIfAbsent(course,currentUser);
  32. input = scan.nextLine();
  33. }
  34.  
  35. contest
  36. .entrySet()
  37. .forEach((entry)->{
  38. System.out.printf("%s: %d participants\n",entry.getKey(),entry.getValue().size());
  39. final int[] count1 = {0};
  40. entry.getValue()
  41. .entrySet()
  42. .stream()
  43. .sorted((k,v) ->{
  44. if(v.getValue() - k.getValue() != 0){
  45. return v.getValue() - k.getValue();
  46. }
  47. return k.getKey().compareTo(v.getKey());
  48. })
  49. .forEach(k-> {
  50. count1[0] += 1;
  51. System.out.printf("%d. %s <::> %d%n", count1[0],k.getKey(),k.getValue());
  52. });
  53. });
  54.  
  55. LinkedHashMap<String, Integer> users = new LinkedHashMap<>();
  56. for (LinkedHashMap<String, Integer> value : contest.values()) {
  57. value
  58. .entrySet()
  59. .forEach((k) ->{
  60. String name = k.getKey();
  61. int points = k.getValue();
  62. if(users.containsKey(name)){
  63. int newPoints = users.get(name) + points;
  64. users.put(name,newPoints);
  65. }
  66. users.putIfAbsent(name,points);
  67. });
  68. }
  69. System.out.println("Individual standings:");
  70. final int[] count = {0};
  71. users
  72. .entrySet()
  73. .stream()
  74. .sorted((a,b) -> {
  75.  
  76. if(b.getValue()- a.getValue() != 0){
  77. return b.getValue()-a.getValue();
  78. }
  79. return a.getKey().compareTo(b.getKey());
  80. })
  81. .forEach(k-> {
  82. count[0] += 1;
  83. System.out.printf("%d. %s -> %d%n", count[0],k.getKey(),k.getValue());
  84. });
  85. }
  86. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement