Rails
Time limit:1000 ms |
|
Memory limit:10000 K |
Total submissions:19969 |
|
Accepted:8062 |
Description
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 cocould be only a dead-end one (see picture) and due to lack of available space it coshould have only one track.
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 <= 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 ,..., an. 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 your 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.
Input
The input consists of blocks of lines. each block should t 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 specify al 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
51 2 3 4 55 4 1 2 3066 5 4 3 2 100
Sample output
YesNoYes
There is a train that is about to enter the station. A train can be disconnected from the train, but the train must arrive in order and then ask about the possible combination of the train. For example, 1, 2, 4, 5, if 1 is the first station and then the station, 2 is the second station and then the third is the first station... In this way, the game is played in the order of 1, 2, 3, 4, and 5. Then, of course, if 1, 2, 4, and 5 enter the site together, the playing sequence is 5, 4, 3, 2, and 1. analysis: the question is to provide an outbound order to ask if this is possible, so my idea is to first save the order required by the question, for example, the first group of data is 1 2 3 4 5 and then K is used to represent 1 2 3 4 5 in sequence. When k = 1, 1 plays. So in order to let 1 play, we must first make 1 into the station. I use a to represent the trains that pass by in sequence. Therefore, a = 1 indicates that at the same time, a ++ indicates that the first train to pass by at this time is 2. This is a simulation. When K = N, the simulation is over. At this time, it means that all trains are playing in order, or if a is over N, the simulation is over. At this time, it means that all the trains that pass by are in the station, the reason why a <= n + 1 is that the train in the station has to come out for the last time, but whether it succeeds or not, it should end. Finally, check whether there are any remaining trains in the station. If yes, it indicates that the station failed. Otherwise, the station succeeded.
11357567 |
Tserrof |
1363 |
Accepted |
252 K |
360 ms |
C ++ |
635b |
2013-03-16 21:51:52 |
#include <iostream>#include <stack>using namespace std;#define MAXN 1002int N, coaches[MAXN];int main(){while(cin>>N && N){while(cin>>coaches[1] && coaches[1]){for(int i=2;i<=N;++i)cin>>coaches[i];stack<int>s;int A=1;s.push(A++);int k=1;while(k<=N && A<=N+1){while(s.top()!=coaches[k] && A<=N+1){s.push(A++);}if(s.top()==coaches[k]){s.pop();++k;if(s.empty() && A<=N)s.push(A++);}}if(s.empty())cout<<"Yes"<<endl;else cout<<"No"<<endl;}cout<<endl;}return 0;}
In fact, this question can also be written into a stack by yourself. Of course, it will be faster without stacks. It can be represented directly by an array or number. However, this question is just to practice stacks, so I copied the stack I wrote and submitted it again.
11360015 |
Tserrof |
1363 |
Accepted |
276 K |
297 Ms |
C ++ |
1566b |
2013-03-17 15:43:57 |
#include <iostream>using namespace std;#define MAXN 1002int N, coaches[MAXN];#define MAXSIZE 10000template<typename T>class stack{private:T *STACK;int TOP;public:stack();~stack();bool pop();bool push(T);T top();bool empty();void show(bool);};template<typename T> stack<T>::stack(){STACK=new T[MAXSIZE];TOP=-1;}template<typename T> stack<T>::~stack(){delete STACK;}template<typename T> bool stack<T>::pop( ){if(TOP==-1)return false;--TOP;return true;}template<typename T> bool stack<T>::push(T d){if(TOP==MAXSIZE-1)return false;STACK[++TOP]=d;return true;}template<typename T>T stack<T>::top(){return STACK[TOP];}template<typename T> bool stack<T>::empty(){if(TOP==-1)return true;return false;}template<typename T> void stack<T>::show(bool reverse){if(reverse){for(int i=TOP;i>=0;--i)std::cout<<STACK[i]<<" ";}else{for(int i=0;i<=TOP;++i)std::cout<<STACK[i]<<" ";}}int main(){while(cin>>N && N){while(cin>>coaches[1] && coaches[1]){for(int i=2;i<=N;++i)cin>>coaches[i];stack<int>s;int A=1;s.push(A++);int k=1;while(k<=N && A<=N+1){while(s.top()!=coaches[k] && A<=N+1){s.push(A++);}if(s.top()==coaches[k]){s.pop();++k;if(s.empty() && A<=N)s.push(A++);}}if(s.empty())cout<<"Yes"<<endl;else cout<<"No"<<endl;}cout<<endl;}return 0;}