Rails UVa514

TImes: 17 mins. 栈混洗模板,模拟混洗过程秒。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#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;
}