火车进站出站问题

#include "iostream"
#include "stack"
using namespace std;
#define max 1001
int main()
{
	int n,target[max];
	while(cin>>n)
	{
		stack<int> s;
		for(int i=1;i<=n;i++)
			cin>>target[i];  
		int a=1,b=1;
		bool flag=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
			{
				flag=0;
				break;
			}
		}
		if(flag)
			cout<<"Yes"<<endl;
		else
			cout<<"No"<<endl;
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/chu_jian86a/article/details/52441467
今日推荐