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 1 2 3 4 5 5 4 1 2 3 0 6 6 5 4 3 2 1 0 0Sample Output
Yes No Yes 1 #include<cstdio> 2 #include<stack> 3 using namespace std; 4 int main() 5 { 6 int n; 7 while(scanf("%d",&n),n) 8 { 9 int a[1005]; 10 while(scanf("%d",&a[0]),a[0]) 11 { 12 int i,k; 13 for(i = 1;i < n;i++) 14 scanf("%d",&a[i]); 15 stack<int>s; 16 for(i = 1,k = 0;i <= n;i++) 17 { 18 s.push(i); 19 while(s.top()==a[k]) 20 { 21 if(!s.empty()) 22 { 23 k++; 24 s.pop(); 25 } 26 if(s.empty()) 27 break; 28 } 29 } 30 if(k==n) 31 printf("Yes\n"); 32 else 33 printf("No\n"); 34 } 35 printf("\n"); 36 37 } 38 return 0; 39 }
转载于:https://www.cnblogs.com/llal/p/5705265.html
