mardi 4 octobre 2016

Is the following code correct?

I wrote the following code as a solution to the following question Codechef CLEANUP problem

#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 runs fine on my computer, but on the website, I get an SIGSEGV error. SIGSEGV error I don't really find any extra usage or out of bounds call in the program. What is the problem?

Aucun commentaire:

Enregistrer un commentaire