12725 - Fat and Orial

All about problems in Volume 127. If there is a thread about your problem, please use it. If not, create one with its number in the subject.

Moderator: Board moderators

Post Reply
New poster
Posts: 40
Joined: Mon Jul 01, 2013 9:24 pm
Location: Valladolid, Spain

12725 - Fat and Orial

Post by MPC1984 » Sat Apr 25, 2015 9:57 pm

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.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.math.BigDecimal;

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);
        double resultado, mediaActual, mediaFinal;
        int i, asignaturasActuales, asignaturasFinales;
        String[] datos;
        int numCasosPrueba = Integer.parseInt(entrada.readLine());
        for (i = 1; i <= numCasosPrueba; i++) {
            datos = entrada.readLine().split(" ");
            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;
            if (resultado <= 10d) {
                salida.write(("Case #" + i + ": " + (new BigDecimal(resultado)).setScale(2, BigDecimal.ROUND_HALF_UP) + "\n").getBytes());
            } else {
                salida.write(("Case #" + i + ": Impossible\n").getBytes());
Thanks in advance. :wink:

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

Re: 12725 - Fat and Orial

Post by anacharsis » Thu Jun 02, 2016 6:22 am

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...

Post Reply

Return to “Volume 127 (12700-12799)”