Advertisement
Guest User

ConcertTickets TLE

a guest
Sep 22nd, 2019
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.09 KB | None | 0 0
  1. import java.io.*;
  2. import java.util.*;
  3.  
  4. public class ConcertTickets {
  5.  
  6.     static class InputReader {
  7.         BufferedReader reader;
  8.         StringTokenizer tokenizer;
  9.  
  10.         public InputReader(InputStream stream) {
  11.             reader = new BufferedReader(new InputStreamReader(stream), 32768);
  12.             tokenizer = null;
  13.         }
  14.  
  15.         String next() {
  16.             while (tokenizer == null || !tokenizer.hasMoreTokens()) {
  17.                 try {
  18.                     tokenizer = new StringTokenizer(reader.readLine());
  19.                 } catch (IOException e) {
  20.                     throw new RuntimeException(e);
  21.                 }
  22.             }
  23.             return tokenizer.nextToken();
  24.         }
  25.  
  26.         public int nextInt() {
  27.             return Integer.parseInt(next());
  28.         }
  29.  
  30.         public long nextLong() {
  31.             return Long.parseLong(next());
  32.         }
  33.  
  34.         public double nextDouble() {
  35.             return Double.parseDouble(next());
  36.         }
  37.     }
  38.  
  39.     static InputReader r = new InputReader(System.in);
  40.     static PrintWriter pw = new PrintWriter(System.out);
  41.  
  42.     static TreeMap<Integer, Integer> prices = new TreeMap<Integer, Integer>();
  43.  
  44.     public static void main(String[] args) {
  45.  
  46.         int n = r.nextInt(); // # tickets
  47.         int m = r.nextInt(); // # customers
  48.         for(int i = 1; i <= n; i++){
  49.             add(r.nextInt());
  50.         }
  51.         for(int i = 1; i <= m; i++){
  52.             int c = r.nextInt();
  53.            
  54.             if(prices.lowerKey(c+1) != null){
  55.                 pw.println(prices.lowerKey(c+1));
  56.                 remove(prices.lowerKey(c+1));
  57.             } else {
  58.                 pw.println(-1);
  59.             }
  60.         }
  61.  
  62.         pw.close();
  63.     }
  64.  
  65.     static void add(int val){
  66.         if(prices.containsKey(val)){
  67.             prices.put(val, prices.get(val) + 1);
  68.         } else {
  69.             prices.put(val, 1);
  70.         }
  71.     }
  72.  
  73.     static void remove(int val){
  74.         prices.put(val, prices.get(val) - 1);
  75.         if(prices.get(val).equals(0)){
  76.             prices.remove(val);
  77.         }
  78.     }
  79. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement