poj 1363 rail

c++ 栈的模拟火车进站,

不过又 遇到了这么坑爹的输入输出方式,搞得头大,没办法,为了以后的少吃亏,还是认真点吧

5//    表示进站的数N,如果等于0,结束整个循环
1 2 3 4 5// 出站的顺序
5 4 1 2 3// 出站的顺序
0// 表示这一组数据处理完毕,空行
6// 表示进站数N
6 5 4 3 2 1//  出站的顺序
0// 表示这一组数据处理完毕
0// 结束整个循环

自己捉摸了写了个栈与队列组合的程序,不过时间确实有些多
282ms;内存还好些 700 B;

#include<iostream>
#include<stack>
#include<queue>
using namespace std;

int num[1000];
int N;
queue<int> vi;
stack<int> st;
bool slove()
{
int i;
while(!vi.empty())
vi.pop();
while(!st.empty())
st.pop();
for( i=0;i<N;i++)
{
vi.push(num[i]);
}
for( i=1;i<=N;i++)
{
st.push(i);
while(!st.empty() && vi.front() == st.top ())
{
vi.pop();
st.pop();
}
}
if(i == N+1 && !vi.empty())
return false;
else
return true;
}
int main()
{
while(cin>>N,N)
{
while(cin>>num[0],num[0])
{
for(int i=1;i<N;i++)
cin>>num[i];

if(slove())
cout<<"Yes"<<endl;
else
cout<<"No"<<endl;
}
cout<<endl;
}
}





原文地址:https://www.cnblogs.com/lfyy/p/2779628.html