ENG  RUSTimus Online Judge
Online Judge
Problems
Authors
Online contests
About Online Judge
Frequently asked questions
Site news
Webboard
Links
Problem set
Submit solution
Judge status
Guide
Register
Update your info
Authors ranklist
Current contest
Scheduled contests
Past contests
Rules
back to board

Discussion of Problem 1220. Stacks

Using C++ STL gives Memory access Runtime Error
Posted by Rajvijay 20 Nov 2018 01:19
#include<bits/stdc++.h>
using namespace std;
int main(){
    long N,i,stack_id,num;
    string code;
    const string push="PUSH";
    vector< stack<int> > stacks;


    cin>>N;

    for(i=0;i<N;i++){
        cin>>code;

        if(code==push){
            cin>>stack_id>>num;

            if(stack_id>stacks.size()){
                stacks.push_back( stack<int>() );
            }
            stacks[stack_id-1].push(num);

        }
        else{
            cin>>stack_id;

            cout<<stacks[stack_id-1].top()<<"\n";
            stacks[stack_id-1].pop();
        }
    }

    return 0;
}



I get correct output on the sample code though.
Re: Using C++ STL gives Memory access Runtime Error
Posted by ToadMonster 20 Nov 2018 13:37
Try test:
1
push 900 1

Btw, have you seen memory limit for this task?
You need to be able to execute 100,000 pushes of 4-bytes integers (in one stack and in random stacks) - 400 Kb of data + empty program requires ~200K.

Your implementation should fail (memory limit) during vector resize.