POJ 1363 Rails

题目传送门:POJ 1363

Descirbe:
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.
POJ 1363 Rails_第1张图片

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

题目大意:

这道题第一次看中文题目的时候,都没看懂输入输出说的啥,可能是翻译的问题吧(应该不会是我的问题)。大概就是,给你一列数,问你能否通过堆栈,将这列数变成指定的排列。输入有若干样例,每个样例先给个n,然后有若干行,每行是一个1-n的排列,每个样例最后一行是0,代表该样例结束,要求对每个样例的每个排列输出Yes或者No,每个样例间加个空行。

比如:n是6,给你一个排列6 5 4 3 2 1,那么这个应该输出Yes,因为,将1-n就是1-6按顺序存入栈,然后再一个一个弹出,就是6 5 4 3 2 1了,所以这个排列是可以形成的。

解题思路:

大概思路也比较简单,就是每次入栈前都检查一下,如果跟当前排列的数相等,直接pop,否则就入栈,最后如果栈为空,就是Yes(都出栈了,说明排列可以形成) 否则为No

AC代码:

 

 1 #include 
 2 #include 
 3 #include  // 别忘了头文件
 4 using namespace std;
 5 int main()
 6 {
 7     int n,i,cnt;
 8     while(~scanf("%d",&n) && n)
 9     {
10         int a[n+1]; // 构建数组,+1是为了从1开始,纯属习惯
11         i = 1; // 输入时用的下标
12         cnt = 1; // 检查时用的下标
13         while(~scanf("%d",&a[i]) && a[i])
14         {
15             i++;
16             stack<int> s;
17             for( ; i <= n; i++) scanf("%d",&a[i]); // 输入,不多说了
18             for(int j = 1; j <= n+1; j++) // 这里注意了,为什么是n+1,就好比上面举的那个n=6例子
19             // 前面入栈的都不符合,到最后才相等,所以就得多一个用来判断
20             {
21                 if(s.empty() && j <= n) {s.push(j);continue;} // 栈空就直接入栈
22                 if(j == a[cnt]) {cnt++; continue;} // 相等的话,也不用入栈了,直接跳过
23                 while(!s.empty() && s.top() == a[cnt]) {s.pop();cnt++;} // 应该很好理解
24                 if(j <= n) s.push(j); // 一直别忘了if的判断
25             }
26             if(s.empty()) printf("Yes\n"); // 按要求输出
27             else printf("No\n");
28             // 注意一些变量要归位,不然下次判断会出错
29             i = 1;
30             cnt = 1;
31         }
32         printf("\n");
33     }
34 }

 

小结: 这个题就是熟练一下栈的操作

更新一下:如果堆栈为空,s.top() 会有问题,所以查看栈顶元素前要注意判空

 

你可能感兴趣的:(POJ 1363 Rails)