chillurbrain

Доп. из 13. Девайсы

May 26th, 2016
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.38 KB | None | 0 0
  1. import java.io.*;
  2. import java.util.*;
  3.  
  4. public class Main {
  5.  
  6.     public static void main(String[] args) throws IOException {
  7.         Map<String, List<Integer>> devices = new HashMap<>();
  8.         Scanner sc = new Scanner(System.in);
  9.         int maxDeviceCount = 0;
  10.         for (int i = 0; i < 6; i++) {
  11.             sc.nextLine();
  12.             String device = sc.nextLine();
  13.  
  14.             List<Integer> prices = devices.get(device);
  15.             if (prices == null) {
  16.                 prices = new ArrayList<>();
  17.                 devices.put(device, prices);
  18.             }
  19.  
  20.             prices.add(Integer.valueOf(sc.nextLine()));
  21.             if (prices.size() > maxDeviceCount) {
  22.                 maxDeviceCount = prices.size();
  23.             }
  24.         }
  25.  
  26.         String minDevice = null;
  27.         int minPrice = Integer.MAX_VALUE;
  28.  
  29.         for (Map.Entry<String, List<Integer>> e : devices.entrySet()) {
  30.             List<Integer> prices = e.getValue();
  31.             if (prices.size() == maxDeviceCount) {
  32.                 Collections.sort(prices);
  33.                 if (!prices.isEmpty()) {
  34.                     int price = prices.get(0);
  35.  
  36.                     if (price < minPrice) {
  37.                         minPrice = price;
  38.                         minDevice = e.getKey();
  39.                     }
  40.                 }
  41.             }
  42.         }
  43.         System.out.print(minDevice);
  44.     }
  45. }
Add Comment
Please, Sign In to add comment