c++_铁轨_栈
大概题意
先输入n
表示按从1到n的顺序入栈
再输入一段长度为n的数列
判断这段数列是否可能成为出栈队列
基本思路
由题意容易得出这是一道与栈相关的题
用STL创建一个栈(老师教我们要多用STL)
然后进行暴力模拟就可以了
正解
#include <cstdio>
#include <stack>
using namespace std;
const int N = 1010;
int n, target[N];
stack<int> stk;
int main()
{
while (scanf("%d", &n) == 1)
{
for (int i = 1; i <= n; i ++ ) scanf("%d", &target[i]);
int A = 1, B = 1;
bool check = true;
while (B <= n)
{
if (A == target[B]) A ++ , B ++ ;
else if (!stk.empty() && stk.top() == target[B]) stk.pop(), B ++ ;
else if (A <= n) stk.push(A ++ );
else
{
check = false;
break;
}
}
printf("%s\n", check ? "Yes" : "No");
}
return 0;
}