Hi CodeForces!
In this blog written that in problems Divide and Conquer Optimization opt[i] <= opt[i + 1]
opt[i] -> such k that gives optimal answer, for example dp[i] = dp[k - 1] + C[k][i]
Please, can you explain why?
# | 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 |
Hi CodeForces!
In this blog written that in problems Divide and Conquer Optimization opt[i] <= opt[i + 1]
opt[i] -> such k that gives optimal answer, for example dp[i] = dp[k - 1] + C[k][i]
Please, can you explain why?
Name |
---|
That monotonicity condition is what allows us to use Divide and Conquer optimization to reduce O(N^2) to O(N*logN). Take a look at some sample code.
We call solve(L, R, optima_l, optima_r) to find the optimal split points for all indices i such that L <= i <= R, subject to the condition that optima_l <= opt[i] <= optima_r. We achieve this by finding the optimal point for mid=(L+R)/2 by iterating in the range [optima_l, optima_r]. Then we call solve(L, mid-1, optima_l, optima_mid) and solve(mid+1, R, optima_mid, optima_r) recursively.
There are logN levels of recursion. Consider a single level. The TOTAL number of iterations over all intervals in that level is O(n). This is because opt[1] <= opt[2] <= opt[3] .... <= opt[N]. So this monotonicity condition is the reason we can reduce the time complexity in this manner.
Sometimes, formally proving the monotonicity condition can become difficult for a problem, maybe during an ongoing contest. In that case, I mostly rely on intuition or use another condition (which is sufficient but not necessary) to apply this optimization. I have found this second quadrangular inequality condition to be applicable to many problems. :)
Thank you)