欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Uva 514 Rails java

程序员文章站 2024-03-20 20:57:10
...

Uva 514 Rails java

import java.util.*;
//		Sample Input 
//		5
//		1 2 3 4 5
//		5 4 1 2 3
//		0
//		6
//		6 5 4 3 2 1
//		0
//		0
//		Sample Output
//		Yes
//		No
//		Yes
public class Main {
    	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner in = new Scanner(System.in);
		while(in.hasNext()) {
			int n = in.nextInt();
//			System.out.println("n=" + n);
			if(n != 0)
//				if(in.hasNext())
//					System.out.println();
			{
				while(true) {
					int t = in.nextInt();
					if(t == 0)
						break;
					else {
						Stack<Integer> C = new Stack();
						int [] target = new int[n];
						target[0] = t;
						for(int i = 1; i < n; i++) {
							target[i] = in.nextInt();
						}
						int B = 0;
						int A = 1;
						while(A <= n) {
							if(!C.empty() && C.peek() == target[B]) {
								C.pop();
								B++;
							}
							else {
								C.push(A);
								A++;
							}
						}
						while(!C.empty()) {
							if(!C.empty() && C.peek() == target[B]) {
								C.pop();
								B++;
							}
							else 
								break;
						}
						if(C.empty() && B == n)
							System.out.println("Yes");
						else 
							System.out.println("No");
					}
				}
				if(in.hasNext())
					System.out.println();
			}
		}
			
	}

}