Uva514 rails (Rails)
Question: rails
Link: uva514
Description:
There is a railway station in a city, with N trains entering the station from the direction, numbered 1-N in the order of arrival. your task is to determine whether you can allow them to enter the Track B in a specific order and enter the station. For example, the order of the output stack (5 4 1 2 3) is impossible, but (5 4 3 2 1) is possible.
Question Analysis:
In order to restructure the carriage, with the help of the transfer station, once every carriage is moved from A to C, it cannot return to A. Once it is moved from C to B, it cannot return to C, it means a-> C and C-> B. In the transfer station C, the carriage is in line with the principle of forward and backward. Therefore, it can be seen as a stack.
<<
Reference code:
// Rails. CPP # include <iostream> # include <stack> using namespace STD; const int maxn = 1000 + 10; int N, target [maxn]; int main () {While (CIN> N & N) {for (INT I = 1; I <= N; I ++) {CIN> target [I];} stack <int> S; int A = 1, B = 1; int OK = 1; while (B <= N) {if (a = target [B]) {A ++; B ++;} else if (! S. empty () & S. top () = target [B]) {S. pop (); B ++;} else if (a <= N) s. push (A ++); else {OK = 0; break ;}} cout <(OK? "Yes": "no") <Endl; // note that the brackets must be enclosed here} return 0 ;}