Problem B: Rail station
Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 479 Solved: 243
[ Submit][ Status][ Web Board]
Description
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.
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
5 4 1 2 3
1 2 3 4 5
0
6
6 5 4 3 2 1
1 2 3 5 6 4
6 3 2 5 4 1
0
10
3 1 5 8 2 6 9 4 10 7
4 6 10 9 2 1 8 7 3 5
6 9 8 7 5 4 3 2 10 1
6 9 8 7 5 4 3 10 2 1
6 9 8 7 5 4 10 3 2 1
6 9 10 8 7 5 4 3 2 1
6 10 9 8 7 5 4 3 2 1
7 6 5 4 3 2 1 8 9 10
7 6 5 4 3 2 1 8 10 9
4 6 9 7 10 1 5 8 2 3
10 8 1 6 5 7 9 4 3 2
0
0
Sample Output
No
Yes
---
Yes
Yes
No
---
No
No
Yes
Yes
Yes
Yes
Yes
Yes
Yes
No
No
---
HINT
#include<stdio.h>
#include<stdlib.h>
#define TRUE 1
#define FALSE 0
typedef int BOOL;
struct STACK{
int maxcoaches ;
int top ;
int* list ;
};
typedef struct STACK *Stack;
typedef int *Elem;
void init(Stack stack,int i){
stack->maxcoaches =i;
stack->top= 0;
stack->list=(Elem)malloc((i)*sizeof(int));
}
void clear(Stack stack){
stack->maxcoaches = 0;
stack->top = 0;
if( stack->list != NULL ){
free(stack->list);
stack->list =NULL;
}
}
int topvalue(Stack stack){
if ( stack->top > 0 ){
int e = stack->list[ stack->top - 1 ];
return e;
}
}
BOOL push(Stack stack, int e){
if (stack->top < stack->maxcoaches){
stack->list[stack->top++] = e;
return TRUE;
}
else return FALSE;
}
BOOL pop(Stack stack, Elem e){
if ( stack->top > 0 ){
int i=stack->top;
*e=stack->list[i-1];
stack->top--;
return TRUE;
}
else return FALSE;
}
BOOL ifpossible(int i,int temp[]){
int j,k;
Elem e;
int a;
e=&a;
Stack tmpstack=(Stack)malloc(sizeof(struct STACK));
Stack stack=(Stack)malloc(sizeof(struct STACK));
init(stack,i);
init(tmpstack,i);
for(j=0;j<i;j++){
tmpstack->list[j]=i-j;
}
tmpstack->maxcoaches=i;
tmpstack->top=i;
for(k=tmpstack->top;tmpstack->list[k-1]<temp[0];k--){
pop(tmpstack,e);
push(stack,*e);
}
pop(tmpstack,e);
push(stack,*e);
pop(stack,e);
for(j=1;j<i;j++){
if(temp[j]<topvalue(stack)){
return FALSE;
}
else{
if(temp[j]>topvalue(stack)){
for(k=tmpstack->top;tmpstack->list[k-1]<temp[j];k--){
pop(tmpstack,e);
push(stack,*e);
}
pop(tmpstack,e);
push(stack,*e);
}
pop(stack,e);
}
}
return TRUE;
}
int main(){
int i,r;
while((scanf("%d",&i)==1)&& i!=0){
BOOL res;
int temp[i];
for(r=0;r<i;r++){
scanf("%d",&temp[r]);
}
while(temp[0]!=0){
res=ifpossible(i,temp);
if(res==TRUE){
printf("Yes\n");
}
else printf("No\n");
scanf("%d",&temp[0]);
if(temp[0]!=0){
for(r=1;r<i;r++){
scanf("%d",&temp[r]);
}
}
}
printf("---\n");
}
}