I was solving a question which required (according to my solution) stack implementation. First I used STL stack for the it. But memory limit was exceeded. Then I created my own class for stack but issue was still the same, which at least cleared that STL was not at fault. I need your advice, why is occupying 65952 KiB (that too at error).
Please note: I have provided code so that there is no other confusion as my code could be wrong as well. I am asking anyone to debug it, I am just interested in knowing memory issues.
And code is correctly working, those ampersand after cin or cout are actually appropriate >> and << operators, please ignore them
Code is provided below
#include<bits/stdc++.h>
using namespace std;
class st{
vector<int> arr;
int tp;
public:
st(){
tp = -1;
}
void push(int a){
++tp;
arr.push_back(a);
}
void pop(){
--tp;
}
int top(){
return arr[tp];
}
int size(){
return tp+1;
}
void reset(){
tp = -1;
}
};
int main(){
int t;
cin >> t;
st s;
while(t--){
s.reset();
vector<int> vec;
vector<int> del;
vector<int>::iterator itr;
int a,n,k;
cin >> n >> k;
for(int i = 0; i < n; ++i){
cin >> a;
vec.push_back(a);
if(i > 0){
while(s.size() and a > vec[s.top()]){del.push_back(s.top());s.pop();}
}
s.push(i);
}
int i;
for(i = 0; i < k && i < del.size(); ++i){
itr = vec.begin()+del[i]-i;
vec.erase(itr);
}
if(i != k){
while(i < k){
itr = vec.end()-i;
vec.erase(itr);
++i;
}
}
for(i = 0; i < vec.size(); ++i) cout << vec[i] << " ";
cout << endl;
}
return 0 ;
}