Given a stack which can keep MMM numbers at most. Push NNN numbers in the order of 1, 2, 3, ..., NNN and pop randomly. You are supposed to tell if a given sequence of numbers is a possible pop sequence of the stack. For example, if MMM is 5 and NNN is 7, we can obtain 1, 2, 3, 4, 5, 6, 7 from the stack, but not 3, 2, 1, 7, 5, 6, 4.
Input Specification:
Each input file contains one test case. For each case, the first line contains 3 numbers (all no more than 1000): MMM (the maximum capacity of the stack), NNN (the length of push sequence), and KKK (the number of pop sequences to be checked). Then KKK lines follow, each contains a pop sequence of NNN numbers. All the numbers in a line are separated by a space.
Output Specification:
For each pop sequence, print in one line "YES" if it is indeed a possible pop sequence of the stack, or "NO" if not.
Sample Input:
5 7 5
1 2 3 4 5 6 7
3 2 1 7 5 6 4
7 6 5 4 3 2 1
5 6 4 3 7 2 1
1 7 6 5 4 3 2
Sample Output:
YES
NO
NO
YES
NO
result :
#include<stdio.h>
#include<stdlib.h>/* 1 for push, -1 for pop,0 for never use*/
int full(int *a,int n,int m);
int block(int *a,int n,int m);
int main (void)
{
int m,n,k;
int *a;
int *b;
int i;
int h;
int t;
int point;
scanf("%d%d%d",&m,&n,&k);
for(t=1;t<=k;t++)
{
point=1;
a=(int*)malloc(sizeof(int)*n);
b=(int*)malloc(sizeof(int)*n);
for(i=0;i<n;i++)
{
a[i]=0;
}
for(i=0;i<n;i++)
{
scanf("%d",&b[i]);
}
for(i=0;i<n;i++)
{
h=b[i]-1;
while(h>=0)
{
if(a[h]==-1)
;
else
a[h]=1;
h--;
}
if(full(a,n,m))
{
point=0;
break;
}
if(block(a,n,b[i]-1))
{
point=0;
break;
}
a[b[i]-1]=-1;
}
if(point==1)
printf("YES\n");
}
return 0;
}
int full(int *a,int n,int m)
{
int i;
int t;
t=0;
for(i=0;i<n;i++)
{
if(a[i]==1)
{
t++;
}
}
if(t>m)
{
printf("NO\n");
return 1;
}
else return 0;
}
int block(int *a,int n,int i)
{
int t;
for(t=i+1;t<n;t++)
{
if(a[t]==1)
{
printf("NO\n");
return 1;
}
}
return 0;
}