(SPOJ) 8314 - Fórmula 1 - Solução

import java.io.*;
import java.util.*;

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;
        int sinal = 1;
        while (true) {
            n = br.read();
            if (n >= '0' && n <= '9') break;
            if (n == '-') sinal = -1;
            if (n == '+') sinal = 1;
        }
        while (true) {
            resp = resp*10 + n-'0';
            n = br.read();
            if (n < '0' || n > '9') break;
        }
        return resp*sinal;
    }
   
    void processa() throws NumberFormatException, IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));

        int qteCorridas = leitor(br);
        int qtePilotos = leitor(br);
        while (qteCorridas != 0 || qtePilotos != 0) {
            int[][] matrizResultado = new int[qteCorridas][qtePilotos]; // matriz que guarda a colocação de cada piloto
            for (int i = 0; i < qteCorridas; i++) {
                for (int j = 0; j < qtePilotos; j++) {
                    matrizResultado[i][leitor(br)-1] = j;
                }
            }
           
            int qteSistPontuacao = leitor(br);
           
            for (int i = 0; i < qteSistPontuacao; i++) {
                int qteRecebePontos = leitor(br);
                int[][] matrizPontuacao = new int[qteSistPontuacao][qteRecebePontos]; // matriz que guarda o valor dos pontos
                for (int j = 0; j < qteRecebePontos; j++) {
                    matrizPontuacao[i][j] = leitor(br);
                }
               
                Integer[] vetor = new Integer[qtePilotos]; // vetor para guardar os pontos de cada piloto
                for (int j = 0; j < qtePilotos; j++) {
                    vetor[j] = 0;
                }
               
                for (int j = 0; j < qteCorridas; j++) {
                    for (int k = 0; k < qteRecebePontos; k++) {
                        int indice = matrizResultado[j][k];
                        vetor[indice] += matrizPontuacao[i][k];
                    }
                }
               
                // Pega maior valor
                int maior = -1;
                for (int j = 0; j < qtePilotos; j++) {
                    if (vetor[j] > maior) {
                        maior = vetor[j];
                    }
                }
               
                for (int j = 0; j < qtePilotos; j++) {
                    if (vetor[j] == maior) {
                        bw.write((j+1) + " ");
                    }
                }
                bw.write("\n");
            }
             
            qteCorridas = leitor(br);
            qtePilotos = leitor(br);
        }

        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