之前思路不太对。。。
WA代码:
#include <iostream>
#include <cstdio>
#include <stack>
#include <cstring>
using namespace std;
#define N 1010
int b[N];
int main()
{
int n;
while(scanf("%d", &n), n)
{
memset(b, 0, sizeof b);
while(scanf("%d", b + 1), b[1])
{
for (int i = 2; i <= n; i ++) scanf("%d", b + i);
stack<int> c;
int i = 1, j = 1;
while(i <= n && j <= n)
{
if(b[i] == j) i ++, j ++;
else c.push(j ++);
}
if(i > n){
puts("Yes");
continue;
}
else{
bool flag = true;
while(!c.empty()){
int u = c.top();
if(u == b[i]) i ++, c.pop();
else{
flag = false;
break;
}
}
if(flag) puts("Yes");
else puts("No");
}
}
cout << endl;
}
return 0;
}
AC代码:
#include <iostream>
#include <cstdio>
#include <stack>
#include <cstring>
using namespace std;
#define N 1010
int b[N];
int main()
{
int n;
while(scanf("%d", &n), n)
{
memset(b, 0, sizeof b);
while(scanf("%d", b + 1), b[1])
{
for (int i = 2; i <= n; i ++) scanf("%d", b + i);
stack<int> c;
int i = 1, j = 1;
bool flag = true;
while(i <= n)
{
if(b[i] == j) i ++, j ++;
else if(!c.empty() && c.top() == b[i]){
c.pop();
i ++;
}
else if(j <= n){
c.push(j ++);
}
else{
flag = false;
break;
}
}
if(flag) puts("Yes");
else puts("No");
}
cout << endl;
}
return 0;
}