Advertisement
Yahya-Ak-Ayoub

A_Rank_List

Oct 1st, 2020
286
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.21 KB | None | 0 0
  1. import java.io.BufferedReader;
  2. import java.io.IOException;
  3. import java.io.InputStreamReader;
  4. import static java.lang.Integer.parseInt;
  5. import java.util.ArrayList;
  6. import java.util.Collection;
  7. import java.util.Collections;
  8. import java.util.Comparator;
  9. import java.util.HashMap;
  10. import java.util.StringTokenizer;
  11.  
  12. public class A_Rank_List {
  13.  
  14.     public static void main(String[] args) throws IOException {
  15.         BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
  16.         StringTokenizer tok = new StringTokenizer(reader.readLine());
  17.  
  18.         int n = parseInt(tok.nextToken()), m = parseInt(tok.nextToken());
  19.  
  20.         HashMap<String, Integer> map = new HashMap<String, Integer>();
  21.  
  22.         for (int i = 0; i < n; i++) {
  23.  
  24.             String str = reader.readLine();
  25.  
  26.             if (map.containsKey(str)) {
  27.                 int x = (int) map.get(str);
  28.                 map.put(str, (x + 1));
  29.             } else {
  30.                 map.put(str, 1);
  31.             }
  32.         }
  33.  
  34.         ArrayList<pair> lis = new ArrayList<pair>();
  35.  
  36.         for (String i : map.keySet()) {
  37.             tok = new StringTokenizer(i);
  38.             int x = map.get(i);
  39.             pair p1 = new pair(parseInt(tok.nextToken()), parseInt(tok.nextToken()));
  40.             for (int j = 0; j < x; j++) {
  41.                 lis.add(p1);
  42.             }
  43.  
  44.         }
  45.         Collections.sort(lis);
  46.        System.out.println(map.get(lis.get(m-1).problem + " " + lis.get(m-1).penlty));
  47.        
  48.     }
  49.  
  50.     public static class pair implements Comparable<pair> {
  51.  
  52.         int problem, penlty;
  53.  
  54.         public pair(int problem, int penlty) {
  55.             this.problem = problem;
  56.             this.penlty = penlty;
  57.         }
  58.  
  59.         @Override
  60.         public int compareTo(pair o) {
  61.             if (this.problem < o.problem) {
  62.                 return 1;
  63.             } else if (this.problem > o.problem) {
  64.                 return -1;
  65.             } else if (this.penlty > o.penlty) {
  66.                 return 1;
  67.             } else if (this.penlty < o.penlty) {
  68.                 return -1;
  69.             } else {
  70.                 return 0;
  71.             }
  72.  
  73.         }
  74.     }
  75.  
  76. }
  77. /*
  78.  
  79. 7 2
  80. 4 10
  81. 4 10
  82. 4 10
  83. 3 20
  84. 2 1
  85. 2 1
  86. 1 10
  87.  
  88.  */
  89.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement