Original question:
There is a famous railway station in Poppush City. Country there is incredibly hilly. The station
was built in the last century. Unfortunately, funds were extremely limited that time. It is possible to
Establish only a surface track. Moreover, it turned out that the station could is only a dead-end one (see picture) and due to lack of available space it Could has only one track.
The local tradition is this every train arriving from the direction A continues in the direction
B with coaches reorganized in some. Assume that the train arriving from the direction A have
N≤1000 coaches numbered in increasing order,..., N. The chief for train reorganizations must
Know whether it is possible to marshal coaches continuing on the direction B so that their order would
Be a 1. A 2,..., a N. Help him and write a program that decides whether it was possible to get the required order of coaches. You can assume this single coaches can is disconnected from the train before they enter the station and that they can move themselves until they is on the track in the direction B. You can also suppose so at any time there can is located as many coaches as necessary in the station. But once a coach had entered it cannot return to the track in the direction A and also once it had left the St ation in the direction B it cannot return to the station.
Input
The input file consists of blocks of lines. Each block except the last describes one train and possibly
More requirements to its reorganization. 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,..., N. The last line of the block contains just ' 0 '. The last block consists of just one line containing ' 0 '.
Output
The output file contains the lines corresponding to the lines with permutations in the input file. A line of the output file contains ' Yes ' if it's possible to marshal the coaches in the order required on the Correspondi Ng line of the input file. Otherwise it contains ' No '. In addition, there are one empty line after the lines corresponding to one block of the input file. There is no line in the output file corresponding to the last "null" block of the input file.
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
English:
First give you a number, and then give you the number of N, ask you this n number and 1 to n in order to form a stack. Pit
#include <bits/stdc++.h> using namespace std;
Stack<int> S;
Vector<int> I,o;
void Ini () {i.clear ();
O.clear (); while (!
S.empty ()) S.pop ();
S.push (-1);
} int main () {Ios::sync_with_stdio (false);
int n,x;
while (Cin>>n,n) {while (cin>>x,x) {ini ();
O.push_back (x);
int flag=true;
for (int i=1;i<=n;i++) i.push_back (i);
for (int i=2;i<=n;i++) {cin>>x;
O.push_back (x);
} int j=0,k=0;
while (true) {if (k==n) break;
if (I[j]==o[k]) {j++;k++;
Continue
} if (O[k]==s.top ()) {k++;
S.pop ();
Continue
} if (j<n) {s.push (i[j]);
j + +;
} else {flag=false;
Break
}} if (flag) cout<< "Yes" <<endl;
else cout<< "No" <<endl;
} cout<<endl;
} return 0;
}
Answer:
Find a simple question practice practiced hand, the result of this problem I changed for nearly 2 hours, no matter how to change the first group of data can not, still think their slag to even stack will not. The result found that the problem read wrong =_=. Note instead of entering one n, and then entering two rows of data, you enter a row of data. The default stacking order is 1 to n numbers, and, instead of entering a 0 to end, instead of entering a 0 to change a n, continuous input of two 0 is the end. Halo