vats0726's blog

By vats0726, history, 6 years ago, In English

Hello Friends, I am getting a runtime-Error on my submission to problem 230-A(http://codeforces.net/contest/230/submission/39135638) Plzzz help me out.

  • Vote: I like it
  • 0
  • Vote: I do not like it

»
6 years ago, # |
  Vote: I like it 0 Vote: I do not like it

Hi,

I think there is some problem with erasing the iterator while iterating through the map..

The output of this code shows that the value of m.end() is not updated in the first for loop.

for(int i=0;i<n;i++)
{   int s,b;
    cin>>s>>b;
    m[s]=b;

}

int cnt=0; 
map<int,int>::iterator it,it2;
for(it=m.begin();it!=m.end();it++)
{   
    cout<<"it 1 "<<it->first<<" "<<it->second<<endl;

    for(it2=m.begin();it2!=m.end();it2++)
    {
        cout<<"it 2 "<<it2->first<<" "<<it2->second<<endl;
        if(it2->first<s)
        {   cnt++;
            s+=it2->second;
            m.erase(it2);
            break;
        }
    }
}

Input : 999 2

1010 10

67 89

Output: it 1 67 89

it 2 67 89

it 1 1330860869 1162633042

it 2 1010 10

also, I think m[s]+=b was what you were looking for :)