模拟题一直是自己的噩梦,调了一上午才调出来这道题。
感觉模拟题做起来痛苦的主要原因在于:
1 题目需求布豪实现,题意晦涩
2 很难像其它算法题那样学到思路,出现错误,往往是一些细节问题导致的,并非思维和知识点上的错误,修正这些细节错误感觉很难学到东西。
代码如下:
#include<bits/stdc++.h>
using namespace std;
const int N = 100010,M= 110;
int n , m , k ;
deque<int> q;
stack<int> stk;
stack<int> s;
void print(){
/// cout << "print :" << endl;
stack<int> outpt;
while(s.size()){
int t = s.top();
s.pop();
outpt.push(t);
}
while(outpt.size()>1){
int t = outpt.top();
outpt.pop();
cout << t << ' ' ;
}
// cout << outpt.size() << endl;
int t = outpt.top();
cout << t << endl ;
// cout << outpt[outpt.size()-1] << endl;
}
int main (){
cin >> n >> m >> k ;
for(int i=1;i<=n;i++){
int x ;
cin >> x ;
q.push_back(x);
}
while(q.size()||stk.size()||s.size()){
// cout << q.size() << endl;
int t;
//
// cout << "q:" << q.size() << endl;
// cout <<"s:" << s.size() << endl;
// cout << "stk:" << stk.size() << endl;
// if(s.size()) cout << s.top() << endl;
if(!stk.size()){
// cout << "0000" << endl;
if(q.size()==0){
print();
continue;
}
t = q.front();
//cout << q.size() << "test1" << endl;;
q.pop_front();
//cout << q.size() << "test2" << endl;
// cout << "t:" << t << endl;
//q.pop_front();
if(!s.size()){
s.push(t);
}else {
if(t>s.top()){
stk.push(t);
}else{
s.push(t);
}
}
}else{
// cout << 1111 << endl;
// cout << stk.size() << endl;
t = stk.top();
stk.pop();
if(!s.size()) {
s.push(t) ;
continue;
}
//cout << "debug not empty:"<< t << endl;
if(t>s.top()){
stk.push(t);
if(q.size()==0){
print();
continue;
}
t = q.front();
q.pop_front();
// if(t==15) cout << "debug15" << endl;
if(t>s.top()){
if(stk.size()==m){
print();
// cout << "end of print" << endl;
q.push_front(t);
}else{
stk.push(t);
}
}else{
s.push(t);
}
}else{
s.push(t);
}
}
//cout << "come here" << endl;
if(s.size()==k){
print();
}
}
return 0 ;
}