0

right now I'm trying to solve a competitive programming problem but whenever I try to push to a vector (bb), it gives me a segfault. I've tried fixing this by switching from stack to vector and by just attempting to push a regular integer with no variables, but I've had no luck.

#include <bits/stdc++.h>
#include <iostream>
#include <stack>
using namespace std;


int main(){
    ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    int t, n;
    cin >> t;

    for(int e = 0; e < t; e++){

        cin >> n;
        int want = 1, temp;
        bool flag = true;
        stack<int> mountain;
        vector<int> bb;

        for(int i = 0; i < n; i++){
            cin >> temp;
            mountain.push(temp);
        }

        while(!mountain.empty() || !bb.empty()){

            if(mountain.top() == want){
                mountain.pop();
                want++;

            }else if(!bb.empty() && bb.back() == want){
                bb.pop_back();
                want++;

            }else if(mountain.size() > 1){
                 bb.push_back(mountain.top()); // <-----------------
                mountain.pop();

            }else{
                cout << "N\n";
                flag = false;
                break;

            }
            if(flag && want == n + 1) cout << "Y\n";
        } 

    }   

    return 0;
    }

If you need any context for the problem it can be found here.

Xiao
  • 17
  • 1
  • This reads like a typical puzzle from some online contest site. If your goal is to learn C++, you will not learn anything there. In nearly all cases, like this one, the correct solution requires knowing some kind of a mathematical or a programming trick. If you don't know what the trick is, and attempt to code a brute-force approach, your program runs forever, and fails for that reason. If you're trying to learn C++, you won't learn anything from meaningless online contest sites [but only from a good C++ book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). – Sam Varshavchik Apr 10 '20 at 11:38
  • @SamVarshavchik Thanks for the quick response, but I'm not concerned with the actual problem solving, I'm quite sure that I have a good solution planned out right now. It's just that one line of code where I try to push to bb that is giving me trouble. – Xiao Apr 10 '20 at 11:42
  • Can you post the problematic input to your program as well? – D. Jones Apr 10 '20 at 11:46
  • @D.Jones `2 4 2 3 1 4 4 4 1 3 2` – Xiao Apr 10 '20 at 11:47
  • 2
    If you're not "concerned with the actual problem solving" and would like to understand the reason for the segfault, then this is simply what your debugger is for. Just use your debugger to run your program, one line at a time, monitor the values of all variables, observing them, as they change, and see, for yourself, the reason for the segfault. What did you see when you ran this in a debugger? – Sam Varshavchik Apr 10 '20 at 11:53
  • 1
    I didn't try to actually solve the problem but your problem is not in vector or push_pack. segfault is from the line `if(mountain.top() == want){`. You are trying to get the value at the top of an empty stack. Which I acquired that info from a simple `gdb` session. – D. Jones Apr 10 '20 at 12:09
  • @D.Jones I just did the same as you and posted... lol... – g19fanatic Apr 10 '20 at 12:13

1 Answers1

0

When I compile your code and run it through gdb, I get a segfault at the following line:

 while(!mountain.empty() || !bb.empty()){

            if(mountain.top() == want){    <-------------------
                mountain.pop();
                want++;

This is because in your while check, you test whether bb OR mountain is empty to continue. bb isn't empty, mountain is. You're then trying to call mountain.top() which is causing your segfault.

Need to clean up some logic here.

g19fanatic
  • 10,567
  • 6
  • 33
  • 63