Dynamic Programming21 sections · 916 units
Open in Course

Codeforces 478D Red Green Towers - The Setup

Understanding constraints

You're given rr red blocks and gg green blocks where 1r,1 \le r, g2105 \le 2 \cdot 10^5. A tower of height hh uses exactly 1+2+3++h=h(h+1)21 + 2 + 3 + \dots + h = \frac{h(h+1)}{2} blocks total.

You want the tallest tower possible. Find the largest hh where h(h+1)2r+g\frac{h(h+1)}{2} \le r + g. Let S=h(h+1)2S = \frac{h(h+1)}{2}. The remaining r+gSr + g - S blocks go unused. You don't need to use all blocks. Understanding the problem structure is half the battle.