(SPOJ) 11001 - Corrida - Solução

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

class Main {
    class Competidor {
        public int idt;
        public long tempoFinal;
    }
   
    public static void main(String[] args) throws NumberFormatException, IOException {
        Main processando = new Main();
        processando.processa();
       
        System.exit(0);
    }
   
    void processa() throws NumberFormatException, IOException {
        String line = "";
       
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

        line = br.readLine();
        StringTokenizer tokenizer = new StringTokenizer(line);
        int qteCarros = Integer.valueOf(tokenizer.nextToken());
        int qteVoltas = Integer.valueOf(tokenizer.nextToken());
       
        Competidor[] competidores = new Competidor[qteCarros];
       
        for (int i = 0; i < qteCarros; i++) {
            competidores[i] = new Competidor();
        }
       
        long menor = 1000000000L;
        long soma = 0L;
        for (int i = 0; i < qteCarros; i++) {
            soma = 0;
            line = br.readLine();
            tokenizer = new StringTokenizer(line);
            for (int j = 0; j < qteVoltas; j++) {
                int tempo = Integer.valueOf(tokenizer.nextToken());
                soma += tempo;
            }
            competidores[i].idt = i;
            competidores[i].tempoFinal = soma;
        }
       
        Arrays.sort(competidores, 0, qteCarros, new Comparator<Competidor>() {
            @Override
            public int compare(Competidor c1, Competidor c2)
            {
                if (c1.tempoFinal < c2.tempoFinal) {
                    return +1;
                }
                else if (c1.tempoFinal == c2.tempoFinal) {
                    return 0;
                }
                else {
                    return -1;
                }
            }
        });
       
        for (int i = qteCarros-1; i >= qteCarros-3; i--) {
            System.out.println((competidores[i].idt)+1);
        }
                                   
        return;
    }
}

Comments

Popular posts from this blog

(Coderbyte) Powers of Two - Solução

(Coderbyte) Dash Insert II - Solução

(CoderByte) Number Search - Solução