--- title: S1E 鐵路 tags: solution --- # E. 鐵路 - 本題源自於Onling Judge:[514 - Rails](https://onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=show_problem&problem=455) 題目目標在於給定出站順序的前提下,利用已知入站順序1~N, 確定是否仍能夠以目標出站順序離開。 有一個簡單的想法,我們利用queue的特性來維護出站順序、 利用stack的特性來維護入站順序, 並依次比較它們的front/top是否相同,如果相同就安排出站(pop)。 > <font color=red>由於題目的測資有誤,故此題我們採取直接不計分的方式</font> ```cpp= # include <bits/stdc++.h> using namespace std; int main(){ int N,n,k; while(cin>>N && N!=0){ while(cin>>k && k!=0){ queue<int> out; stack<int> in; out.push(k); for(n = 2 ; n<=N ; n++) cin>>k, out.push(k); for(n = 1 ; n<=N ; n++){ in.push(n); while(!in.empty() && in.top() == out.front()) in.pop(), out.pop(); } cout<<(out.empty() ? "Yes\n" : "No\n"); } cout<<'\n'; } } ```
×
Sign in
Email
Password
Forgot password
or
By clicking below, you agree to our
terms of service
.
Sign in via Facebook
Sign in via Twitter
Sign in via GitHub
Sign in via Dropbox
Sign in with Wallet
Wallet (
)
Connect another wallet
New to HackMD?
Sign up