Rails
Input
The input consists of blocks of
lines. Each block except the last describes one train and possibly
more requirements for its reorganization. In the first line of the
block there is the integer N described above. In each of the next
lines of the block there is a permutation of 1, 2, ..., N. The last
line of the block contains just 0.
The last block consists of just one line containing 0.
The last block consists of just one line containing 0.
Output
The output contains the lines
corresponding to the lines with permutations in the input. A line
of the output contains Yes if it is possible to marshal the coaches
in the order required on the corresponding line of the input.
Otherwise it contains No. In addition, there is one empty line
after the lines corresponding to one block of the input. There is
no line in the output corresponding to the last ``null'' block of
the input.
Sample Input
5 1 2 3 4 5 5 4 1 2 3 0 6 6 5 4 3 2 1 0 0
Sample Output
Yes No Yes
# include<stdio.h>
int main()
{
int a[1024],b[1024],c[3000],i,j,n;
int top;
while(scanf("%d",&n),n!=0)
{
while(scanf("%d",&b[0]),b[0]!=0)
{
a[0]=1;
for(i=1;i<n;i++)
{
a[i]=i+1;
scanf("%d",&b[i]);
}
i=top=j=0;
while(i<n)
{
c[top]=a[i];
while(c[top]==b[j]&&j<n)
{
top--;
j++;
}
i++;
top++;
}
if(j==n)
printf("Yes\n");
else
printf("No\n");
}
printf("\n");
}
return 0;
}