码迷,mamicode.com
首页 > 其他好文 > 详细

POJ1363--Rails(栈)

时间:2015-06-17 00:39:10      阅读:96      评论:0      收藏:0      [点我收藏+]

标签:

Rails
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 27593   Accepted: 10745

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

5
1 2 3 4 5
5 4 1 2 3
0
6
6 5 4 3 2 1
0
0

 

Sample Output

Yes
No

Yes

 

Source

 
//题意:按1~n进栈,然后给出一个顺序,看能否按照这个顺序出栈
//栈(STL)
 1 #include<cstdio>
 2 #include<stack>
 3 using namespace std;
 4 int main()
 5 {
 6     int n,i,j,num[1010];
 7     while(~scanf("%d",&n),n)
 8     {
 9         while(~scanf("%d",&num[0]),num[0])
10         {
11             for(i=1;i<n;i++)
12             scanf("%d",&num[i]);
13             stack<int>s;
14             for(j=0,i=1;i<=n;i++)
15             {
16                 s.push(i);
17                 while(!s.empty()&&s.top()==num[j])
18                 { j++; s.pop(); }
19                     
20             } 
21             if(j==n)
22             printf("Yes\n");
23             else
24             printf("No\n");
25         }
26         printf("\n");
27     }
28     return 0;
29 }

//数组

 1 #include<stdio.h>
 2 int main()
 3 {
 4     int n,i,j,t;
 5     int a[1010],b[1010];
 6     while(~scanf("%d",&n),n)
 7     {
 8         while(scanf("%d",&a[0]),a[0])
 9         {
10             for(i=1; i<n ;i++)
11             scanf("%d",&a[i]);
12             t=0;                           //数组a[]相当于栈,t为栈顶当前所在位置,据While()循环判断条件,t会更新; 
13             for(j=0,i=1; i<=n; i++)
14             {
15                 b[t++]=i;
16                 while(t>0 && b[t-1]==a[j]) //给组数据,自己测试; 6 3 2 1 6 5 4; (while也有循环作用)
17                 { t--; j++; }
18             }
19             if(j == n)
20             printf("Yes\n");
21             else
22             printf("No\n");
23         }
24         printf("\n");
25     }
26     return 0;
27 }

 

POJ1363--Rails(栈)

标签:

原文地址:http://www.cnblogs.com/fengshun/p/4582193.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!