### rgoewedky's blog

By rgoewedky, history, 4 years ago,

I am getting TLE in two cases.

Here is my code:

import java.io.*;
import java.util.*;
public class Solution {
StringTokenizer tokenizer;
tokenizer = null;
}
String next() { // reads in the next string
while (tokenizer == null || !tokenizer.hasMoreTokens()) {
try {
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
public int nextInt() { return Integer.parseInt(next()); } // reads in the next int
public long nextLong() { return Long.parseLong(next()); } // reads in the next long
public double nextDouble() { return Double.parseDouble(next()); } // reads in the next double
}

static PrintWriter out= new PrintWriter(System.out);
public static void main(String[] args) {
// YOUR CODE HERE //! @ % & * () _ {} # ~ : < > ? "" | ^

int n=f.nextInt();
int m=f.nextInt();
int x=0,ind=0;

TreeMap<Integer,Integer> map=new TreeMap<>();
for (int i=0;i<n;i++) {
x=f.nextInt();
if (map.containsKey(x)) {
map.put(x,map.get(x)+1);
}
else map.put(x,1);
}

//sort(a);

for (int i=0;i<m;i++) {
x=f.nextInt();
//ind=lower_bound(a,x);

if (map.containsKey(x)) {

if (map.get(x)>0) {
out.println(x);
map.put(x,map.get(x)-1);

if (map.get(x)==0) {
map.remove(x);
}
}

else {
out.println(-1);
// map.put(x,map.get(x)-1);
}

}

else if (map.lowerKey(x)!=null) {
if (map.get(map.lowerKey(x))>0) {
out.println(map.lowerKey(x));
map.put(map.lowerKey(x),map.get(map.lowerKey(x))-1);

if (map.get(map.lowerKey(x))==0) {
map.remove(map.lowerKey(x));
}
}
else out.println(-1);
}
else out.println(-1);
}

out.close(); // flushes the output once printing is done
}
}

• -1

 » 3 weeks ago, # |   0 bro have you got the answer?