慄醬有一天在網上衝浪的時候發現了一道很有意思的數據結構題。
該數據結構形如長條形。
一開始該容器爲空,有以下七種操作。
1 a從前面插入元素a
2 從前面刪除一個元素
3 a從後面插入一個元素
4 從後面刪除一個元素
5 將整個容器頭尾翻轉
6 輸出個數和所有元素
7 對所有元素進行從小到大排序
輸入描述:
只有一組數據,第一行n≤50000,m≤200000, a≤100000 代表最大數據數目和操作次數。
接下來每一行一個操作如上描述。保證所有操作合法(不會在容器爲空時刪除元素)。
6、7操作共計不會超過10次。
輸出描述:
當執行6操作時,第一行先輸出當前的個數,然後從頭到尾按順序輸出,每兩個元素之間用一個空格隔開,末尾不能有空格。
思路:這個題完全是爲雙向隊列而出題的!
#include<bits/stdc++.h>
#define IOS ios::sync_stdio(false); cin.tie(0); cout.tie(0);
using namespace std;
deque<int> st;
int main(){
//IOS;
int n,m;
cin>>n>>m;
while(m--){
int k;
cin>>k;
int res;
if(k==1){
cin>>res;
st.push_front(res);
}
if(k==2){
st.pop_front();
}
if(k==3){
cin>>res;
st.push_back(res);
}
if(k==4){
st.pop_back();
}
if(k==5){
reverse(st.begin(),st.end() );
}
if(k==6){
cout<<st.size()<<endl;
for(auto i=st.begin();i!=st.end();i++){
cout<<*i<<" ";
}
cout<<endl;
}
if(k==7){
sort(st.begin(),st.end());
}
}
return 0;
}