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 1303. Minimal Coverage

WA14 Access violation
Posted by Havard 22 May 2013 17:43
#include <cstdio>
#include <climits>
#include <cstdlib>
#include <algorithm>
#include <list>
#include <vector>

using namespace std;

#define PB push_back
#define BEG begin()
#define MP make_pair
#define F first
#define S second

bool compare(const pair<int,int> &left, const pair<int,int> &right){
    return left.F <= right.F;
}

int main(){
    int M;
    int readInt1, readInt2;
    vector<pair<int,int> > segments;
    vector<int> solutionInd;
    scanf("%d", &M);
    while(scanf("%d %d", &readInt1, &readInt2) && !(readInt1 == 0 && readInt2 == 0)){
        segments.PB(MP(readInt1, readInt2));
    }
    sort(segments.begin(), segments.end(), compare);
    if(segments.empty() || segments[0].F > 0){
        printf("No solution\n");
        return 0;
    }

    int currEnd = 0;
    int segInd = 0;
    int ansInd = -1;
    int maxEnd = 0;

    while(segInd < segments.size()){
        while(segInd < segments.size() && segments[segInd].F <= currEnd){
            if(segments[segInd].S > maxEnd){
                maxEnd = segments[segInd].S;
                ansInd = segInd;
            }

            segInd++;
        }
        solutionInd.PB(ansInd);
        currEnd = maxEnd;

        if(currEnd >= M || segments[segInd].F > currEnd)
            break;
    }
    if(solutionInd.empty() || currEnd < M){
        printf("No solution\n");
    } else{
        printf("%d \n", solutionInd.size());
        for(int i = 0; i<solutionInd.size(); i++){
            printf("%d %d\n", segments[solutionInd[i]].F, segments[solutionInd[i]].S);
        }
    }

}

Keep getting runtime error(Access violation) even though I can't find anywhere it is possible to go outside the vectors memory space.

Re: WA14 Access violation
Posted by Havard 23 May 2013 17:06
Update: The problem was accepted when using visual studio compiler instead of g++.
Re: WA14 Access violation
Posted by Md. Taufique Hussain 4 Aug 2013 00:01
Same thing happened to me. But why is that?
Re: WA14 Access violation
Posted by Kartik Parnami 15 Apr 2014 04:18
Hi,
I get WA6 with g++ but get runtime error access violation on test case 14 with Visual Studio 2010 C++.

Code :

/*
 * File:   main.cpp
 * Author: Parnami
 * Created on April 14, 2014, 10:18 PM
 * Description : TIMUS Online Judge Problem ID : 1303 (DP)
 */

#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <vector>
#include <map>
#include <string>
#include <queue>
#include <cmath>
#include <utility>
#include <limits.h>

using namespace std;

inline int fastRead()
{
    int input;
    char c=0;
    while (c<33) c=getchar();
    input=0;
    while (c>33)
    {
          input=input*10+c-'0';
          c=getchar();
    }
    return input;
}

vector < pair <int,int> > myVec,answer;

bool inRange(pair <int,int> cur, int starter)
{
    if(starter>=cur.first && starter<cur.second)
        return true;
    return false;
}

int main(int argc, char** argv) {

    int m,starter,ender,iter,i,flag;
    pair <int,int> maxxer,current;
    m = fastRead();
    while(1)
    {
        cin>>starter>>ender;
        if(starter==0&&ender==0)
            break;
        if(ender<=0||(starter>=m&&ender>m))
        {
            //Do Nothing
        }
        else
        {
            //cout<<"Pushing "<<starter<<"-"<<ender<<endl;
            myVec.push_back(make_pair(starter,ender));
        }
    }
    if(!myVec.empty())
    {
        sort(myVec.begin(),myVec.end());
        iter = 0;
        starter=0;
        while(starter<m)
        {
            //cout<<starter<<endl;
            current = myVec[iter];
            //cout<<"Yahaan Phuncha"<<endl;
            flag = 0;
            while(!inRange(current,starter) && iter!=myVec.size())
            {
                //cout<<"Not in range : "<<current.first<<"-"<<current.second<<endl;
                iter++;
                current = myVec[iter];
            }
            if(iter==myVec.size())
            {
                //cout<<"End of vector nowhere to search"<<endl;
                flag = 1;
                break;
            }
            maxxer = current;
            iter++;
            if(iter==myVec.size())
            {

            }
            else
            {
                while(1)
                {
                    if(myVec[iter].first>starter)
                    {
                        break;
                    }
                    else
                    {
                        if(myVec[iter].second>maxxer.second)
                        {
                            maxxer = myVec[iter];
                        }
                        iter++;
                    }
                }
            }
            //cout<<"Pushing into answer "<<maxxer.first<<"-"<<maxxer.second<<endl;
            answer.push_back(maxxer);
            starter = maxxer.second;
        }
    }
    else
        flag=1;
    if(flag)
    {
        cout<<"No solution"<<endl;
    }
    else
    {
        cout<<answer.size()<<endl;
        for(i=0;i<answer.size();i++)
        {
            cout<<answer[i].first<<" "<<answer[i].second<<endl;
        }
    }
    return 0;
}

Edited by author 15.04.2014 04:19