您的位置:首页 > 大数据 > 人工智能

UVa 514: Rails

2017-01-08 14:58 330 查看
怎么进来就想想它怎么回去。

#include <iostream>
#include <stack>

using namespace std;

int main()
{
stack<int> in_station;
int num_carriage;

while(cin >> num_carriage && num_carriage != 0)
{
if(num_carriage < 1 || num_carriage > 1000)
continue;

int *seq_carriage = new int[num_carriage];
while(cin >> seq_carriage[0] && seq_carriage[0] != 0)
{
// in_station.push(seq_carriage[0]);
int seq_top = num_carriage + 1;

for(int i = 1; i < num_carriage; i++)
{
cin >> seq_carriage[i];
}

int result = 0;
for(int i = num_carriage - 1; i >= 0; i--)
{
int top_stack;
result = 0;

// push to the temporary station
if(!in_station.empty())
{
top_stack = in_station.top();
result = 1;
}

if (result && top_stack > seq_carriage[i])
{
break;
}

in_station.push(seq_carriage[i]);
top_stack = in_station.top();

// pop the train.
while(seq_top == top_stack + 1 )
{
in_station.pop();
seq_top = top_stack;

if (in_station.empty())
{
break;
}
top_stack = in_station.top();
}
}

if(!in_station.empty())
cout << "No" << endl;
else
cout << "Yes" << endl;

while(!in_station.empty())
{
in_station.pop();
}
}
cout << endl;

delete []seq_carriage;
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  rails uva 514