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 | 3555 |

2 | wxhtxdy | 3519 |

3 | Radewoosh | 3408 |

4 | Benq | 3368 |

5 | mnbvmar | 3280 |

6 | ecnerwala | 3277 |

7 | LHiC | 3276 |

8 | sunset | 3264 |

9 | maroonrk | 3158 |

10 | TLE | 3145 |

# | User | Contrib. |
---|---|---|

1 | Errichto | 190 |

2 | Radewoosh | 177 |

3 | tourist | 173 |

4 | antontrygubO_o | 171 |

5 | Vovuh | 166 |

5 | PikMike | 166 |

7 | rng_58 | 159 |

8 | majk | 156 |

9 | farmersrice | 153 |

9 | Um_nik | 153 |

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?

↑

↓

Codeforces (c) Copyright 2010-2019 Mike Mirzayanov

The only programming contests Web 2.0 platform

Server time: Dec/05/2019 23:57:44 (h3).

Desktop version, switch to mobile version.

Supported by

User lists

Name |
---|

That monotonicity condition is what allows us to use Divide and Conquer optimization to reduce

O(N^2)toO(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 callsolve(L, mid-1, optima_l, optima_mid)andsolve(mid+1, R, optima_mid, optima_r)recursively.There are

logNlevels of recursion. Consider a single level. The TOTAL number of iterations over all intervals in that level isO(n). This is becauseopt[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)