题目链接:http://7xjob4.com1.z0.glb.clouddn.com/21455e1d8d4a7b8170202987eb38b76f
就是给你n个火车的出站顺序,问你是否可以实现。
下面是AC代码:
#include<cstdio>
#include<stack>
using namespace std;
const int N = 1005;
int n, target[N], A, B;
int main()
{
while (scanf ("%d", &n), n)
{
while (scanf ("%d", &target[1]), target[1])
{
for (int i = 2; i <= n; ++i)
scanf ("%d", &target[i]);
stack<int> s;
A = B = 1;
bool ok = true;
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
{
ok = false;
break;
}
}
printf (ok ? "Yes\n" : "No\n");
}
printf("\n");
}
return 0;
}