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

×

Why doesn't the following code for CLEANUP work?

#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
int main()
{
    int t;
    cin >> t;
    while(t--)
    {
        int num_job, job_com;
        cin >> num_job >> job_com;
        vector<int> j;
        int k;
        for(int i = 0; i < job_com; i++)
        {
            cin >> k;
            j.push_back(k);
        }
        if(num_job == job_com)
        {
            cout << endl;
            cout << endl;
        }
        else
        {
        sort(j.begin(), j.end());
        bool m = true;
        vector<int> chef;
        vector<int> ass;
        int l = 1;
        int i = 1;
        while(l<=num_job - job_com)
        {
            while(m == true && l <= num_job - job_com)
            {
                if(i == j[0])
                {
                    i = i + 1;
                    j.erase(j.begin());
                }
                else if(i != j[0])
                {
                      chef.push_back(i);
                      i = i + 1;
                      m = false;
                      l = l + 1;
                }
            }
            while(m == false && l <= num_job - job_com)
            {
                if(i == j[0])
                {
                    i = i + 1;
                    j.erase(j.begin());
                }
                else if(i != j[0])
                {
                      ass.push_back(i);
                      i = i + 1;
                      m = true;
                      l = l + 1;
                }
            }
        }
        int yo = chef.size();
        int oy = ass.size();
        for(int c = 0; c <  yo; c ++)
        {
            cout << chef[c] << " ";
        }
        cout << endl;
        if(oy != 0)
        {
        for(int c = 0; c < oy; c ++)
        {
            cout << ass[c] << " ";
        }
        cout << endl;
        }
        else
        {
        cout << endl;
        }
    }      
    }
    return 0;
}

It works fine on my machine, but when I submit it, I get an SIGSEGV error.

asked 02 Oct '16, 21:12

ritik's gravatar image

2★ritik
111
accept rate: 0%

edited 02 Oct '16, 21:13


Hi

This is an error caused by an invalid memory reference or segmentation fault. The most common causes are accessing an vector element out of bounds, or using too much memory.

Some things for you to try:

Make sure you aren't using variables that haven't been initialized. These may be set to 0 on your computer, but aren't guaranteed to be on the judge.

Check every single occurrence of accessing an vector element and see if it could possibly be out of bounds.

Hope this helps.

link

answered 02 Oct '16, 22:05

smsubham's gravatar image

3★smsubham
675216
accept rate: 15%

edited 02 Oct '16, 22:12

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:

×316
×29

question asked: 02 Oct '16, 21:12

question was seen: 402 times

last updated: 02 Oct '16, 22:12