You are not logged in. Please login at www.codechef.com to post your questions!

×

Stack memory issue

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 ;
}

asked 04 Jun '18, 06:25

ay2306's gravatar image

4★ay2306
2329
accept rate: 11%

first of all post the question for which u implemented this code.

(04 Jun '18, 15:04) gyanendra3713★
toggle preview
Preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "title")
  • image?![alt text](/path/img.jpg "title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported
  • mathemetical formulas in Latex between $ symbol

Question tags:

×1,917
×173
×20

question asked: 04 Jun '18, 06:25

question was seen: 91 times

last updated: 05 Jun '18, 18:24