Advertisement
damesova

Agents [90of100]

May 7th, 2019
234
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.80 KB | None | 0 0
  1. import java.util.*;
  2.  
  3. public class _04_Agents1 {
  4.     public static void main(String[] args) {
  5.         Scanner sc = new Scanner(System.in);
  6.  
  7.         //  mission, rate
  8.         Map<String, Double> mission = new HashMap<>();
  9.         List<String> agents = new ArrayList<>();
  10.  
  11.         String input = "";
  12.         while (!"registration".equals(input = sc.nextLine())) {
  13.             if (input.charAt(0) == '#') {
  14.                 String[] data = input.split(":");
  15.                 mission.putIfAbsent(data[0], 0.0);
  16.                 mission.put(data[0], Double.parseDouble(data[1]));
  17.             } else if (input.charAt(input.length() - 3) == '0'){
  18.                 agents.add(input);
  19.             }
  20.         }
  21.  
  22.         //  agent   =>  mission,    rate
  23.         Map<String, Map<String, Double>> output = new LinkedHashMap<>();
  24.  
  25.         while (!"operate".equals(input = sc.nextLine())) {
  26.             String[] data = input.split("->");
  27.  
  28.             switch (data[0]) {
  29.                 case "assign":
  30.                     String agent = data[1];
  31.                     String m = data[2];
  32.                     if (mission.containsKey(m) && agents.contains(agent)) {
  33.                         output.putIfAbsent(agent, new LinkedHashMap<>());
  34.                     }
  35.                     output.get(agent).putIfAbsent(m, 0.0);
  36.                     output.get(agent).put(m, mission.get(m));
  37.                     break;
  38.                 case "abort":
  39.                     for (Map.Entry<String, Map<String, Double>> mapEntry : output.entrySet()) {
  40.                         mapEntry.getValue().remove(data[1]);
  41.                     }
  42.                     break;
  43.                 case "change":
  44.                     String key1 = data[1];
  45.                     String key2 = data[2];
  46.                     String tempMainKey = "tempKey";
  47.                     output.put(tempMainKey, output.get(key1));
  48.                     output.put(key1, output.get(key2));
  49.                     output.put(key2, output.get(tempMainKey));
  50.                     output.remove(tempMainKey);
  51.                     break;
  52.             }
  53.         }
  54.  
  55.         output
  56.                 .entrySet()
  57.                 .stream()
  58.                 .filter(x -> x.getValue().size() > 0.0)
  59.                 .sorted((agent1, agent2) -> {
  60.                     double agent1Rating = agent1.getValue()
  61.                             .values()
  62.                             .stream()
  63.                             .mapToDouble(Double::doubleValue)
  64.                             .sum();
  65. //                            .reduce(Double::sum)
  66. //                            .get();
  67.                     double agent2Rating = agent2.getValue()
  68.                             .values()
  69.                             .stream()
  70.                             .mapToDouble(Double::doubleValue)
  71.                             .sum();
  72. //                            .reduce(Double::sum)
  73. //                            .get();
  74.  
  75.                     return Double.compare(agent2Rating, agent1Rating);
  76.                 }).forEach(a -> {
  77.             double agentRating = a.getValue()
  78.                     .values()
  79.                     .stream()
  80.                     .mapToDouble(Double::doubleValue)
  81.                     .sum();
  82. //                    .reduce(Double::sum)
  83. //                    .get();
  84.  
  85.             System.out.println(String.format("Agent: %s - Total Rating: %.2f", a.getKey(), agentRating));
  86.  
  87.             a.getValue()
  88.                     .entrySet()
  89.                     .stream()
  90.                     .sorted((one, two) -> two.getValue().compareTo(one.getValue()))
  91.                     .forEach(e -> {
  92.                         System.out.println(String.format(" - %s -> %.2f", e.getKey(), e.getValue()));
  93.                     });
  94.         });
  95.     }
  96. }
  97.  
  98. //Условие:
  99. //https://judge.softuni.bg/Contests/Practice/Index/1145#3
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement