This is about yesterday's Div2 contest Problem B. For reference, here's my code which got RE on system test 7. Finally, I fixed my code and got AC. Read the blunder I made. I hope you people handle it elegantly the next time you encounter this. Pay attention to the last part of the code which includes the two for loops.↵
↵
<spoiler summary="Wrong code">↵
~~~~~↵
void solve(){↵
ll n,a;↵
cin>>n;↵
vector<ll> o,e;↵
rep1(i,2*n){↵
cin>>a;↵
if(a%2)↵
o.pb(i);↵
else↵
e.pb(i);↵
}↵
if(e.size()>o.size())↵
swap(o,e);↵
if(o.size()%2){↵
o.pop_back();↵
e.pop_back();↵
}↵
else{↵
o.pop_back();↵
o.pop_back(); ↵
}↵
for(ll i=0; i<o.size()-1; i+=2){↵
wr(o[i], o[i+1]);↵
}↵
for(ll i=0; i<e.size()-1; i+=2){↵
wr(e[i], e[i+1]);↵
}↵
}↵
~~~~~↵
</spoiler>↵
↵
The problem here is while outputting the answer, I write the loop's end condition as `i < o.size()-1` and `i < e.size()-1` .↵
Now,this works fine when the vector isn't empty but when the vector is empty, size() returnssize() returns an `unsigned int` (In this case, an unsigned 0) and when I subtract 1 from it, like this `i < e.size()-1`, I get a huge number instead of -1 which I expected I'd get. The loop runs for quite a long time instead of never running at all.↵
↵
Now, to fix this there are 2 ways you can do so.↵
↵
1. Typecast the returned value into int like this `(int)(e.size()-1)`.↵
2. Store this returned value from size method into a variable like this `int sz = e.size()`.↵
3. As [user:HideBehind,2020-06-21] mentioned, the simplest way is to write it as `i+1 < e.size()`.↵
↵
Here's my working code showing both the methods.↵
↵
<spoiler summary="Got AC with it">↵
↵
~~~~~↵
void solve(){↵
ll n,a;↵
cin>>n;↵
vector<ll> o,e;↵
rep1(i,2*n){↵
cin>>a;↵
if(a%2)↵
o.pb(i);↵
else↵
e.pb(i);↵
}↵
if(e.size()>o.size())↵
swap(o,e);↵
if(o.size()%2){↵
o.pop_back();↵
e.pop_back();↵
}↵
else{↵
o.pop_back();↵
o.pop_back(); ↵
}↵
int sz = o.size();↵
for(ll i=0; i<sz-1; i+=2){↵
wr(o[i], o[i+1]);↵
}↵
for(ll i=0; i<(int)e.size()-1; i+=2){↵
cout << e[i]<<" "<<e[i+1]<<"\n";↵
}↵
}↵
~~~~~↵
↵
↵
</spoiler>↵
↵
I felt like a dumbo after RE on system tests, fixed it, and then thought to inform you all guys (my first blog).↵
↵
**PS:** Making a mistake like this deals some serious damage on ratings XD
↵
<spoiler summary="Wrong code">↵
~~~~~↵
void solve(){↵
ll n,a;↵
cin>>n;↵
vector<ll> o,e;↵
rep1(i,2*n){↵
cin>>a;↵
if(a%2)↵
o.pb(i);↵
else↵
e.pb(i);↵
}↵
if(e.size()>o.size())↵
swap(o,e);↵
if(o.size()%2){↵
o.pop_back();↵
e.pop_back();↵
}↵
else{↵
o.pop_back();↵
o.pop_back(); ↵
}↵
for(ll i=0; i<o.size()-1; i+=2){↵
wr(o[i], o[i+1]);↵
}↵
for(ll i=0; i<e.size()-1; i+=2){↵
wr(e[i], e[i+1]);↵
}↵
}↵
~~~~~↵
</spoiler>↵
↵
The problem here is while outputting the answer, I write the loop's end condition as `i < o.size()-1` and `i < e.size()-1` .↵
Now,
↵
Now, to fix this there are 2 ways you can do so.↵
↵
1. Typecast the returned value into int like this `(int)(e.size()-1)`.↵
2. Store this returned value from size method into a variable like this `int sz = e.size()`.↵
3. As [user:HideBehind,2020-06-21] mentioned, the simplest way is to write it as `i+1 < e.size()`.↵
↵
Here's my working code showing both the methods.↵
↵
<spoiler summary="Got AC with it">↵
↵
~~~~~↵
void solve(){↵
ll n,a;↵
cin>>n;↵
vector<ll> o,e;↵
rep1(i,2*n){↵
cin>>a;↵
if(a%2)↵
o.pb(i);↵
else↵
e.pb(i);↵
}↵
if(e.size()>o.size())↵
swap(o,e);↵
if(o.size()%2){↵
o.pop_back();↵
e.pop_back();↵
}↵
else{↵
o.pop_back();↵
o.pop_back(); ↵
}↵
int sz = o.size();↵
for(ll i=0; i<sz-1; i+=2){↵
wr(o[i], o[i+1]);↵
}↵
for(ll i=0; i<(int)e.size()-1; i+=2){↵
cout << e[i]<<" "<<e[i+1]<<"\n";↵
}↵
}↵
~~~~~↵
↵
↵
</spoiler>↵
↵
I felt like a dumbo after RE on system tests, fixed it, and then thought to inform you all guys (my first blog).↵
↵
**PS:** Making a mistake like this deals some serious damage on ratings XD