64,682
社区成员
发帖
与我相关
我的任务
分享
#include <stdio.h>
#define MAX_SIZE 1000
int pop[MAX_SIZE+1];
int flag[MAX_SIZE+1];
/*
* match程序是基于以下结论.
* 一个合法的pop序列满足这样的关系:
* 对任何两个相邻的数(ai, ai+1),
* 若ai+1 > ai,则ai+1 大于前面所有的数
* 若ai+1 < ai,则比ai+1小1的数肯定在前面已经出现
*
*/
int match(int pop[], int size)
{
int i;
int max = 0;
memset(flag, 0, sizeof(flag)) /* 所有标志都清0 */
for (i=1; i<=size; i++) {
if (pop[i]>pop[i-1] && pop[i]>max) {
max = pop[i];
} else if (pop[i]<pop[i-1] && flag[pop[i-1]]) {
flag[i] = 1;
} else {
return 0;
}
}
return 1;
}
int main(void)
{
int n;
int i;
/* 读入pop序列,下标从1开始 */
scanf("%d", &n);
for (i=1; i<=n; i++) scanf("%d", &pop[i]);
/*
* 数组第0项作哨兵,
*/
pop[0] = 0;
if (match(pop, n)) printf("yes");
else printf("no");
return 0;
}