I have TLE veredict with this problem, how can I improve the algorithm?
Here is the Java code:
Code: Select all
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Collections;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader entrada = new BufferedReader(new InputStreamReader(System.in));
BufferedOutputStream salida = new BufferedOutputStream(System.out);
String[] datos;
int i, j, k, numVentanas, numCanguros;
ArrayList<Long> izquierdas = new ArrayList<Long>();
ArrayList<Long> derechas = new ArrayList<Long>();
ArrayList<Long> rangos = new ArrayList<Long>();
long posicionCanguro, min1, min2;
int numCasosPrueba = Integer.parseInt(entrada.readLine());
for (i = 1; i <= numCasosPrueba; i++) {
salida.write(("Case " + i + ":\n").getBytes());
datos = entrada.readLine().split(" ");
numVentanas = Integer.parseInt(datos[0]);
numCanguros = Integer.parseInt(datos[1]);
for (j = 0; j < numVentanas; j++) {
datos = entrada.readLine().split(" ");
izquierdas.add(Long.parseLong(datos[0]));
derechas.add(Long.parseLong(datos[1]));
}
for (j = 0; j < numCanguros; j++) {
posicionCanguro = Long.parseLong(entrada.readLine());
for (k = 0; k < numVentanas; k++) {
min1 = posicionCanguro - izquierdas.get(k);
min2 = derechas.get(k) - posicionCanguro;
if (min1 < 0 || min2 < 0) {
rangos.add(0l);
} else {
if (min1 <= min2) {
rangos.add(min1);
} else {
rangos.add(min2);
}
}
}
Collections.sort(rangos);
salida.write((rangos.get(rangos.size() - 1) + "\n").getBytes());
rangos.clear();
}
izquierdas.clear();
derechas.clear();
}
salida.flush();
salida.close();
}
}
![:wink:](./images/smilies/icon_wink.gif)