Rails
原題及翻譯
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.
當地的傳統是,從A方向到B方向的每一列火車都會以某種方式重新組織車廂。
Assume that the train arriving from the direction A has N 1000 coaches numbered in increasing order 1, 2, . . . , N .
假設從A方向到達的列車有N 1000節車廂,按1、2、3的順序遞增。…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 .
列車重組負責人必須知道是否有可能安排繼續沿B方向行駛的客車,使其順序為A1.A2,…的。
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.
你可以假設單程車廂在進入車站之前可以與列車斷開連接,并且它們可以自行移動,直到它們在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.
但是,一旦客車進入車站,它就不能返回A方向的軌道,而且一旦它離開B方向的車站,它就不能返回車站。
Input
輸入
The input file 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.
在塊的第一行中有上面描述的整數n。
In each of the next lines of the block there is a permutation of 1, 2, . . . , N .
在塊體的每一行中,都有1、2、、的排列…n。
The last line of the block contains just ‘0’.
塊的最后一行只包含“0”。
The last block consists of just one line containing ‘0’.
最后一個塊只包含一行“0”。
Output
輸出
The output file contains the lines corresponding to the lines with permutations in the input file.
輸出文件包含與輸入文件中具有排列的行相對應的行。
A line of the output file contains ‘Yes’ if it is possible to marshal the coaches in the order required on the corresponding line of the input file.
如果可以按照輸入文件相應行上所需的順序封送coach,則輸出文件的行包含“yes”。
Otherwise it contains ‘No’.
否則,它包含“NO”。
In addition, there is one empty line after the lines corresponding to one block of the input file.
此外,在與輸入文件的一個塊對應的行之后還有一個空行。
There is no line in the output file corresponding to the last “null” block of the input file.
輸出文件中沒有對應于輸入文件最后一個“空”塊的行。
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節車廂從A方向駛入車站,按進站順序編號為1~n判斷是否能讓它們按照某種特定的順序進入B方向的鐵軌并駛出車站。(典型的棧)
代碼分析
#include <cstdio> #include <stack> using namespace std; const int MAXN=1000+10; int n,target[MAXN]; int main () {while(scanf("%d",&n)==1){stack<int> s;int A=1,B=1;for(int i=1;i<=n;i++) scanf("%d",&target[i]);int ok=1;while(B<=n){if(A==target[B]) {A++;B++;}else if(!s.empty()&&s.top()==target[B]) {s.pop();B++;}else if(A<=n) {s.push(A++);}else {ok=0;break;}}printf("%s\n",ok?"Yes":"No");}return 0; }這是汝佳老師給出的答案,但是WA,剛看見的時候簡直驚為天人,汝佳老師竟然給出了一個錯誤答案,我還對照了好幾遍,生怕是我哪里寫錯了。
看了好幾遍邊,才發現汝佳老師可能是忘了寫最后一個塊為0的情況,然后我就改了一下汝佳老師的代碼:
#include <iostream> #include <stack> using namespace std; const int maxn=1111; int target[maxn]; int main() {int n;while(cin>>n&&n){while(1){cin>>target[1];if(target[1]==0) break;stack<int>s;for(int i=2;i<=n;i++){cin>>target[i];}bool ok=true;int A=1,B=1;while(B<=n){if(A==target[B]) {A++;B++;}else if(!s.empty()&&s.top()==target[B]) {s.pop();B++;}else if(A<=n) {s.push(A++);}else {ok=false;break;}}cout<<(ok?"Yes":"No")<<endl;}cout<<endl;}return 0; } 與50位技術專家面對面20年技術見證,附贈技術全景圖總結
- 上一篇: Spreadsheet Tracking
- 下一篇: 【分治的典型应用:归并排序】