Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- public class _04_Agents1 {
- public static void main(String[] args) {
- Scanner sc = new Scanner(System.in);
- // mission, rate
- Map<String, Double> mission = new HashMap<>();
- List<String> agents = new ArrayList<>();
- String input = "";
- while (!"registration".equals(input = sc.nextLine())) {
- if (input.charAt(0) == '#') {
- String[] data = input.split(":");
- mission.putIfAbsent(data[0], 0.0);
- mission.put(data[0], Double.parseDouble(data[1]));
- } else if (input.charAt(input.length() - 3) == '0'){
- agents.add(input);
- }
- }
- // agent => mission, rate
- Map<String, Map<String, Double>> output = new LinkedHashMap<>();
- while (!"operate".equals(input = sc.nextLine())) {
- String[] data = input.split("->");
- switch (data[0]) {
- case "assign":
- String agent = data[1];
- String m = data[2];
- if (mission.containsKey(m) && agents.contains(agent)) {
- output.putIfAbsent(agent, new LinkedHashMap<>());
- }
- output.get(agent).putIfAbsent(m, 0.0);
- output.get(agent).put(m, mission.get(m));
- break;
- case "abort":
- for (Map.Entry<String, Map<String, Double>> mapEntry : output.entrySet()) {
- mapEntry.getValue().remove(data[1]);
- }
- break;
- case "change":
- String key1 = data[1];
- String key2 = data[2];
- String tempMainKey = "tempKey";
- output.put(tempMainKey, output.get(key1));
- output.put(key1, output.get(key2));
- output.put(key2, output.get(tempMainKey));
- output.remove(tempMainKey);
- break;
- }
- }
- output
- .entrySet()
- .stream()
- .filter(x -> x.getValue().size() > 0.0)
- .sorted((agent1, agent2) -> {
- double agent1Rating = agent1.getValue()
- .values()
- .stream()
- .mapToDouble(Double::doubleValue)
- .sum();
- // .reduce(Double::sum)
- // .get();
- double agent2Rating = agent2.getValue()
- .values()
- .stream()
- .mapToDouble(Double::doubleValue)
- .sum();
- // .reduce(Double::sum)
- // .get();
- return Double.compare(agent2Rating, agent1Rating);
- }).forEach(a -> {
- double agentRating = a.getValue()
- .values()
- .stream()
- .mapToDouble(Double::doubleValue)
- .sum();
- // .reduce(Double::sum)
- // .get();
- System.out.println(String.format("Agent: %s - Total Rating: %.2f", a.getKey(), agentRating));
- a.getValue()
- .entrySet()
- .stream()
- .sorted((one, two) -> two.getValue().compareTo(one.getValue()))
- .forEach(e -> {
- System.out.println(String.format(" - %s -> %.2f", e.getKey(), e.getValue()));
- });
- });
- }
- }
- //Условие:
- //https://judge.softuni.bg/Contests/Practice/Index/1145#3
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement