題目目標在於給定出站順序的前提下,利用已知入站順序1~N,
確定是否仍能夠以目標出站順序離開。
有一個簡單的想法,我們利用queue的特性來維護出站順序、
利用stack的特性來維護入站順序,
並依次比較它們的front/top是否相同,如果相同就安排出站(pop)。
由於題目的測資有誤,故此題我們採取直接不計分的方式
# 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';
}
}
C. 完美平方數 #include <iostream> #include <math.h> using namespace std; int numSquares(int n); int main() { int num, ans;
Jan 27, 2022C. 組合 #include<bits/stdc++.h> using namespace std; vector<int> a; bool first=true,f=true; fstream input,output; void find(int g,vector<int> &can,int p){ if(!g){ if(!first){
Sep 19, 2021D. Flood Fill # include <bits/stdc++.h> using namespace std; int m[102][102]; struct Fill{ int x,y,t; };
Sep 16, 2021B. 嚴格二元樹 # include <iostream> using namespace std; struct Node{ int val, depth; Node *left, *right; Node(int v, int h){ val = v, depth = h;
Sep 16, 2021or
By clicking below, you agree to our terms of service.
New to HackMD? Sign up