Https://uva.onlinejudge.org/external/5/514.pdf
514 Rails
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 of the station could is only a dead-end one
And due to lack of available space it could has only one track.
A
5, 4, 3, 2, 1 1, 2, 3, 4, 5
B
Station
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 1; 2; : : : ; N. The chief for train reorganizations must know whether
It is possible to marshal coaches continuing in the direction B so that their order would be a1:a2; : : : ; An.
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
That is there can located as many coaches as necessary in the station. But once a coach have
Entered the station it cannot return to the track in the direction A and also once it had left the station
In the direction B it cannot return to the station.
Input
The input le consists of blocks of lines. Each block except the last describes one train and possibly
More requirements to its reorganization. The RST 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 le contains the lines corresponding to the lines with permutations in the input le. A
Line of the output Le contains Yes if it's possible to marshal the coaches in the order required on the
Corresponding line of the input le. Otherwise it contains No. In addition, there are one empty line after
The lines corresponding to one block of the input le. There is no line in the output le corresponding
To the last \null "block of the input le.
ACM Contest problems Archive University of Valladolid (SPAIN)
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
Analysis:
Just give the stack a mock-up.
//stl notation#include <iostream>#include <sstream>#include <iomanip>#include <vector>#include <deque>#include <list>#include <set>#include <map>#include <stack>#include <queue>#include <bitset>#include <string>#include <numeric>#include <algorithm>#include <functional>#include <iterator>#include <cstdio>#include <cstring>#include <cmath>#include <cstdlib>#include <cctype>#include <complex>#include <ctime>#define INF 0x3f3f3f3f#define EPS 1e-6typedef Long LongLL;Const DoublePI =ACOs(-1.0);Const Long LongMoD =1e9+7;using namespace STD;inttarget[1005]; Stack <int>AintMain () {//freopen ("Int.txt", "R", stdin); //freopen ("OUT.txt", "w", stdout); intN while(scanf("%d", &n) && N) { while(scanf("%d", &target[0]) && target[0] !=0) { for(inti =1; i < n;i++)scanf("%d", &target[i]);intOK =1;intA =1, B =0; while(B < N) {if(A = = Target[b]) {a++; b++; }Else if(!a.empty () && a.top () = = Target[b]) {A.pop (); b++; }Else if(A <= N) a.push (a++);Else{OK =0; Break; } }if(OK)puts("Yes");Else puts("No"); }puts(""); }return 0;}
//array notation#include <iostream>#include <sstream>#include <iomanip>#include <vector>#include <deque>#include <list>#include <set>#include <map>#include <stack>#include <queue>#include <bitset>#include <string>#include <numeric>#include <algorithm>#include <functional>#include <iterator>#include <cstdio>#include <cstring>#include <cmath>#include <cstdlib>#include <cctype>#include <complex>#include <ctime>typedef Long LongLL;Const DoublePI =ACOs(-1.0);Const Long LongMoD =1e9+7;using namespace STD;inttarget[1005];inta[1005];intMain () {//freopen ("Int.txt", "R", stdin); //freopen ("OUT.txt", "w", stdout); intN while(scanf("%d", &n) && N) { while(scanf("%d", &target[0]) && target[0] !=0) { for(inti =1; i < n;i++)scanf("%d", &target[i]);inttop =-1, OK =1;intA =1, B =0; while(B < N) {if(A = = Target[b]) {a++; b++; }Else if(Top >=0&& A[top] = = Target[b]) {top--; b++; }Else if(A <= N) {A[++top] = a++; }Else{OK =0; Break; } }if(OK)puts("Yes");Else puts("No"); }puts(""); }return 0;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
UVa 514-rails