用栈模拟不难
// Problem#: 1509
// Submission#: 878974
// The source code is licensed under Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License
// URI: http://creativecommons.org/licenses/by-nc-sa/3.0/
// All Copyright reserved by Informatic Lab of Sun Yat-sen University
#include <iostream>
#include <stack>
#include <vector>
using namespace std;
int main()
{
int coach_num; //车厢的数目
while( cin >> coach_num && coach_num != 0 ) //车厢数目为0将不会操作
{
int test;
while(cin >> test && test != 0)
{
vector<int> coach_out; //coach_out记录用户希望出站的车厢顺序
vector<int>::iterator it;
stack<int> coach_in; //coach_in用于在站内可以出站的车厢的顺序
int coach_counter = 0; //记录已出站的车厢数目
for( int i = 1; i <= coach_num; ++i ) //coach_out记录用户希望出站的车厢顺序
{
int temp;
if( i == 1 )
coach_out.push_back(test);
else
{
cin >> temp;
coach_out.push_back(temp);
}
}
it = coach_out.begin();
for( int j = 1; j <= coach_num; ++j) //如果将要入站的标号与用户希望出站的标号相同,则出站
{ //j用于表示入站的顺序
if( j == (*it)) //如果将要入站的车厢与用户希望接下来要出站的车厢编号一致,则将此车厢出站
{
it = coach_out.erase(it);
coach_counter++; //标记出站的车厢数目加1
}
else //如果来到的车厢不是用户想要出站的就先入站
{
while( !coach_in.empty() && coach_in.top() == (*it))
{
it= coach_out.erase(it);
coach_counter++;
coach_in.pop();
}
coach_in.push(j);
}
}
if(coach_out.empty())
;
else
for(it = coach_out.begin(); it != coach_out.end(); ++it)
{
if(coach_in.top() == (*it))
{
coach_in.pop();
coach_counter++;
}
}
if(coach_counter == coach_num)
cout << "Yes" << endl;
else
cout << "No" << endl;
}
cout << endl;
}
return 0;
}