POJ1363-栈应用

来源:互联网 发布:农产品网络营销策划 编辑:程序博客网 时间:2024/05/15 20:02
Rails
Time Limit: 1000MS Memory Limit: 10000KTotal 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 could be only a dead-end one (see picture) and due to lack of available space it could 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 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. 

Input

The input consists of blocks of lines. 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 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 marshal 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
题目大意:就是有一列火车要进站,火车是一节一节的,节与节之间可以断开,不过必须按照顺序进站,然后问出站的可能的组合。
比如1 2 3 4 5 进站,如果1先进站再出站,2再进站再出站,3再。。。这样出战的顺序就是1 2 3 4 5。那么当然,如果1 2 3 4 5一起进站,那么出战的顺序就是5 4 3 2 1.
分析:题目是给出了出站的顺序问你这个可不可能,那么我的思路是先把题目要求的出战顺序保存,比如第一组数据1 2 3 4 5 然后用k来代表依次让1 2 3 4 5 出战。当k=1时,1出战。那么为了让1出战,首先得让1进站,我用A来表示顺序驶过来的火车。所以A=1进站,同时A++,代表这时候驶过来的火车首位是2.这样一次模拟。当k=n时模拟结束,此时代表所有火车都按照顺序出战了,或者如果A超过n的话模拟也结束,此时代表所有驶过来的火车都进站了,之所以A<=N+1,是因为最后一次还得让站里头的火车都出来,但是不管出来成功与否,都应该结束。最后看看站里头还有没有剩余的火车,如果有,证明出站失败,否则出站成功

11357567TSERROF1363Accepted252K360MSC++635B2013-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;}

其实这道题也可以自己写一个栈,当然不用栈会比较快啦,直接用个数组,或者数字来代表,不过做这题就是为了练习栈嘛,所以我又把之前写的栈复制进去提交了一次。
11360015TSERROF1363Accepted276K297MSC++1566B2013-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;}