(SPOJ) 11628 - Bactérias - Solução 1

import java.io.*;
import java.util.*;
import java.lang.Math;

class Main {
    public static void main(String[] args) throws NumberFormatException, IOException {
        Main processando = new Main();
        processando.processa();
       
        System.exit(0);
    }
   
    static int leitor(BufferedReader br) throws NumberFormatException, IOException {
        int n;
        int resp = 0;
        while (true) {
            n = br.read();
            if (n >= '0' && n <= '9') {
                break;
            }
        }
        while (true) {
            resp = resp*10 + n-'0';
            n = br.read();
            if (n < '0' || n > '9') {
                break;
            }
        }
        return resp;
    }
   
    void processa() throws NumberFormatException, IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));

        int qteBacterias = leitor(br);
       
        double maior = -1;
        int indiceBacteria = -1;
        double[] vetor = new double[2001];
        for (int i = 0; i < qteBacterias; i++) {
            double reproducao = leitor(br);
            int tempoDeVida = leitor(br);
           
            // Precomputar os logaritimos
            int reproducaoInt = (int)reproducao;
            if (vetor[reproducaoInt] == 0) {
                vetor[reproducaoInt] = Math.log(reproducao);
                reproducao = tempoDeVida*vetor[reproducaoInt];
            }
            else {
                reproducao = tempoDeVida*vetor[reproducaoInt];   
            }
            if (reproducao > maior) {
                indiceBacteria = i;
                maior = reproducao;
            }
        }
       
        bw.write(indiceBacteria+"\n");
        bw.flush();
        bw.close();

        return;
    }
}

Comments

Popular posts from this blog

(Coderbyte) Dash Insert II - Solução

(Coderbyte) Run Length - Solução

(Coderbyte) Counting Minutes I - Solução