## 12725 - Fat and Orial

Moderator: Board moderators

MPC1984
New poster
Posts: 40
Joined: Mon Jul 01, 2013 9:24 pm

### 12725 - Fat and Orial

Hi everybody!

It's possible to solve this problem with JAVA and don't get TLE?
I don't know how to improve my code for getting AC (know I'm getting TLE).
Someone could help me?

Here it's my JAVA's code:

Code: Select all

import java.io.BufferedOutputStream;
import java.io.IOException;
import java.math.BigDecimal;

public class Main {

public static void main(String[] args) throws IOException {

BufferedOutputStream salida = new BufferedOutputStream(System.out);
int i, asignaturasActuales, asignaturasFinales;
String[] datos;
for (i = 1; i <= numCasosPrueba; i++) {
mediaActual = Double.parseDouble(datos[0]);
mediaFinal = Double.parseDouble(datos[1]);
asignaturasActuales = Integer.parseInt(datos[2]);
asignaturasFinales = Integer.parseInt(datos[3]);
resultado = mediaFinal + (asignaturasActuales * (mediaFinal - mediaActual)) / asignaturasFinales;
salida.write(("Case #" + i + ": " + (new BigDecimal(resultado)).setScale(2, BigDecimal.ROUND_HALF_UP) + "\n").getBytes());
} else {
salida.write(("Case #" + i + ": Impossible\n").getBytes());
}
}
salida.flush();
salida.close();
}
}

anacharsis
Learning poster
Posts: 69
Joined: Mon Feb 09, 2015 1:56 am

### Re: 12725 - Fat and Orial

In Java, BigDecimal can make some computations easier, but it's incredibly slow.
So, use native doubles on this one.
Also, you will be tempted to use String.format( ) to output your native double results.
However, in Java, String.format() is incredibly slow.
So, try writing your own custom formatter.
If you do that, you should be able to AC with Java in under 0.5 seconds.
Still incredibly slow compared to C/C++, but it works...