I wrote a recursive solution with O(n^3) time complexity imo.
But I am getting TLE Verdict on submission .
# | User | Rating |
---|---|---|
1 | tourist | 3985 |
2 | jiangly | 3814 |
3 | jqdai0815 | 3682 |
4 | Benq | 3529 |
5 | orzdevinwang | 3526 |
6 | ksun48 | 3517 |
7 | Radewoosh | 3410 |
8 | hos.lyric | 3399 |
9 | ecnerwala | 3392 |
9 | Um_nik | 3392 |
# | User | Contrib. |
---|---|---|
1 | cry | 169 |
2 | maomao90 | 162 |
2 | Um_nik | 162 |
4 | atcoder_official | 161 |
5 | djm03178 | 158 |
6 | -is-this-fft- | 157 |
7 | adamant | 155 |
8 | awoo | 154 |
8 | Dominater069 | 154 |
10 | luogu_official | 150 |
I wrote a recursive solution with O(n^3) time complexity imo.
But I am getting TLE Verdict on submission .
Name |
---|
The main issue is that the parameter
valX
andvalY
is thesolve
funciton may be negative, and lead to many useless states. Since we only care about whether we have enough x and y, so change LINE22 intoreturn dp[i][valX][valY] = min(1 + solve(i + 1, max(0, valX - v[i].ff), max(0, valY - v[i].ss)), solve(i + 1, valX, valY));
would just be fine.Thank u so much!
Still got a TLE. Submission
I got the following update.
Accepted
The default dp value should also be changed. Change
1e9
in LINE21 and LINE43 into-1
.Why taking -1 gets an AC whereas taking 1e9 as dp value gets a TLE.
AC submission
TLE Submission
Difference in the runtime is also huge. TLE submission takes 2.2s still no output whereas AC submission takes 0.3s with just this small change. Can you clarify?
In the TLE Submission, the default dp value is
1e9
. This means that when you call thesolve
function and the currentdp[i][valX][valY]
is 1e9, it keeps doing recursion.Assume current state is valX=10, valY=10, and the suffix of the input data is like
[..., {1,1}, {1,1}, {1,1}, {1,1}]
. We cannot subtract valX and valY to 0 even if we use all the suffix, so thedp[i][valX][valY]
is 1e9. When you visit this state again, LINE21 check that the dp value is 1e9 and still need to be searched. The runtime would be exponential to n in the worst case.Generally speaking, it's a bad idea to mix the
UNVISITED
status and theINVALID
status when we do dfs with memorization.