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

Rails UVa514

2018-02-10 21:36 387 查看
TImes: 17 mins.

栈混洗模板,模拟混洗过程即可。#include <iostream>
#include <stack>
using namespace std;
stack<int> A, C;
int res[1005] = { 0 };
void clear(stack<int> &x)
{
while (!x.empty())
x.pop();
}
bool solve(int N)
{
int tot = 1;
while (!A.empty())
{
if (!C.empty() && C.top() == res[tot])
{
C.pop();
tot++;
}
else
{
C.push(A.top());
A.pop();
}
}
while (!C.empty())
{
if (C.top() != res[tot])
return false;
C.pop();
tot++;
}
return true;
}
int main()
{
int N;
while (cin >> N && N)
{
while (cin >> res[1] && res[1])
{
for (int i = 2; i <= N; i++)
cin >> res[i];
clear(A); clear(C);
for (int i = N; i > 0; i--)
A.push(i);
cout << (solve(N) ? "Yes" :"No") << endl;
}
cout << endl;
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: