sicily/1509. Rails
2011-07-12 12:04 Min·zc 阅读(504) 评论(0) 编辑 收藏 举报
1 #include <iostream>
2 #include <stack>
3 using namespace std;
4 int train[1001];
5 int main()
6 {
7 int t;
8 int fs=0;
9 while(cin>>t&&t!=0)
10 {
11 while(cin>>train[0]&&train[0]!=0)
12 {
13 for(int i=1;i<t;i++)
14 {
15 cin>>train[i];
16 }
17 stack<int> s;
18 int k=0;
19 for(int i=1;i<=t;i++)
20 {
21 //从A到来的车与目标序列相同,让A车过
22 if(i==train[k])
23 k++;
24 else
25 {
26 //如果栈中的车与目标队列相同,那么就是让队列中的车出来
27 while(!s.empty()&&s.top()==train[k])
28 {
29 s.pop();
30 k++;
31 }
32
33 s.push(i);
34 }
35 }
36 //最后清空堆栈
37 while(!s.empty())
38 {
39 if(s.top()==train[k])
40 k++;
41 s.pop();
42 }
43 if(k==t)
44 cout<<"Yes"<<endl;
45 else
46 cout<<"No"<<endl;
47
48 }
49 cout<<endl;
50 }
51 }
2 #include <stack>
3 using namespace std;
4 int train[1001];
5 int main()
6 {
7 int t;
8 int fs=0;
9 while(cin>>t&&t!=0)
10 {
11 while(cin>>train[0]&&train[0]!=0)
12 {
13 for(int i=1;i<t;i++)
14 {
15 cin>>train[i];
16 }
17 stack<int> s;
18 int k=0;
19 for(int i=1;i<=t;i++)
20 {
21 //从A到来的车与目标序列相同,让A车过
22 if(i==train[k])
23 k++;
24 else
25 {
26 //如果栈中的车与目标队列相同,那么就是让队列中的车出来
27 while(!s.empty()&&s.top()==train[k])
28 {
29 s.pop();
30 k++;
31 }
32
33 s.push(i);
34 }
35 }
36 //最后清空堆栈
37 while(!s.empty())
38 {
39 if(s.top()==train[k])
40 k++;
41 s.pop();
42 }
43 if(k==t)
44 cout<<"Yes"<<endl;
45 else
46 cout<<"No"<<endl;
47
48 }
49 cout<<endl;
50 }
51 }