Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package AssociativeArrays;
- import com.sun.source.tree.Tree;
- import java.util.*;
- import java.util.stream.Collector;
- import java.util.stream.Collectors;
- public class MOBAChallenger {
- public static String commonPosition(Map<String, TreeMap<String, Integer>> userPositions, String player1, String player2) {
- TreeMap<String, Integer> stringIntegerTreeMap = userPositions.get(player1);
- for (String s : stringIntegerTreeMap.keySet()) {
- if (userPositions.get(player2).containsKey(s)) {
- return "Common";
- }
- }
- return "Notcommon";
- }
- public static String Position(Map<String, TreeMap<String, Integer>> userPositions, String player1, String player2) {
- TreeMap<String, Integer> stringIntegerTreeMap = userPositions.get(player1);
- for (String s : stringIntegerTreeMap.keySet()) {
- if (userPositions.get(player2).containsKey(s)) {
- return s;
- }
- }
- return "";
- }
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- String input = scanner.nextLine();
- Map<String, TreeMap<String, Integer>> userPositions = new LinkedHashMap<>();
- while (!"Season end".equals(input)) {
- if (input.contains("->")) {
- String[] tokens = input.split(" -> ");
- String player = tokens[0];
- String position = tokens[1];
- int points = Integer.parseInt(tokens[2]);
- if (!userPositions.containsKey(player)) {
- userPositions.put(player, new TreeMap<>());
- userPositions.get(player).put(position, points);
- } else if (!userPositions.get(player).containsKey(position)) {
- userPositions.get(player).put(position, points);
- } else {
- if (userPositions.get(player).get(position) < points) {
- userPositions.get(player).put(position, points);
- }
- }
- } else {
- String[] battles = input.split(" vs ");
- String player1 = battles[0];
- String player2 = battles[1];
- if (userPositions.containsKey(player1) && userPositions.containsKey(player2)) {
- if (commonPosition(userPositions, player1, player2).equals("Common")) {
- String s = Position(userPositions, player1, player2);
- if (userPositions.get(player1).get(s) > userPositions.get(player2).get(s)) {
- userPositions.remove(player2);
- } else if (userPositions.get(player1).get(s) < userPositions.get(player2).get(s)) {
- userPositions.remove(player1);
- }
- }
- }
- }
- input = scanner.nextLine();
- }
- // Map<String,Integer> sorted=new LinkedHashMap<>();
- // for (Map.Entry<String, TreeMap<String, Integer>> stringTreeMapEntry : userPositions.entrySet()) {
- // Map<String, Integer> finalSorted = sorted;
- // stringTreeMapEntry.getValue().forEach((e1, e2)->{
- // int sum=stringTreeMapEntry.getValue().values().stream().mapToInt(x->x).sum();
- // finalSorted.putIfAbsent(stringTreeMapEntry.getKey(),sum);
- // });
- // }
- // sorted= sorted.entrySet().stream().sorted((a,b)->b.getValue().compareTo(a.getValue()))
- // .collect(Collectors.toMap(Map.Entry::getKey,Map.Entry::getValue, (e1,e2)->e1,LinkedHashMap::new));
- // sorted.forEach((key,value)-> {
- // System.out.printf("%s: %d skill%n",key,value);
- // userPositions.get(key).entrySet().stream().sorted((a,b)->{
- // int res= b.getValue().compareTo(a.getValue());
- // if(res==0)
- // res = a.getKey().compareTo(b.getKey());
- //
- // return res ; })
- // .forEach(e-> System.out.printf("- %s <::> %d%n",e.getKey(),e.getValue()));
- // });
- userPositions.entrySet().stream()
- .sorted((e1, e2) -> {
- int sum1 = e1.getValue().values().stream().mapToInt(Integer::intValue).sum();
- int sum2 = e2.getValue().values().stream().mapToInt(Integer::intValue).sum();
- if (sum1 == sum2) {
- return e1.getKey().compareTo(e2.getKey());
- } else {
- return sum2 - sum1;
- }
- })
- .forEach((entry -> {
- int sum = entry.getValue().values().stream().mapToInt(Integer::intValue).sum();
- System.out.printf("%s: %d skill%n", entry.getKey(), sum);
- entry.getValue().entrySet().stream()
- .sorted((e1, e2) -> {
- if (e1.getValue().equals(e2.getValue())) {
- return e1.getKey().compareTo(e2.getKey());
- } else {
- return e2.getValue().compareTo(e1.getValue());
- }
- })
- .forEach(e -> System.out.printf("- %s <::> %d%n", e.getKey(), e.getValue()));
- }));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement