(SPOJ) 2609 - Trilhas - 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;
        
        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));

        int qteTrilhas = leitor(br);
               
        int idtrilha = 0;
        int trilhaMaisFacil = 1000000001;
        int[] vetor = new int[1000];
        for (int i = 0; i < qteTrilhas; i++) {
            int qtePontos = leitor(br);
           
            for (int j = 0; j < qtePontos; j++) {
                vetor[j] = leitor(br);
            }       
           
            int anterior = 0;
            int diferenca = 0;
            int diferenca2 = 0;           
            for (int j = 1; j < qtePontos; j++) {
                if (vetor[j] > vetor[anterior]) {
                    diferenca += (vetor[j] - vetor[anterior]);
                }
                else { // vetor[j] < vetor[anterior]
                    diferenca2 += (vetor[anterior] - vetor[j]);
                }
                anterior = j;
            }
                       
            if (diferenca > diferenca2) {
                if (diferenca2 < trilhaMaisFacil) {
                    trilhaMaisFacil = diferenca2;
                    idtrilha = i+1;
                }
            }
            else {
                if (diferenca < trilhaMaisFacil) {
                    trilhaMaisFacil = diferenca;
                    idtrilha = i+1;
                }
            }

        }
       
        System.out.println(idtrilha);

        return;
    }
}

Comments

Popular posts from this blog

(Coderbyte) Dash Insert II - Solução

(Coderbyte) Run Length - Solução

(Coderbyte) Counting Minutes I - Solução