UVA

#include <iostream>
#include <stack>
using namespace std;
const int N = 1000 + 10;

int n, target[N];
//target数组用来记录,题目所要求的,进入B方向的n个火车的依次序号 
int main()
{
	while (cin >> n && n)
	{
		stack<int> s;
		while (cin >> target[1] && target[1])
		{
			int A = 1, B = 1; //B表示,来自A方向的车厢判断到哪一节,A表示,进入B方向的车厢,判断到哪个下标了 
			for (int i = 2; i <= n; i++)
			cin >> target[i];
			
			int ok = 1;
			while (B <= n)
			{
				if (A == target[B])
				{
					A++; B++;
				}
				else if (!s.empty() && s.top() == target[B])
				{
					s.pop();
					B++;
				}
				else if (A <= n)
				{
					s.push(A++);
				}
				else 
				{
					ok = 0;
					break;
				}
			}
			cout << (ok ? "Yes" : "No") << endl;
		}
		cout << endl;		
	}
	return 0;
}

原文地址:https://www.cnblogs.com/mofushaohua/p/7789398.html