TOPIC

WA 5% - Java

gsilva56 asked 4 years ago

Estou recebendo 5% de Wrong Answer neste exercício, porém todos os casos de teste estão dando certo... Alguém sabe o que pode ser?

import java.io.IOException;
import java.util.Scanner;
import java.util.Stack;

public class Main {
    public static void main(String[] args) throws IOException {
        Scanner scanner = new Scanner(System.in);

        Stack<Integer> entrada = new Stack<>();
        Stack<Integer> estacao = new Stack<>();

        int i = 1;
        boolean ok = false;
        boolean continuar = false;
        int num;

        while (true) {
            num = scanner.nextInt();
            if (num == 0) {
                break;
            }
            int[] saida = new int[num];
            while (true) {
                for (int j = num; j >= 1; j--) {
                    entrada.push(j);
                }
                int vagao_saida = scanner.nextInt();
                if (vagao_saida == 0) {
                    break;             
                }
                saida[0] = vagao_saida;
                while (i < num) {
                    vagao_saida = scanner.nextInt();
                    saida[i] = vagao_saida;
                    i++;
                }
                i = 0;
                while (i < num) {
                    if (!entrada.empty() && entrada.peek() == saida[i]) {
                        entrada.pop();
                        i++;
                        ok = true;
                    }
                    else {
                        if (!estacao.empty()) {
                            if (!entrada.empty() && entrada.peek() < saida[i]) {
                                estacao.push(entrada.peek());
                                entrada.pop();
                                continuar = true;
                            }
                            if (saida[i] == estacao.peek()) {
                                i++;
                                estacao.pop();
                                ok = true;
                            }
                            else if (saida[i] != estacao.peek() && !continuar) {
                                ok = false;
                                break;
                            }
                        }
                        else {
                            if (!entrada.empty() && entrada.peek() < saida[i]) {
                                estacao.push(entrada.peek());
                                entrada.pop();
                            }
                        }
                    }
                    continuar = false;
                }
                if (ok) {
                    System.out.println("Yes");
                } else {
                    System.out.println("No");
                }
                estacao.clear();
                entrada.clear();
                i = 1;
            }
        }
    }
}

This topic has not been answered yet. Be the first!

Remember not post solutions. Your post may be reviewed by our moderators.