Hi codeforces community!
So I was solving this problem: 1703G - Хороший ключ, плохой ключ
I wrote first solution: 283940469 ~ TLE
I wrote second solution: 283940935 ~ AC
Two programs are very similar to each other so i will list most notable differences between them:
- First solution uses
C-array
a
of sizeN = 1e5
defined globally. Second solution usesvector
of sizen
which is defined for each test case - First solution uses
vector<vector<ll>>
dp
, withN
rows and each row is of size31
and for each test it fills every row withvector<ll>(31, -1)
. Second solution uses similardp
vector but hasn
rows and is defined in thesolve
function - First solution accesses variables
n
,k
,dp
from the global scope. Second solution passes references tovectors
as parameters and variablek
also as parameters for each recursive call.
I sincerely thought first solution would be faster than second solution.
So, my question is why is that first solution gets TLE but second solution gets AC?