There is a famous railway station in PopPush City. Country there is incredibly hilly. The station was built in last century. Unfortunately, funds were extremely limited that time. It was possible to establish only a surface track. Moreover, it turned out that the station could be only a dead-end one (see picture) and due to lack of available space it could have only one track. 5, 4, 3, 2, 1 1, 2, 3, 4, 5 Station The local tradition is that every train arriving from the direction A continues in the direction B with coaches reorganized in some way. • Assume that the train arriving from the direction A has NS 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 will be a1, a2, ..., ɑŋ. Help him and write a program that decides whether it is possible to get the required order of coaches. • You can assume that single coaches can be disconnected from the train before they enter the station and that they can move themselves until they are on the track in the direction B. • You can also suppose that at any time there can be located as many coaches as necessary in the station. But once a coach has entered the station it cannot return to the track in the direction A and also once it has left the station in the direction B it cannot return back to the station.
![There is a famous railway station in PopPush City. Country there is incredibly hilly. The station was
built in last century. Unfortunately, funds were extremely limited that time. It was possible to
establish only a surface track. Moreover, it turned out that the station could be only a dead-end
one (see picture) and due to lack of available space it could have only one track.
5, 4, 3, 2, 1
1, 2, 3, 4, 5
Station
The local tradition is that every train arriving from the direction A continues in the direction B with
coaches reorganized in some way.
• Assume that the train arriving from the direction A has N s 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 will be a1, a2, ..., ɑŋ. Help him and write a
program that decides whether it is possible to get the required order of coaches.
• You can assume that single coaches can be disconnected from the train before they enter
the station and that they can move themselves until they are on the track in the direction B.
• You can also suppose that at any time there can be located as many coaches as necessary in
the station. But once a coach has entered the station it cannot return to the track in the
direction A and also once it has left the station in the direction B it cannot return back to the
station.](/v2/_next/image?url=https%3A%2F%2Fcontent.bartleby.com%2Fqna-images%2Fquestion%2Fe815d996-ac78-4584-a71e-4284151c4e48%2Ff05b19cd-1823-4049-b461-2d5611db9a18%2Fekdgrg_processed.jpeg&w=3840&q=75)
![Program Input
• The input file consists of blocks of lines, each of which is a test case. 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, which is the number of coaches in the train. In each
of the next lines of the block there is a permutation of 1, 2, ..., N . For example, if N is 5, and
the permutation could be 5, 3, 2, 1, 4. Your program will take this permutation as input and
determine whether you can marshal the coaches from track A an incoming order 1, 2, 3, 4, 5
to track B with an outgoing order 5, 3, 2, 1, 4 using the station, which can be treated as a
stack.
• The last line of the block contains just 0.
• If a block starts with a zero, the program will terminate.](/v2/_next/image?url=https%3A%2F%2Fcontent.bartleby.com%2Fqna-images%2Fquestion%2Fe815d996-ac78-4584-a71e-4284151c4e48%2Ff05b19cd-1823-4049-b461-2d5611db9a18%2Fksyk4j_processed.jpeg&w=3840&q=75)
![](/static/compass_v2/shared-icons/check-mark.png)
#include <iostream>
#include <fstream>
#include <string>
#include <stack>
using namespace std;
int main() {
ifstream myfile;
string s;
myfile.open ("input.txt");
if (myfile.is_open()){
//if file is open and valid
int n;
myfile>>n;
while(n>0){
int a[n];
for(int i=0;i<n;i++){ //input the elements
myfile>>a[i];
}
stack<int> mystack;
mystack.push(1);
int k=0;
for(int i=2;i<=n;i++){ // adding the coaches to stack if it is not a desired in track B sequence
if(!mystack.empty() && mystack.top()==a[k]){
k++;
mystack.pop();
i--;
continue;
}
mystack.push(i);
}
bool flag = true;
while (!mystack.empty()){ // checking the output sequence of the coaches according to the testcase
if(mystack.top()==a[k]){
k++;
mystack.pop();
}else{
flag = false;
break;
}
}
if(flag)
s = s+"Yes\n";
else
s = s + "No\n";
myfile>>n; //taking input for next inline
if(n==0){ // checking the end of the current block
myfile>>n;
}
}
myfile.close();
ofstream out_data("output.txt");
out_data <<s; //output data file
}else
cout << "Error opening file\n";
return 0;
}
Trending now
This is a popular solution!
Step by step
Solved in 4 steps with 3 images
![Blurred answer](/static/compass_v2/solution-images/blurred-answer.jpg)
![Database System Concepts](https://www.bartleby.com/isbn_cover_images/9780078022159/9780078022159_smallCoverImage.jpg)
![Starting Out with Python (4th Edition)](https://www.bartleby.com/isbn_cover_images/9780134444321/9780134444321_smallCoverImage.gif)
![Digital Fundamentals (11th Edition)](https://www.bartleby.com/isbn_cover_images/9780132737968/9780132737968_smallCoverImage.gif)
![Database System Concepts](https://www.bartleby.com/isbn_cover_images/9780078022159/9780078022159_smallCoverImage.jpg)
![Starting Out with Python (4th Edition)](https://www.bartleby.com/isbn_cover_images/9780134444321/9780134444321_smallCoverImage.gif)
![Digital Fundamentals (11th Edition)](https://www.bartleby.com/isbn_cover_images/9780132737968/9780132737968_smallCoverImage.gif)
![C How to Program (8th Edition)](https://www.bartleby.com/isbn_cover_images/9780133976892/9780133976892_smallCoverImage.gif)
![Database Systems: Design, Implementation, & Manag…](https://www.bartleby.com/isbn_cover_images/9781337627900/9781337627900_smallCoverImage.gif)
![Programmable Logic Controllers](https://www.bartleby.com/isbn_cover_images/9780073373843/9780073373843_smallCoverImage.gif)