B - Rails


Time Limit:1000MS     Memory Limit:10000KB     64bit IO Format:%I64d & %I64u


Submit  Status


System Crawler  (2012-09-14)



Description

There is a famous railway station in PopPush City. Country there is incredibly hilly. The station was built in last century. Unfortunately,

 funds were extremely limited that time. It was possible to establish only a surface track. Moreover, it turned out that the station could 

be only a dead-end one (see picture) and due to lack of available space it could have only one track. 

                                                             

Rails_出栈

 

The local tradition is that every train arriving from the direction A continues in the direction B with coaches reorganized in some way.

 Assume that the train arriving from the direction A has N <= 1000 coaches numbered in increasing order 1, 2, ..., N. The chief for train reorganizations must know whether it is possible to marshal coaches continuing in the direction B so that their order will be a1, a2, ..., aN.

 Help him and write a program that decides whether it is possible to get the required order of coaches. You can assume that single coaches 

can be disconnected from the train before they enter the station and that they can move themselves until they are on the track in the

 direction B. You can also suppose that at any time there can be located as many coaches as necessary in the station. But once a coach has entered the station it cannot return to the track in the direction A and also once it has left the station in the direction B it cannot return

 back to the station. 



Input


The input consists of blocks of lines. Each block except the last describes one train and possibly more requirements for its reorganization. In the first line of the block there is the integer N described above. In each of the next lines of the block there is a permutation of 1, 2, ..., N. The last line of the block contains just 0. 

The last block consists of just one line containing 0.


Output


The output contains the lines corresponding to the lines with permutations in the input. A line of the output contains Yes if it is possible to marshal the coaches in the order required on the corresponding line of the input. Otherwise it contains No. In addition, there is one empty line after the lines corresponding to one block of the input. There is no line in the output corresponding to the last ``null'' block of the input.


Sample Input


51 2 3 4 5 5 4 1 2 3 0 6 6 5 4 3 2 1 0 0


Sample Output


YesNo Yes







/*该题是典型的栈的应用题

火车进站的顺序为1.....n,

判断给出的出站顺序是否合法

栈的特点是先进后出,若某个数已出栈,则比它大的且已入栈的元素

一定在它之前出栈,也就是说如果某个数已经出栈,但栈中还有比它

大的元素的话,则此出栈顺序一定是非法的。

解题步骤:

注:0为未进栈,1为在栈中,2为已出栈

1)设所有元素未进栈,即vis数组值都为0;

2)依次读入题目中所给的出栈顺序的元素,对每个读入的元素做如

下判断:

1.对于当前读入的元素,判断是否有比它大的元素还为出栈,若有说明

该出栈顺序非法,结束该出栈顺序的读入。

2.若对于当前读入的元素,判断为合法,把比该元素小的并未入栈的元素

入栈,设该元素为已出栈标志,继续读下一个元素。

3)判断每个出栈顺序是否合法,合法输出“Yes”,否则输出“No”;

*/


<span style="font-size:14px;">#include <iostream>
#include <cstring>
using namespace std;
int main()
{
	int vis[1005];
	int n,x;
	while(cin>>n&&n)
	{
		int max=0;
		cin>>x;
		while(x)
		{
			
			memset(vis,0,sizeof(vis));
			//设初始标志为0表示所有的数都未入栈,1代表入栈,2代表出栈
			bool ok=true;
	
			for(int j=2;j<=n;j++)
			{
				if(ok)
				{
					bool islegal=true;
					for(int i=x+1;i<=max;i++)//判断设有入栈标志的数中有没有比x大的数
					{
						if(vis[i]==1)
						{
							islegal=false;
							break;
						}
					}
					
					if(islegal)
					{//若出栈顺序合法更新当前入栈的最大值
					 //并把该数(x)设为出栈标志,并把小于(x)的数设入栈标志
						max=(max>x)?max:x;
						vis[x]=2;
						for(int k=x-1;k>0&&!vis[k];k--)
							vis[k]=1;
					}
					else//否则出栈顺序非法
						ok=false;
				}
				cin>>x;
			}
			if(ok)
				cout<<"Yes"<<endl;
			else
				cout<<"No"<<endl;
			cin>>x;
		}
		cout<<endl;
	}
	return 0;
}</span>