LEALCO ( JAN13 ) TLE due to push_back

This is my subimssion during the contest.

This is my submission during practice.

Only change is around line number 61.

I changed

vector<int> tnums;
for(int i=0; i<n; ++i)
    tnums.push_back(nums[i]);

to

vector<int> tnums(n);
for(int i=0; i<n; ++i)
    tnums[i] = nums[i];

and it turned TLE to AC.

Is vector.push_back() that slow ?

The version without push_back() passes within time limits even with cin,cout instead of scanf,printf !

In short,
using push_back, scanf and printf gives TLE.
not using push_back and using cin and cout gives AC.

So push_back hurts your code’s performance more than cin,cout ?

as we push into vector, size of array changes. Since it is dynamic, if size exceeds that of current capacity, it initializes double or 1.5 times more memory and copy the old array to new array. This can take time.

1 Like
//