[{"content":"The Inter IIT Tech Meet 15.0 Bootcamp gave us a quant problem. Kushagra Verma and I solved it as team Jain Streeter. This post explains the task, the research and the method.\nProblem statement The task is to build two trading strategies. One strategy is for BTC/USDT. The other is for ETH/USDT. The data is from Binance, from 2021 to 2025.\nEach strategy has its own risk plan. The plan includes stop losses and a risk-to-reward ratio.\nThe rules are strict:\nEach trade has a cost of 0.15% (fee and slippage). This includes entries, exits, size changes and stops. Backtesting libraries are not permitted. The engine, the metrics and the indicators use only numpy, pandas and scipy. Each strategy must report 15 metrics, a 5-year equity curve and a trade history. The judges also test the strategies on hidden data. The score rewards four things: return for each unit of risk, small drawdowns, fast recovery, and better results than buy-and-hold in more than half of the quarters. Robustness is more important than the highest return. This rule guided every decision.\nResearch results We studied the training data (2021 to 2023) before we wrote any rule. We found three important facts.\nFact 1: Direction is almost random. Volatility is not.\nNo indicator predicted the direction of the next week. Machine learning classifiers had an AUC of 0.40 to 0.51. A random guess has an AUC of 0.5. A test with shuffled labels had an AUC of 0.52. Trades from the classifiers lost 22% on BTC and 82% on ETH.\nVolatility is different. A simple HAR model explains 60% (BTC) and 73% (ETH) of the next-day log volatility. This test used data that the model did not see.\nFact 2: Trend following makes money only in calm, orderly trends.\nWe divided the days by volatility and trend quality. A momentum trade made money in calm trends in 4 of 4 years. In high volatility, it made almost no money.\nFact 3: Bear markets cause most of the damage to buy-and-hold.\nBTC fell 77% in 2022. It then stayed below its old high for about 780 days. After a loss of 65%, a gain of 186% is necessary to recover. For this reason, it is more important to avoid the fall than to catch each rise.\nThe method trend votesdecides whether to holdmarket statedecides how muchposition×= Two separate jobs. Multiply them to get the position. The method has two parts. The trend signal decides whether to hold. The market state decides how much to hold.\nThe trend signal uses 7 votes. A vote is up when the close price is above a moving average. The averages are 20, 30, 50, 75, 100, 150 and 200 days. We use the share of the votes that are up.\nWe do not use only one lookback period. Results change too much between neighbors. For example, 30-day momentum has a Sharpe ratio of 1.08. The 20-day and 45-day values are 0.77 and 0.68.\nEach day, the code gives the market a state. It uses only data up to the close of that day.\nStorm0.15Downtrend0.04Bear rally0.38Calm uptrend1.42Volatile uptrend0.54Uptrend0.79Fading uptrend0.271x = fully invested Average long position in each state (BTC, 2021 to 2025). The position is large only in a calm uptrend. All thresholds use the history of the data itself. No threshold is a fixed number. For this reason, the same code works on a new coin or a new price level.\nThe daily decision The long position is the product of these layers:\nLayer Condition Multiplier Trend votes Share of the 7 averages below the close 0 to 1 Regime Calm trend, stormy, or neither 1.5, 0.6 or 1.0 Storm brake Downside risk is more than 1.5 times its 1-year median 0.5 Squeeze Bollinger Bands are inside the Keltner Channel 0.75 200-day gate The long-term trend is down 0.5 The long target is min(votes x regime x brake x squeeze, 1.5) x gate. It is a fraction of the equity.\nEach layer has evidence. For example, the 200-day gate reduced the loss in 2022 from -34% to -16%. Rallies in the 2022 bear market caused most of the loss without the gate.\nThe short side (BTC only) The BTC strategy (CTR-S) can open a short position of 0.5 times the equity. It opens a short only when all five conditions are true:\nThe long target is zero. The 200-day gate is down. All 7 votes are down. Volatility is calm. BTC is less than 60% below its 365-day high. Condition 5 is necessary because rebounds are strongest after a crash.\nWe defined four variants before we ran any test. We also defined the pass criteria before the test. Only one variant passed all criteria. The same four variants made ETH worse. For this reason, ETH is long only.\nTiming without look-ahead A backtest is not valid if it uses future data. For this reason, the engine has a strict rule. A decision from bar t is executed at the open of bar t+1 or later.\nbar tbar t+1close of t: decideopen of t+1: fill The decision uses only closed data. The order fills later. We also use truncation tests. We cut the data at a chosen point and run the code again. All decisions before the cut must be identical. If they are not identical, the code used future data and the test fails.\nResults Each test starts with 10,000 USDT. It includes the 0.15% cost on each fill, execution at the next open, and interest on borrowed money.\n2021 to 2025 BTC CTR-S BTC buy-and-hold ETH CTR-ETH ETH buy-and-hold Total return +368.4% +197.9% +481.7% +306.5% Sharpe ratio 0.98 0.67 0.96 0.75 Maximum drawdown -29.6% -76.6% -37.3% -79.3% +60+372021-64-12022+156+812023+121+1002024-6-42025buy and holdCTR-S BTC return for each year. The strategy is behind in bull years and ahead in bear years. In strong bull years, the strategy returns less than buy-and-hold. In bear years, it loses much less. In 2022, the strategy lost 1.1%. Buy-and-hold lost 64.2%.\nWhat did not work Failed tests are part of the evidence. They show that we did not select only the good results. These ideas failed:\nA fixed trailing stop of 3 times the ATR. It triggered 37 times. The BTC Sharpe ratio fell from 1.16 to 0.71. Machine learning and reinforcement learning. Q-learning lost about 79% on BTC and 83% on ETH, with all five seeds. The final strategies do not use ML or RL. Short-term mean reversion. The edge for each trade was smaller than the round-trip cost of 0.30%. The cost explains most of the failures. The result is approximately N x (e - 0.30%). N is the number of round trips in a year. The letter e is the average edge for each trade. Published intraday effects are 2 to 15 basis points. Each round trip costs 30 basis points here.\nLimitations The strategy is slower than buy-and-hold in strong rallies. It is better over a full market cycle, but not in each year. Jumps often start in calm markets. In those markets, the strategy holds up to 1.5 times the equity. The borrow cost for the short is an assumption (10% a year). The BTC short made only 23 trades in almost nine years. Two bear markets caused most of the gain. BTC and ETH fall together. The two strategies do not give diversification in a crash. Run the code docker build -t ctr . \u0026amp;\u0026amp; docker run --rm ctr selftest docker run --rm -v \u0026#34;$PWD/runs:/app/runs\u0026#34; ctr run data/raw/BTCUSDT_1h.csv The repository contains the technical report, the presentation and all results.\nPriyanshuIITGHY2006/InterIIT_Quant_Jain-StreeterCalm-Trend Regime (CTR)Code, technical report, presentation and results. ","permalink":"https://priyanshudebnath.me/posts/calm-trend-regime/","summary":"Two daily trading strategies for BTC and ETH. The strategies do not predict direction. They change the position size to fit the market.","title":"Calm-Trend Regime: Trading Strategies for Bitcoin and Ether"},{"content":"I got TLE on test 14 with a solution that was completely correct. Then I changed one loop, the order in which I check indices, and it passed comfortably.\nNothing else changed. Same recursion, same math, same checks. Just the order. That trick is worth knowing, because it shows up in far more problems than this one.\nThe problem E. Vlad, Misha and Two Arrays Codeforces Round 1102 (Div. 2)Rating 21002 s · 256 MB divide and conquercombinatoricsmathdfs and similar Vlad has a permutation ppp of length nnn. For every iii he counts the subarrays [l,r][l, r][l,r] whose minimum is pip_ipi​, and writes that count into aia_iai​. You get aaa and have to count how many permutations produce it, modulo 109+710^9 + 7109+7. Here n≤5⋅105n \\le 5 \\cdot 10^5n≤5⋅105 and ai≤1012a_i \\le 10^{12}ai​≤1012.\nThe idea Take any segment [l,r][l, r][l,r] and let kkk be the position of its minimum. Every subarray inside the segment has exactly one minimum, so the aaa-values of a valid segment always add up to\n∑i=lrai=m(m+1)2,m=r−l+1.\\sum_{i=l}^{r} a_i = \\frac{m(m+1)}{2}, \\quad m = r - l + 1.i=l∑r​ai​=2m(m+1)​,m=r−l+1.The minimum splits the segment in two. Everything to its left is a smaller, independent copy of the same problem, and so is everything to its right. That\u0026rsquo;s a Cartesian tree, built from the top.\nSo how do you find kkk when all you have is aaa? You look for the index where the left part sums to exactly (i−l+12)\\binom{i-l+1}{2}(2i−l+1​) and the right part sums to exactly (r−i+12)\\binom{r-i+1}{2}(2r−i+1​). Prefix sums make that an O(1)O(1)O(1) check per index.\nOnce you have the split, counting is easy. The left side has k−lk - lk−l values and the right side has r−kr - kr−k. The values themselves don\u0026rsquo;t care about positions, only about relative order, so you pick which of the r−lr - lr−l remaining values go to the right subtree:\nf(l,r)=(r−lr−k)⋅f(l,k−1)⋅f(k+1,r)f(l, r) = \\binom{r-l}{r-k} \\cdot f(l, k-1) \\cdot f(k+1, r)f(l,r)=(r−kr−l​)⋅f(l,k−1)⋅f(k+1,r)If no index passes the check, the answer is 0.\nWhy only one index can pass the check Say the real minimum is at kkk and you\u0026rsquo;re testing some i\u0026lt;ki \u0026lt; ki\u0026lt;k. The right part [i+1,r][i+1, r][i+1,r] already accounts for its (r−i+12)\\binom{r-i+1}{2}(2r−i+1​) internal subarrays. But the subarray [i,k][i, k][i,k] crosses the boundary and its minimum sits at kkk, on the right. That\u0026rsquo;s at least one extra subarray, so the right sum comes out strictly too big. The case i\u0026gt;ki \u0026gt; ki\u0026gt;k is the mirror image, with the left sum too big.\nSo for a valid array the first index that passes is the minimum, and you can stop searching there. I also brute-forced every permutation up to n=8n = 8n=8 to be sure. The number of distinct valid arrays comes out to the Catalan numbers (1, 2, 5, 14, 42, \u0026hellip;), exactly the number of Cartesian tree shapes. Cute.\nWhere it went wrong My first version found the split with the obvious loop:\nfor (int i = l; i \u0026lt;= r; i++) { if (leftSum(l, i) == tri(i - l) \u0026amp;\u0026amp; rightSum(i, r) == tri(r - i)) { // recurse on [l, i-1] and [i+1, r] } } Correct. Also quadratic.\nLook at the second sample: a = 1 2 3 4, which comes from p=[4,3,2,1]p = [4, 3, 2, 1]p=[4,3,2,1]. The minimum of every segment is at its right end. The loop walks the entire segment to find it, peels off one element, then walks the entire rest again.\nT(n)=O(n)+T(n−1)=O(n2)T(n) = O(n) + T(n-1) = O(n^2)T(n)=O(n)+T(n−1)=O(n2)With n=5⋅105n = 5 \\cdot 10^5n=5⋅105 that\u0026rsquo;s around 1.25⋅10111.25 \\cdot 10^{11}1.25⋅1011 steps. Test 14 was almost certainly one of these.\nThe fix Check the candidates from both ends, alternating: l,r,l+1,r−1,l+2,…l, r, l+1, r-1, l+2, \\dotsl,r,l+1,r−1,l+2,…\nBoth ends (AC)\nfor (int step = 0; step \u0026lt; len; step++) { int i = (step % 2 == 0) ? l + step / 2 : r - step / 2; // same check as before } Left to right (TLE)\nfor (int i = l; i \u0026lt;= r; i++) { // same check as before } That\u0026rsquo;s it. The whole change.\nHere\u0026rsquo;s why it works. If the split lands at distance ddd from the nearer end, the alternating loop finds it in about 2d2d2d steps. And ddd is exactly the size of the smaller piece. So a split costs time proportional to the smaller side, never the bigger one:\nT(n)=O(min⁡(k,n−k))+T(k)+T(n−k−1)T(n) = O(\\min(k, n-k)) + T(k) + T(n - k - 1)T(n)=O(min(k,n−k))+T(k)+T(n−k−1)That recurrence is O(nlog⁡n)O(n \\log n)O(nlogn), and the proof is a one-liner I really like. Charge the cost of each split to the elements of the smaller piece, one unit each. An element only gets charged when it lands in the smaller half, and each time that happens the segment it lives in at least halves in size. You can\u0026rsquo;t halve nnn more than log⁡2n\\log_2 nlog2​n times. So each element pays at most log⁡n\\log nlogn, and the total is O(nlog⁡n)O(n \\log n)O(nlogn).\nThe skewed case that killed the first version now costs O(1)O(1)O(1) per level, since the split is at the right end and the loop checks rrr second. A perfectly balanced split costs O(n)O(n)O(n) per level across log⁡n\\log nlogn levels. And everything in between is covered by the charging argument.\nIf this feels familiar, it should. It\u0026rsquo;s the same reasoning as small-to-large merging (DSU on tree), just applied to searching instead of merging.\nNumbers Same code, compiled with -O2, input a = 1 2 ... 500000:\nScan order Time Left to right over 20 s (I killed it) Both ends, alternating 0.1 s Not a constant factor. A different complexity class.\nThe full solution This is a cleaned-up version of what I submitted, with my template and the segment-tree pruning stripped out since the complexity doesn\u0026rsquo;t need either. Run it on the samples below.\n#include \u0026lt;bits/stdc++.h\u0026gt; using namespace std; using ll = long long; const ll MOD = 1e9 + 7; vector\u0026lt;ll\u0026gt; pre, fact, inv; ll power(ll b, ll e) { ll r = 1; b %= MOD; for (; e; e \u0026gt;\u0026gt;= 1, b = b * b % MOD) if (e \u0026amp; 1) r = r * b % MOD; return r; } ll C(int n, int k) { return fact[n] * inv[k] % MOD * inv[n - k] % MOD; } ll tri(ll k) { return k * (k + 1) / 2; } ll sum(int l, int r) { return pre[r + 1] - pre[l]; } // Number of valid permutations for segment [l, r], 0 if none. ll solve(int l, int r) { if (l \u0026gt; r) return 1; int len = r - l + 1; for (int step = 0; step \u0026lt; len; step++) { int i = (step % 2 == 0) ? l + step / 2 : r - step / 2; if (sum(l, i - 1) == tri(i - l) \u0026amp;\u0026amp; sum(i + 1, r) == tri(r - i)) { ll L = solve(l, i - 1); if (!L) return 0; ll R = solve(i + 1, r); return C(r - l, r - i) * L % MOD * R % MOD; } } return 0; } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); const int MX = 500001; fact.assign(MX, 1); inv.assign(MX, 1); for (int i = 1; i \u0026lt; MX; i++) fact[i] = fact[i - 1] * i % MOD; inv[MX - 1] = power(fact[MX - 1], MOD - 2); for (int i = MX - 1; i \u0026gt; 0; i--) inv[i - 1] = inv[i] * i % MOD; int t; cin \u0026gt;\u0026gt; t; while (t--) { int n; cin \u0026gt;\u0026gt; n; pre.assign(n + 1, 0); for (int i = 0; i \u0026lt; n; i++) { ll x; cin \u0026gt;\u0026gt; x; pre[i + 1] = pre[i] + x; } if (pre[n] != tri(n)) { cout \u0026lt;\u0026lt; 0 \u0026lt;\u0026lt; \u0026#34;\\n\u0026#34;; continue; } cout \u0026lt;\u0026lt; solve(0, n - 1) \u0026lt;\u0026lt; \u0026#34;\\n\u0026#34;; } } Test cases sample\nInput:\n4 3 1 4 1 4 1 2 3 4 4 1 6 1 2 3 3 3 3 Expected:\n2 1 3 0 single element\nInput:\n1 1 1 Expected:\n1 balanced tree\nInput:\n1 3 1 4 1 Expected:\n2 skewed, n = 100000\ninput file · expected output\nStack depth The recursion can go nnn levels deep on a skewed input. Codeforces gives you a big stack, so it\u0026rsquo;s fine there. On your own machine you may need ulimit -s unlimited before testing a worst case. The last test case is the skewed worst case at n=100000n = 100000n=100000. It\u0026rsquo;s too big to show inline, so expand it for a preview, or grab everything with Download all.\nWhere else this shows up Any time you recurse by splitting a range at a point you have to search for, ask one question: does finding the split cost the size of the whole range, or only the smaller side? If it\u0026rsquo;s the whole range, a skewed input will get you. Searching from both ends is the cheapest way to make it the smaller side.\nIt\u0026rsquo;s the same pattern behind \u0026ldquo;find the unique element, split around it\u0026rdquo; problems, where people usually reach for a sparse table or a stack. Sometimes you don\u0026rsquo;t need either. You just need to stop scanning from the left.\nNext time you get a TLE on a divide and conquer you know is right, before you rewrite anything, check which way your loop is walking.\n","permalink":"https://priyanshudebnath.me/posts/scan-from-both-ends/","summary":"My solution to Vlad, Misha and Two Arrays was correct and still got TLE on test 14. The fix was to check the split candidates in the order l, r, l+1, r-1, \u0026hellip; instead of left to right. Here is why that one change turns O(n²) into O(n log n).","title":"Scan From Both Ends: How One Line Took My Divide and Conquer From TLE to AC"},{"content":"I was solving Codeforces 2038L — Bridge Renovation, got a clean closed-form answer out of it, and then asked myself the obvious follow-up: what if the constraints hadn\u0026rsquo;t been so generous? That question is what turned into this post — the formal bin-packing problem, exactly why it\u0026rsquo;s NP-hard (and the sharper, less commonly explained fact that it\u0026rsquo;s strongly NP-hard), every classical approximation algorithm with a proof of its worst-case bound (not just a citation of one), the handful of special cases where the problem quietly becomes polynomial, and the backtracking/bitmask techniques you\u0026rsquo;d reach for if a contest handed you a small instance and expected an exact answer.\nA concrete starting point: Codeforces 2038L — Bridge Renovation The problem. Monocarp needs to renovate three bridges of widths 18, 21, and 25 units — he needs nnn planks of length 18, nnn planks of length 21, and nnn planks of length 25 (the same nnn for all three). Planks are only sold in a standard length of 60, workers can cut a plank into pieces but refuse to join two planks together, and offcuts are just waste. Given 1≤n≤10001 \\le n \\le 10001≤n≤1000, find the minimum number of standard planks. For n=1n=1n=1: you can\u0026rsquo;t fit all three required lengths in one plank (18+21+25=64\u0026gt;6018+21+25=64\u0026gt;6018+21+25=64\u0026gt;60), so the answer is 2 — one plank cut into 25 and 18 (17 wasted), the other into 21 (39 wasted).\nSolving it. First, enumerate every way to cut a single 60-length plank into pieces of length 18, 21, and 25 — there are only 13 such patterns, since the numbers are so constrained (at most three 18s, two 21s, or two 25s fit in one plank at all). Sorted by how much of the plank they use:\nPattern (18s, 21s, 25s) Length used Waste (1,2,0)(1, 2, 0)(1,2,0) — 18+21+2118+21+2118+21+21 60 0 (2,1,0)(2, 1, 0)(2,1,0) — 18+18+2118+18+2118+18+21 57 3 (3,0,0)(3, 0, 0)(3,0,0) — 18+18+1818+18+1818+18+18 54 6 (0,0,2)(0, 0, 2)(0,0,2) — 25+2525+2525+25 50 10 (0,1,1)(0, 1, 1)(0,1,1) — 21+2521+2521+25 46 14 (1,0,1)(1, 0, 1)(1,0,1) — 18+2518+2518+25 43 17 …and 7 more, all strictly worse Three patterns do all the real work: (1,2,0)(1,2,0)(1,2,0) wastes nothing, (3,0,0)(3,0,0)(3,0,0) is the most efficient pure source of 18s, and (0,0,2)(0,0,2)(0,0,2) is the only way to get 25s without dragging in an 18 or 21 you don\u0026rsquo;t need yet. The construction: cut ⌊n/2⌋\\lfloor n/2 \\rfloor⌊n/2⌋ planks as (1,2,0)(1,2,0)(1,2,0) — this exactly satisfies the 21-requirement (or comes up one short, if nnn is odd) and produces ⌊n/2⌋\\lfloor n/2 \\rfloor⌊n/2⌋ of the needed 18s, leaving ⌈n/2⌉\\lceil n/2 \\rceil⌈n/2⌉ still to get. Cut ⌈n/2⌉\\lceil n/2 \\rceil⌈n/2⌉ planks as (0,0,2)(0,0,2)(0,0,2) to cover the 25s — that\u0026rsquo;s already ⌊n/2⌋+⌈n/2⌉=n\\lfloor n/2\\rfloor + \\lceil n/2\\rceil = n⌊n/2⌋+⌈n/2⌉=n planks total before touching the leftover 18s at all. Cover the remaining ⌈n/2⌉\\lceil n/2 \\rceil⌈n/2⌉ eighteens with (3,0,0)(3,0,0)(3,0,0) planks (occasionally swapping one for a (2,1,0)(2,1,0)(2,1,0) plank when nnn is odd, to pick up that last 21 for free instead of using an extra plank) — that\u0026rsquo;s ⌈⌈n/2⌉/3⌉\\left\\lceil \\lceil n/2 \\rceil / 3 \\right\\rceil⌈⌈n/2⌉/3⌉ more planks, which is exactly ⌈n/6⌉\\lceil n/6 \\rceil⌈n/6⌉ by the standard nested-ceiling identity ⌈⌈n/a⌉/b⌉=⌈n/(ab)⌉\\lceil \\lceil n/a \\rceil / b \\rceil = \\lceil n/(ab) \\rceil⌈⌈n/a⌉/b⌉=⌈n/(ab)⌉. Total:\nplanks(n)=n+⌈n6⌉\\text{planks}(n) = n + \\left\\lceil \\frac{n}{6} \\right\\rceilplanks(n)=n+⌈6n​⌉I checked this construction by brute force against every nnn from 0 to 25 (exhaustive BFS over the true state space, no shortcuts) before trusting it — it matched exactly every time.\nProving you can\u0026rsquo;t do better. A construction only tells you an upper bound; proving n+⌈n/6⌉n+\\lceil n/6\\rceiln+⌈n/6⌉ is optimal needs a matching lower bound, and \u0026ldquo;total wood needed divided by plank length\u0026rdquo; isn\u0026rsquo;t tight here (⌈64n/60⌉≈1.067n\\lceil 64n/60 \\rceil \\approx 1.067n⌈64n/60⌉≈1.067n, well under the true ≈1.167n\\approx 1.167n≈1.167n) — the pieces don\u0026rsquo;t divide evenly, so volume alone underestimates the real cost. The right tool is a weighting argument (this is LP duality, done by hand): assign a weight to each unit you need — y18=1/3y_{18} = 1/3y18​=1/3, y21=1/3y_{21} = 1/3y21​=1/3, y25=1/2y_{25} = 1/2y25​=1/2 — chosen so that every one of the 13 feasible patterns (a,b,c)(a,b,c)(a,b,c) satisfies a⋅y18+b⋅y21+c⋅y25≤1a\\cdot y_{18} + b\\cdot y_{21} + c\\cdot y_{25} \\le 1a⋅y18​+b⋅y21​+c⋅y25​≤1. Check the three patterns doing the real work: (1,2,0)→13+23=1(1,2,0) \\to \\frac13 + \\frac23 = 1(1,2,0)→31​+32​=1, (3,0,0)→1(3,0,0) \\to 1(3,0,0)→1, (0,0,2)→1(0,0,2) \\to 1(0,0,2)→1 — all tight, by design. Check the rest and every single one comes in at ≤1\\le 1≤1 too (I verified all 13 by hand; the worst of the \u0026ldquo;off\u0026rdquo; patterns, (0,1,1)(0,1,1)(0,1,1), gives 13+12=56\\frac13+\\frac12 = \\frac5631​+21​=65​).\nNow take any valid solution using PPP planks, whatever patterns it uses. Since every pattern satisfies that inequality, summing it over all PPP planks gives 13(total 18s)+13(total 21s)+12(total 25s)≤P\\frac{1}{3}(\\text{total 18s}) + \\frac{1}{3}(\\text{total 21s}) + \\frac{1}{2}(\\text{total 25s}) \\le P31​(total 18s)+31​(total 21s)+21​(total 25s)≤P. The solution must produce at least nnn of each length, so the left side is at least n3+n3+n2=7n6\\frac{n}{3}+\\frac{n}{3}+\\frac{n}{2} = \\frac{7n}{6}3n​+3n​+2n​=67n​. Therefore P≥7n6P \\ge \\frac{7n}{6}P≥67n​, and since PPP is an integer, P≥⌈7n/6⌉P \\ge \\lceil 7n/6 \\rceilP≥⌈7n/6⌉. One more identity — ⌈7n/6⌉=⌈n+n/6⌉=n+⌈n/6⌉\\lceil 7n/6 \\rceil = \\lceil n + n/6 \\rceil = n + \\lceil n/6 \\rceil⌈7n/6⌉=⌈n+n/6⌉=n+⌈n/6⌉ since nnn is already an integer — and this lower bound exactly matches the construction above. ■\\blacksquare■\nThe \u0026ldquo;what if\u0026rdquo; that leads to this post. That whole solution leaned on the constraints being generous in a very specific way: only three fixed, small item lengths, a fixed bin size, and both chosen so that a handful of hand-enumerable cutting patterns dominate everything else. That\u0026rsquo;s what made \u0026ldquo;enumerate 13 patterns and solve a tiny weighted covering problem\u0026rdquo; work at all. Now strip those generosities away — arbitrary item sizes instead of three fixed constants, arbitrarily many distinct sizes instead of three, no clean divisibility relationship to bin capacity — and the number of \u0026ldquo;patterns\u0026rdquo; explodes combinatorially, the weighting trick stops finding a clean closed form, and you\u0026rsquo;re standing at the edge of the general bin-packing problem. That\u0026rsquo;s where the rest of this post picks up.\nThe problem, stated plainly You\u0026rsquo;re given nnn items with positive sizes s1,s2,…,sns_1, s_2, \\dots, s_ns1​,s2​,…,sn​ and bins of uniform capacity CCC. Every item must go into exactly one bin, and no bin\u0026rsquo;s contents may sum past CCC. The optimization version asks for the minimum number of bins. The decision version asks a yes/no question instead: can everything fit into at most kkk bins? That distinction matters more than it looks like it should — the complexity argument in the next section is built entirely on the decision version, and then extended to the optimization version afterward.\nTwo numbers come up constantly, so name them now: S=∑isiS = \\sum_i s_iS=∑i​si​ is the total size of everything, and ⌈S/C⌉\\lceil S / C \\rceil⌈S/C⌉ is the obvious lower bound on the number of bins — you can never do better than \u0026ldquo;total volume divided by bin volume,\u0026rdquo; even though you can absolutely do worse.\nWhy it\u0026rsquo;s hard: a full pass through the complexity argument I\u0026rsquo;m not going to assume you remember the exact definitions here, because half the value of \u0026ldquo;explaining bin packing\u0026rsquo;s complexity\u0026rdquo; is being precise about what these words mean rather than gesturing at them.\nP is the class of decision problems solvable in time polynomial in the input size. NP is the class of decision problems where a proposed \u0026ldquo;yes\u0026rdquo; answer can be verified in polynomial time, even if finding that answer might take much longer — for bin packing\u0026rsquo;s decision version, the certificate is just the assignment of items to bins, and checking it means summing each bin once, which is O(n)O(n)O(n).\nNP-hard means every problem in NP reduces to it in polynomial time — informally, it\u0026rsquo;s \u0026ldquo;at least as hard as anything in NP,\u0026rdquo; but it doesn\u0026rsquo;t have to be in NP itself (it might not even be a decision problem). NP-complete is the intersection: NP-hard and in NP. Bin packing\u0026rsquo;s decision version is a textbook NP-complete problem. Its optimization version — \u0026ldquo;find the minimum kkk,\u0026rdquo; not just \u0026ldquo;is kkk enough\u0026rdquo; — is NP-hard but isn\u0026rsquo;t itself a yes/no question, so \u0026ldquo;NP-complete\u0026rdquo; doesn\u0026rsquo;t technically apply to it; showing the decision version is NP-complete is what makes the optimization version NP-hard (an oracle that solves the optimization version in poly time would let you binary-search kkk and solve the decision version in poly time too).\nThe reduction: PARTITION becomes a 2-bin question The standard proof reduces from PARTITION: given a multiset of positive integers A={a1,…,am}A = \\{a_1, \\dots, a_m\\}A={a1​,…,am​} with total sum SSS, can AAA be split into two subsets with equal sum? PARTITION is a classical NP-complete problem (it\u0026rsquo;s weakly NP-complete, a distinction that turns out to matter — more on that in a moment).\nThe reduction is almost embarrassingly direct: take the same multiset as your bin-packing items, set the bin capacity to C=S/2C = S/2C=S/2, and ask whether two bins suffice. If SSS is odd, no partition can exist and the answer is trivially \u0026ldquo;no\u0026rdquo; either way. If SSS is even, then packing everything into two bins of capacity S/2S/2S/2 is only possible if each bin\u0026rsquo;s contents sum to exactly S/2S/2S/2 — any other split would push one bin over capacity, since the two bins together must account for all of SSS. So PARTITION on AAA is solvable exactly when bin packing on AAA fits into 2 bins of capacity S/2S/2S/2:\nPARTITION(A) ⟺ BinPack(A, k=2, C=S/2)\\text{PARTITION}(A) \\iff \\text{BinPack}(A,\\, k{=}2,\\, C{=}S/2)PARTITION(A)⟺BinPack(A,k=2,C=S/2)That\u0026rsquo;s a polynomial-time reduction (it\u0026rsquo;s literally the identity on the input, plus one division), which means 2-bin decision packing is at least as hard as PARTITION. Since the certificate-checking argument above put it in NP, and this reduction puts it in NP-hard, bin packing\u0026rsquo;s decision version is NP-complete. ■\\blacksquare■\nThe sharper fact: bin packing is strongly NP-hard Here\u0026rsquo;s the detail most casual treatments skip. PARTITION is only weakly NP-hard — it has a pseudo-polynomial time algorithm (the classic subset-sum DP, O(nS)O(nS)O(nS)), so if your numbers are small (polynomially bounded in nnn), PARTITION is easy in practice. The PARTITION reduction above only shows bin packing is weakly NP-hard too, for the specific case of k=2k=2k=2.\nThe general bin-packing optimization problem is hard in a strictly stronger sense: it\u0026rsquo;s strongly NP-hard, meaning no algorithm can solve it in time polynomial in nnn and max⁡isi\\max_i s_imaxi​si​ — not even if every item size is small. No pseudo-polynomial algorithm exists unless P = NP. This is proven by reducing from 3-PARTITION instead: given 3m3m3m positive integers with total sum mTmTmT, where every single integer is constrained to lie strictly between T/4T/4T/4 and T/2T/2T/2, decide whether they can be grouped into mmm triples each summing to exactly TTT. That size constraint is the whole trick — it forces exactly three elements into every group in any valid solution (two elements can sum to at most just under TTT, and four elements sum to more than TTT at minimum), which is what makes 3-PARTITION so much more rigid, and so much harder, than PARTITION. Garey and Johnson proved 3-PARTITION strongly NP-complete in Computers and Intractability (1979), and it remains the standard hammer for proving strong NP-hardness across bin packing, scheduling, and cutting-stock problems to this day.\nReducing to bin packing is the same trick as before — items are the 3m3m3m integers, capacity is TTT, ask if mmm bins suffice — except now the size restriction guarantees a feasible packing must use exactly three items per bin, which is exactly the structure 3-PARTITION needs. Because the reduction keeps the numbers polynomially bounded, this shows bin packing itself is strongly NP-hard, not just weakly so.\nWhy should you care about weak versus strong? Because it tells you where hope is allowed to live. A weakly NP-hard problem might have a fast exact algorithm hiding behind small numbers — subset sum, knapsack, PARTITION. Bin packing doesn\u0026rsquo;t get that escape hatch. The exponential blowup is fundamental to the structure of the problem, not an artifact of large item sizes.\nExact algorithms For small instances, or as a subroutine inside something bigger, you sometimes want the actual optimum rather than a bound on how far off you might be.\nBrute force — try every assignment of items to bins. Astronomically slow (O(kn)O(k^n)O(kn)-ish), useful only as a correctness oracle for testing faster methods. Bitmask DP — see the competitive-programming section below; this is the one you\u0026rsquo;d actually reach for at n≤20n \\le 20n≤20. ILP formulation — binary variables xij=1x_{ij} = 1xij​=1 if item iii goes in bin jjj, and yj=1y_j = 1yj​=1 if bin jjj is used at all; minimize ∑jyj\\sum_j y_j∑j​yj​ subject to ∑jxij=1\\sum_j x_{ij} = 1∑j​xij​=1 for every item and ∑isixij≤C⋅yj\\sum_i s_i x_{ij} \\le C \\cdot y_j∑i​si​xij​≤C⋅yj​ for every bin. Solvable with any off-the-shelf ILP solver for moderate sizes, though symmetry between interchangeable bins tends to slow branch-and-bound down badly unless you add symmetry-breaking constraints. Branch and bound — Martello and Toth\u0026rsquo;s MTP algorithm and Korf\u0026rsquo;s bin-completion algorithm are the two names that come up repeatedly in the literature; both exploit strong lower bounds and dominance rules to prune aggressively, and both can solve instances with hundreds of items in practice, far beyond what brute force or bitmask DP could touch. The cutting-stock connection — bin packing is the cutting-stock problem (minimizing the number of raw material rolls cut into ordered piece lengths), and the Gilmore–Gomory formulation solves its LP relaxation via column generation: instead of enumerating every possible \u0026ldquo;pattern\u0026rdquo; of items in a bin up front (there can be exponentially many), you generate promising patterns on demand by solving a knapsack subproblem. This is the actual industrial approach for large instances, and it routinely gets within a fraction of a bin of optimal. Approximation algorithms This is the heart of the problem in practice — nobody runs an ILP solver to pack VMs onto hosts every few seconds. Every algorithm below is greedy, runs in at most O(nlog⁡n)O(n \\log n)O(nlogn), and comes with a proven worst-case guarantee relative to OPT\\mathrm{OPT}OPT, the optimal bin count.\nNext Fit Keep exactly one bin \u0026ldquo;open.\u0026rdquo; If the current item fits, place it there; otherwise close that bin for good, open a fresh one, and place it there. Next Fit never looks back at an old bin, which is what makes it fast (genuinely O(n)O(n)O(n), no data structure needed) and also what makes it wasteful.\nNext Fit on [6, 5, 4, 3, 7, 2], capacity 10 — never reconsiders a closed bin Step Play Reset Watch bin 1 in that trace: it closes with 4 units of slack the instant item 3 doesn\u0026rsquo;t fit, and that slack is gone forever — Next Fit will never place anything there again, even though a later small item might have fit perfectly. Run First Fit on the exact same six items and it packs them into 3 bins instead of Next Fit\u0026rsquo;s 4, precisely because it\u0026rsquo;s willing to look back.\nClaim: NF(I)≤2⋅OPT(I)\\mathrm{NF}(I) \\le 2 \\cdot \\mathrm{OPT}(I)NF(I)≤2⋅OPT(I).\nProof. Let bins 1,2,…,k1, 2, \\dots, k1,2,…,k be the bins Next Fit uses, in the order it opens them. Take any two consecutive bins iii and i+1i+1i+1. Next Fit only opens bin i+1i+1i+1 because the item it was about to place didn\u0026rsquo;t fit in bin iii — meaning at that moment, fill(i)+fill(i+1)\u0026gt;C\\mathrm{fill}(i) + \\mathrm{fill}(i+1) \u0026gt; Cfill(i)+fill(i+1)\u0026gt;C (the item\u0026rsquo;s size plus whatever was already in bin iii exceeds CCC, and that item is now sitting in bin i+1i+1i+1). Since bin iii never receives anything after bin i+1i+1i+1 opens — Next Fit only ever adds to the current last bin — that inequality holds for the final packing too, not just at that instant.\nPair up the bins: (1,2),(3,4),…(1,2), (3,4), \\dots(1,2),(3,4),…. Each pair sums to more than CCC, so ⌊k/2⌋\\lfloor k/2 \\rfloor⌊k/2⌋ pairs contribute more than ⌊k/2⌋⋅C\\lfloor k/2 \\rfloor \\cdot C⌊k/2⌋⋅C in total item size. That total is at most S=∑isi≤OPT(I)⋅CS = \\sum_i s_i \\le \\mathrm{OPT}(I) \\cdot CS=∑i​si​≤OPT(I)⋅C (since OPT(I)\\mathrm{OPT}(I)OPT(I) bins suffice to hold everything). So ⌊k/2⌋⋅C\u0026lt;OPT(I)⋅C\\lfloor k/2 \\rfloor \\cdot C \u0026lt; \\mathrm{OPT}(I) \\cdot C⌊k/2⌋⋅C\u0026lt;OPT(I)⋅C, giving ⌊k/2⌋\u0026lt;OPT(I)\\lfloor k/2 \\rfloor \u0026lt; \\mathrm{OPT}(I)⌊k/2⌋\u0026lt;OPT(I), i.e. k\u0026lt;2 OPT(I)+1k \u0026lt; 2\\,\\mathrm{OPT}(I) + 1k\u0026lt;2OPT(I)+1. Since kkk and OPT(I)\\mathrm{OPT}(I)OPT(I) are both integers, k≤2 OPT(I)k \\le 2\\,\\mathrm{OPT}(I)k≤2OPT(I). ■\\blacksquare■\nThis bound is also tight — there are instances where Next Fit genuinely needs arbitrarily close to twice the optimal bin count (alternate items of size just over C/2C/2C/2 with items of size just under C/2C/2C/2; Next Fit is forced to open a fresh bin for every single item, while an optimal packing pairs them up two per bin), so 2⋅OPT(I)2 \\cdot \\mathrm{OPT}(I)2⋅OPT(I) isn\u0026rsquo;t a loose worst-case artifact of this particular proof — it\u0026rsquo;s the actual answer.\nFirst Fit and Best Fit First Fit scans every open bin in order and places the item in the first one it fits. Best Fit scans every open bin and places the item in whichever one leaves the least leftover space (breaking ties however you like). Both need a way to query \u0026ldquo;which bins fit this item\u0026rdquo; efficiently — a balanced BST keyed by remaining capacity gets both down to O(nlog⁡n)O(n \\log n)O(nlogn).\nFirst Fit on [4, 8, 1, 4, 2, 1, 7, 3], capacity 10 Step Play Reset Best Fit on [2, 5, 4, 7, 1, 3], capacity 10 Step Play Reset That Best Fit example is worth sitting with for a second. Run First Fit on the identical six items and it also finishes in 3 bins — but with 2, 3, and 3 units of slack scattered across them. Best Fit finishes with 2, 6, and 0 — it drives one bin to exactly full. Same bin count, different local decisions, because Best Fit\u0026rsquo;s criterion (\u0026ldquo;leave as little room as possible\u0026rdquo;) concentrates leftover space instead of spreading it thin. That\u0026rsquo;s a genuinely different greedy policy, even though the two algorithms are asymptotically just as good as each other in the worst case.\nClaim: FF(I)≤2⋅OPT(I)\\mathrm{FF}(I) \\le 2 \\cdot \\mathrm{OPT}(I)FF(I)≤2⋅OPT(I) (and the identical proof works for Best Fit — see below).\nProof. Let bins 1,…,k1, \\dots, k1,…,k be First Fit\u0026rsquo;s bins, in the order opened. Take any two bins i\u0026lt;ji \u0026lt; ji\u0026lt;j. Bin jjj was opened at some moment because the item about to be placed — call it xxx — didn\u0026rsquo;t fit in any of bins 1,…,j−11, \\dots, j-11,…,j−1, in particular not in bin iii. That means, at that moment, fill(i)\u0026gt;C−size(x)\\mathrm{fill}(i) \u0026gt; C - \\mathrm{size}(x)fill(i)\u0026gt;C−size(x). Bin fills only grow over time, so this still holds for the final packing: fill(i)\u0026gt;C−size(x)\\mathrm{fill}(i) \u0026gt; C - \\mathrm{size}(x)fill(i)\u0026gt;C−size(x). And xxx itself ended up in bin jjj, so fill(j)≥size(x)\\mathrm{fill}(j) \\ge \\mathrm{size}(x)fill(j)≥size(x). Adding these: fill(i)+fill(j)\u0026gt;C\\mathrm{fill}(i) + \\mathrm{fill}(j) \u0026gt; Cfill(i)+fill(j)\u0026gt;C.\nThis is the same pairing argument as Next Fit\u0026rsquo;s proof above — every earlier bin and every later bin sum to more than CCC — so it concludes identically: k≤2 OPT(I)k \\le 2\\,\\mathrm{OPT}(I)k≤2OPT(I). ■\\blacksquare■\nBest Fit satisfies the same bound, by the same proof, because Best Fit only opens a new bin under the identical condition — the item doesn\u0026rsquo;t fit any currently open bin — so the argument \u0026ldquo;bin iii must have had insufficient room for xxx at the moment bin jjj opened\u0026rdquo; holds unchanged.\nThat\u0026rsquo;s the complete, elementary proof of the classical factor-of-2 bound, and it\u0026rsquo;s genuinely all you need for most purposes. The tighter 1.7⋅OPT(I)1.7 \\cdot \\mathrm{OPT}(I)1.7⋅OPT(I) bound Johnson proved in 1973 is real and correct, but its proof is a different animal — it assigns each item a carefully calibrated weight (roughly, w(s)≈s/Cw(s) \\approx s/Cw(s)≈s/C but adjusted upward for items just over C/6C/6C/6, C/3C/3C/3, and similar thresholds, tuned so that no First-Fit bin can ever hold more than 1.71.71.7 units of total weight) and bounds the number of bins by the total weight instead of the total size directly. It\u0026rsquo;s correct and it\u0026rsquo;s the tight bound, but reproducing the full case analysis honestly takes several pages, not a paragraph — Johnson\u0026rsquo;s original 1973 MIT thesis runs over 100 pages precisely because of proofs like this one. I\u0026rsquo;m citing it rather than re-deriving it here, and the 2× proof above is offered as the complete, self-contained version rather than a simplification that quietly loses rigor.\nFirst Fit Decreasing (and Best Fit Decreasing) Sort the items largest-first, then run First Fit. That\u0026rsquo;s the entire algorithm, and it\u0026rsquo;s the one I\u0026rsquo;d reach for by default if you told me \u0026ldquo;just pack these well and don\u0026rsquo;t overthink it.\u0026rdquo;\nFirst Fit Decreasing on [3, 7, 2, 8, 5, 4, 1, 6], capacity 10 — sorted internally to [8,7,6,5,4,3,2,1] before packing Step Play Reset That example packs eight items summing to 36 into exactly 4 bins — which is optimal, since ⌈36/10⌉=4\\lceil 36/10 \\rceil = 4⌈36/10⌉=4, and three of the four bins finish completely full.\nFFD is First Fit run on one particular, carefully-chosen input order — so the FF(I)≤2⋅OPT(I)\\mathrm{FF}(I) \\le 2\\cdot\\mathrm{OPT}(I)FF(I)≤2⋅OPT(I) proof above applies to it for free, with zero extra work: FFD(I)≤2⋅OPT(I)\\mathrm{FFD}(I) \\le 2\\cdot\\mathrm{OPT}(I)FFD(I)≤2⋅OPT(I) is already proven. Sorting first buys something much stronger than that, though: Johnson\u0026rsquo;s 1973 paper established the asymptotic ratio 11/9≈1.22211/9 \\approx 1.22211/9≈1.222 — a real improvement over the bare factor of 2, and the proof technique is the same weight-function idea sketched above, just calibrated further using the fact that the input is sorted (which rules out certain bad interleavings the unsorted proof has to defend against). It took until 2007 for Dósa to show that ratio is tight, and until 2013 for a complete published proof of the exact additive constant, FFD(I)≤119OPT(I)+69\\mathrm{FFD}(I) \\le \\frac{11}{9}\\mathrm{OPT}(I) + \\frac{6}{9}FFD(I)≤911​OPT(I)+96​ — that\u0026rsquo;s four decades between \u0026ldquo;here\u0026rsquo;s a good bound\u0026rdquo; and \u0026ldquo;here\u0026rsquo;s the provably best-possible constant,\u0026rdquo; which says something honest about how much harder pinning down the exact worst case is compared to a serviceable one. Best Fit Decreasing shares the identical 11/911/911/9 asymptotic bound. In practice, on anything resembling a realistic size distribution, FFD tends to land within a bin or two of optimal — the 11/911/911/9 figure is a worst-case guarantee, not a typical outcome.\nBeyond simple greedy: Karmarkar–Karp None of the algorithms above get arbitrarily close to optimal — their guarantees are constant multiplicative factors. Karmarkar and Karp\u0026rsquo;s 1982 algorithm does dramatically better: it solves the LP relaxation of the Gilmore–Gomory cutting-stock formulation and rounds the fractional solution, achieving\nKK(I)≤OPT(I)+O(log⁡2OPT(I))\\mathrm{KK}(I) \\le \\mathrm{OPT}(I) + O(\\log^2 \\mathrm{OPT}(I))KK(I)≤OPT(I)+O(log2OPT(I))in polynomial time — an additive gap that grows only polylogarithmically, not a multiplicative one. This is what\u0026rsquo;s called an asymptotic fully polynomial-time approximation scheme (AFPTAS); a simpler asymptotic PTAS (without the polylog refinement) was given earlier by de la Vega and Lueker in 1981. It\u0026rsquo;s the closest thing to \u0026ldquo;solved\u0026rdquo; that a strongly NP-hard problem is ever going to get.\nOnline algorithms: the same problem, but you can\u0026rsquo;t look ahead Everything above assumes you see the whole item list up front. In the online setting, items arrive one at a time and you must place each irrevocably before seeing the next — this is the realistic model for a VM scheduler accepting requests as they come in, and it\u0026rsquo;s a meaningfully harder setting: you can\u0026rsquo;t sort, and you can\u0026rsquo;t undo a bad early decision.\nResult Bound Source Any online algorithm competitive ratio ≥1.54278\\ge 1.54278≥1.54278 Balogh, Békési, Dósa, Epstein, Levin, 2021 Best known online algorithm (Advanced Harmonic) competitive ratio ≤1.57829\\le 1.57829≤1.57829 Balogh et al., 2018 Classical Harmonic algorithm competitive ratio →1.691…\\to 1.691\\ldots→1.691… Lee \u0026amp; Lee, 1985 That gap between 1.54278 and 1.57829 is still open — nobody knows the exact optimal online competitive ratio, which says something about how genuinely subtle the online version is even after four decades of people chipping away at both bounds.\nSpecial and common cases \u0026ldquo;NP-hard\u0026rdquo; describes the general problem. Plenty of instances you\u0026rsquo;ll actually encounter are much easier, and it\u0026rsquo;s worth knowing exactly which structural properties buy you that.\nEqual-size items. If every item has identical size sss, the problem stops being combinatorial entirely: each bin holds exactly ⌊C/s⌋\\lfloor C/s \\rfloor⌊C/s⌋ items, so the answer is simply ⌈n⌊C/s⌋⌉\\left\\lceil \\dfrac{n}{\\lfloor C/s \\rfloor} \\right\\rceil⌈⌊C/s⌋n​⌉. Proof: no bin can ever hold more than ⌊C/s⌋\\lfloor C/s \\rfloor⌊C/s⌋ items regardless of which items go where (that\u0026rsquo;s just s⋅⌊C/s⌋≤C\u0026lt;s⋅(⌊C/s⌋+1)s \\cdot \\lfloor C/s \\rfloor \\le C \u0026lt; s\\cdot(\\lfloor C/s\\rfloor+1)s⋅⌊C/s⌋≤C\u0026lt;s⋅(⌊C/s⌋+1)), and any bin count up to that per-bin maximum is trivially achievable since the items are interchangeable — so the bin count is forced to be exactly ⌈n/⌊C/s⌋⌉\\lceil n / \\lfloor C/s \\rfloor \\rceil⌈n/⌊C/s⌋⌉, both as a lower bound (packing fewer bins can\u0026rsquo;t hold nnn items at ⌊C/s⌋\\lfloor C/s\\rfloor⌊C/s⌋ each) and as an upper bound (fill every bin to that limit, last one however many remain). A closed-form expression, computable in O(1)O(1)O(1) after one division. The entire hardness of bin packing lives in the variety of item sizes, not in having many items. Equal bin sizes vs. variable bin sizes. Everything above assumes uniform capacity CCC. Variable-Sized Bin Packing (bins of several different capacities, choose freely) is a real generalization used in cloud placement (different instance-type sizes), and it inherits the same NP-hardness — but Murgolo (1987) gave an APTAS for it too, so asymptotically it\u0026rsquo;s no worse than the uniform case. Items of exactly half the bin capacity. If every item is exactly C/2C/2C/2, pack two per bin — again trivial. But a mix of near-C/2C/2C/2 items and small items is precisely the structure behind the classical adversarial constructions used to prove First Fit\u0026rsquo;s and Next Fit\u0026rsquo;s tight bounds: interleave items just over C/2C/2C/2 (which can never share a bin with each other) with items that would fit neatly into the slack, but present them in an order that keeps the algorithm from ever pairing them up. That\u0026rsquo;s not a special case that makes the problem easier — it\u0026rsquo;s the special case that makes the worst-case bounds tight. Everything fits in one bin. If ∑isi≤C\\sum_i s_i \\le C∑i​si​≤C, the answer is trivially 1. Always worth checking first; it costs one linear pass. Any single item exceeds CCC. Infeasible outright — no packing exists, and this needs to be checked before running anything else, since every algorithm above silently assumes si≤Cs_i \\le Csi​≤C for all iii. Items that evenly divide CCC. If every item size divides CCC, First Fit-style algorithms tend to reach zero waste, because there\u0026rsquo;s never a \u0026ldquo;left over but too small to matter\u0026rdquo; remainder — every bin either fills exactly or has room for another full-size chunk. The 3-PARTITION structure. Items strictly between C/4C/4C/4 and C/2C/2C/2, forced into groups of exactly three — this is the case that\u0026rsquo;s provably as hard as the general problem gets (strong NP-hardness), not a special case at all in the sense of being easier, but worth knowing by sight, because if your instance happens to have this shape, you should stop looking for a clever polynomial algorithm and reach for backtracking or an ILP solver instead. Competitive programming: exact algorithms for small instances Contest constraints are usually kind enough that you\u0026rsquo;re never actually asked to solve general bin packing — you\u0026rsquo;re asked to solve it for n≤20n \\le 20n≤20 or so, where exponential-but-small-base algorithms are completely fine.\nBitmask DP. Let dp[mask]\\mathrm{dp}[\\mathrm{mask}]dp[mask] be the minimum number of bins needed to pack exactly the item subset represented by mask. Precompute, for every subset, whether its total size fits in a single bin. Then\ndp[mask]=1+min⁡s⊆masksum(s)≤Cdp[mask∖s]\\mathrm{dp}[\\mathrm{mask}] = 1 + \\min_{\\substack{s \\subseteq \\mathrm{mask} \\\\ \\text{sum}(s) \\le C}} \\mathrm{dp}[\\mathrm{mask} \\setminus s]dp[mask]=1+s⊆masksum(s)≤C​min​dp[mask∖s]Naively enumerating submasks of every mask is O(3n)O(3^n)O(3n) (the standard \u0026ldquo;sum over subsets\u0026rdquo; bound), which is comfortably fast for n≤18n \\le 18n≤18–202020.\nCorrectness. By induction on ∣mask∣|\\mathrm{mask}|∣mask∣. Base case dp[∅]=0\\mathrm{dp}[\\emptyset]=0dp[∅]=0 is trivially correct — zero items need zero bins. For the inductive step, look at any optimal packing of the items in mask: it uses some number of bins, and in particular its last bin holds some subset s⊆masks \\subseteq \\mathrm{mask}s⊆mask with sum(s)≤C\\mathrm{sum}(s) \\le Csum(s)≤C (a valid pattern) — removing that bin leaves an optimal packing of mask∖s\\mathrm{mask} \\setminus smask∖s using one fewer bin (if it weren\u0026rsquo;t optimal for mask∖s\\mathrm{mask}\\setminus smask∖s, you could swap in a better one and improve the original packing too, contradiction). So dp[mask]≤1+dp[mask∖s]\\mathrm{dp}[\\mathrm{mask}] \\le 1 + \\mathrm{dp}[\\mathrm{mask}\\setminus s]dp[mask]≤1+dp[mask∖s] for that specific sss, and since the transition minimizes over every feasible sss, it\u0026rsquo;s at least as good — giving dp[mask]=1+min⁡sdp[mask∖s]\\mathrm{dp}[\\mathrm{mask}] = 1 + \\min_s \\mathrm{dp}[\\mathrm{mask}\\setminus s]dp[mask]=1+mins​dp[mask∖s] exactly. ■\\blacksquare■\nint dp[1 \u0026lt;\u0026lt; 20]; int fits[1 \u0026lt;\u0026lt; 20]; // 1 if this subset\u0026#39;s total size \u0026lt;= C int solve(int n) { int full = (1 \u0026lt;\u0026lt; n) - 1; fill(dp, dp + (1 \u0026lt;\u0026lt; n), INT_MAX); dp[0] = 0; for (int mask = 1; mask \u0026lt;= full; mask++) { for (int sub = mask; sub; sub = (sub - 1) \u0026amp; mask) { if (fits[sub] \u0026amp;\u0026amp; dp[mask ^ sub] != INT_MAX) { dp[mask] = min(dp[mask], dp[mask ^ sub] + 1); } } } return dp[full]; } DFS with backtracking and pruning. This is the approach that actually scales past what bitmask DP can reach, and it\u0026rsquo;s the one worth internalizing since the pruning is where all the real technique lives. Sort items largest-first (this alone helps prune earlier), then recursively try placing each item into every currently open bin plus one fresh bin. Two prunings matter enormously in practice:\nSymmetry breaking. All empty bins are interchangeable — trying an item in empty bin #3 after already trying it in empty bin #2 explores the same structural possibility twice. Never branch into more than one empty bin per item; this alone collapses a huge fraction of the redundant search tree. Lower-bound pruning. At any point in the search, if (bins used so far)+⌈remaining unplaced sizeC⌉≥(best solution found so far)(\\text{bins used so far}) + \\left\\lceil \\dfrac{\\text{remaining unplaced size}}{C} \\right\\rceil \\ge (\\text{best solution found so far})(bins used so far)+⌈Cremaining unplaced size​⌉≥(best solution found so far), abandon the branch immediately — it can\u0026rsquo;t possibly improve on what you already have. Neither pruning can ever discard the true optimum, which is the property that actually matters here. Symmetry breaking is safe because any solution that places an item in empty bin #3 while empty bin #2 stays unused is identical, bin-count-wise, to the solution with the labels of bins #2 and #3 swapped — so refusing to explore \u0026ldquo;item goes in the second empty bin\u0026rdquo; never loses a distinct solution, only a relabeled duplicate of one you\u0026rsquo;ll still reach. Lower-bound pruning is safe because ⌈(remaining size)/C⌉\\lceil (\\text{remaining size})/C \\rceil⌈(remaining size)/C⌉ is a valid lower bound on bins still needed no matter how the rest of the search plays out (it\u0026rsquo;s the same volume argument from the very first section) — so if current-bins-plus-that-bound already meets or exceeds your best answer so far, no completion of this branch can beat it, by definition of \u0026ldquo;lower bound.\u0026rdquo;\nint best; void dfs(int i, vector\u0026lt;int\u0026gt;\u0026amp; binRemaining, int itemsRemainingSum, int C) { if ((int)binRemaining.size() \u0026gt;= best) return; // already worse than best int lowerBound = binRemaining.size() + (itemsRemainingSum + C - 1) / C; if (lowerBound \u0026gt;= best) return; // can\u0026#39;t possibly win from here if (i == (int)items.size()) { best = min(best, (int)binRemaining.size()); return; } for (int b = 0; b \u0026lt; (int)binRemaining.size(); b++) { if (binRemaining[b] \u0026gt;= items[i]) { binRemaining[b] -= items[i]; dfs(i + 1, binRemaining, itemsRemainingSum - items[i], C); binRemaining[b] += items[i]; } if (binRemaining[b] == C) break; // symmetry break: only try ONE empty bin } // also try opening a brand new bin binRemaining.push_back(C - items[i]); dfs(i + 1, binRemaining, itemsRemainingSum - items[i], C); binRemaining.pop_back(); } For nnn up to roughly 30–40 — past what bitmask DP can hold in memory but still too small to trust a bound-and-prune search alone — meet in the middle is the other tool worth knowing: split the items into two halves, compute all achievable \u0026ldquo;bin configurations\u0026rdquo; for each half independently, then merge.\nA couple of real problems to actually implement this against:\nCodeforces Gym 102770B — Bin Packing Problem. The problem stated close to verbatim — good for getting First Fit correct as a baseline before you reach for the exact DFS version above. Codeforces 1066D — Boxes Packing. Not framed as \u0026ldquo;bin packing\u0026rdquo; in the title, but it\u0026rsquo;s a Next-Fit-style greedy packing simulation wrapped in a binary-search-on-the-answer outer loop — a good bridge between the online-algorithm theory above and an actual accepted submission. If you want to sanity-check your own bitmask DP or backtracking implementation, the cleanest self-test is to run it against brute force on small random instances (n≤10n \\le 10n≤10) and confirm the bin counts always agree — it\u0026rsquo;s a five-minute script and it\u0026rsquo;ll catch off-by-one errors in the pruning bound immediately.\nVariants, briefly The 1D case above is the classical version, but the same \u0026ldquo;pack items into bounded containers\u0026rdquo; shape generalizes in a few directions worth knowing exist even if this post doesn\u0026rsquo;t go deep on them:\nVector bin packing — items and bins are ddd-dimensional vectors instead of scalars (think CPU and memory and disk simultaneously), which is the actual model behind real cloud VM placement, not the 1D version. 2D and 3D geometric packing — rectangles or boxes instead of scalar sizes, with or without rotation allowed. Considerably harder to even approximate well than the 1D case. Bin covering — the dual problem: instead of minimizing bins while respecting a capacity, maximize the number of bins you can fill to at least some threshold. Same flavor of hardness, opposite objective. Applications and which algorithm to actually reach for Kubernetes\u0026rsquo; bin-packing scheduling strategy is, structurally, First Fit Decreasing on resource vectors — pack pods onto the fewest nodes, biggest resource requests first. Cutting stock in the paper and steel industries runs on Gilmore–Gomory column generation because the instances are large enough and the material cost high enough that a fraction-of-a-percent improvement over FFD is worth the engineering effort. Ad-slot scheduling and container loading both reduce to close variants of the same problem.\nFor anything you\u0026rsquo;re implementing yourself: First Fit Decreasing is the correct default. It\u0026rsquo;s a few lines of code, runs in O(nlog⁡n)O(n \\log n)O(nlogn), and the 11/911/911/9 worst-case bound means you\u0026rsquo;re never more than about 22% off optimal even in an adversarial instance — in practice it\u0026rsquo;s usually far closer than that. Reach for Best Fit instead only if you specifically care about concentrating leftover space (bin-consolidation heuristics sometimes want this). Reach for an online algorithm only when you genuinely can\u0026rsquo;t batch and sort — items truly do arrive one at a time and must be placed immediately. And reach for exact search — bitmask DP or the pruned DFS above — only when nnn is small enough that \u0026ldquo;provably optimal\u0026rdquo; is actually achievable in the time you have.\nSummary Bin packing: pack nnn items of given sizes into the fewest bins of capacity CCC. Decision version (does kkk bins suffice) is NP-complete; the optimization version is NP-hard. The 2-bin case reduces from PARTITION (weakly NP-hard). The general case reduces from 3-PARTITION and is strongly NP-hard — no pseudo-polynomial algorithm can exist unless P = NP. Next Fit: ratio 2. First Fit / Best Fit: ratio 1.7. First Fit Decreasing / Best Fit Decreasing: tight ratio 11/9⋅OPT+6/911/9 \\cdot \\mathrm{OPT} + 6/911/9⋅OPT+6/9. Karmarkar–Karp: OPT+O(log⁡2OPT)\\mathrm{OPT} + O(\\log^2 \\mathrm{OPT})OPT+O(log2OPT), essentially optimal. Online bin packing is provably harder: best known algorithm is 1.578291.578291.57829-competitive, best known lower bound is 1.542781.542781.54278 — the exact answer is still an open problem. Equal-size items, single-bin-suffices, and evenly-divisible-size instances all collapse to easy special cases; the 3-PARTITION structure is exactly the case that stays maximally hard. For small nnn, bitmask DP (O(3n)O(3^n)O(3n)) or a properly pruned DFS (symmetry-broken, lower-bound-pruned) gets you the exact optimum — that pruning is the actual skill, not the recursion itself. Resources — where to go deeper Problems to actually solve\nCodeforces 2038L — Bridge Renovation. The problem this whole post grew out of — a tightly constrained bin-packing instance (3 fixed item sizes) that has a clean closed form once you see the pattern-enumeration + weighting trick. Codeforces Gym 102770B — Bin Packing Problem. The classical problem stated almost verbatim — good for implementing First Fit correctly as a baseline. Codeforces 1066D — Boxes Packing. A Next-Fit-style greedy simulation wrapped in a binary-search outer loop. Community discussion worth reading\nCodeforces: \u0026ldquo;How to divide N integers into some groups such that the sum of each group doesn\u0026rsquo;t exceed a value, minimizing group count?\u0026rdquo; — this is bin packing, discussed from a purely contest-tactics angle. Codeforces: \u0026ldquo;Bin Packing with Multiple bins and limitations on bin capacity\u0026rdquo; — a worked discussion of the bitmask-DP approach for small nnn. USACO Guide — Bitmask DP. Good general treatment of the subset-DP technique this post\u0026rsquo;s bitmask solution relies on, beyond just bin packing. Primary sources, if you want the actual proofs in full\nD. S. Johnson, Near-Optimal Bin Packing Algorithms, PhD thesis, MIT, 1973 — dspace.mit.edu/handle/1721.1/57819. The origin of the First Fit / First Fit Decreasing bounds, weight-function proof technique included, all 100+ pages of it. M. R. Garey and D. S. Johnson, Computers and Intractability: A Guide to the Theory of NP-Completeness, W. H. Freeman, 1979. The canonical reference for 3-PARTITION and strong NP-completeness generally — still the right place to actually learn this material properly, not just read a summary of it. Wikipedia\u0026rsquo;s Bin packing problem and 3-partition problem articles are solid, well-cited starting points if you want a map of the wider literature before diving into primary sources. References D. S. Johnson. Near-Optimal Bin Packing Algorithms. PhD thesis, MIT, 1973. dspace.mit.edu/handle/1721.1/57819 — First Fit / Best Fit\u0026rsquo;s 1.7⋅OPT1.7\\cdot\\mathrm{OPT}1.7⋅OPT bound and First Fit Decreasing\u0026rsquo;s asymptotic 11/911/911/9 ratio. M. R. Garey and D. S. Johnson. Computers and Intractability: A Guide to the Theory of NP-Completeness. W. H. Freeman, 1979 — 3-PARTITION and strong NP-completeness. G. Dósa. The Tight Bound of First Fit Decreasing Bin-Packing Algorithm Is FFD(I)≤119OPT(I)+69\\mathrm{FFD}(I) \\le \\frac{11}{9}\\mathrm{OPT}(I) + \\frac{6}{9}FFD(I)≤911​OPT(I)+96​. 2007. link.springer.com/chapter/10.1007/978-3-540-74450-4_1 N. Karmarkar and R. M. Karp. An Efficient Approximation Scheme for the One-Dimensional Bin-Packing Problem. FOCS 1982, pp. 312–320. doi.org/10.1109/SFCS.1982.61 W. Fernandez de la Vega and G. S. Lueker. Bin Packing Can Be Solved Within 1+ε1+\\varepsilon1+ε in Linear Time. Combinatorica 1(4):349–355, 1981. doi.org/10.1007/BF02579456 J. Balogh, J. Békési, G. Dósa, L. Epstein, A. Levin. A New Lower Bound for Classic Online Bin Packing. Algorithmica, 2021. arxiv.org/abs/1807.05554 — the 1.542781.542781.54278 online lower bound. J. Balogh, J. Békési, G. Dósa, L. Epstein, A. Levin. A New and Improved Algorithm for Online Bin Packing. 2018. researchgate.net/publication/318260129 — the 1.578291.578291.57829 Advanced Harmonic upper bound. C. C. Lee and D. T. Lee. A Simple On-Line Bin-Packing Algorithm. Journal of the ACM, 1985 — the classical Harmonic algorithm\u0026rsquo;s ≈1.691\\approx 1.691≈1.691 ratio. Bin packing problem — Wikipedia 3-partition problem — Wikipedia Codeforces Gym 102770B — Bin Packing Problem Codeforces 1066D — Boxes Packing Codeforces 2038L — Bridge Renovation ","permalink":"https://priyanshudebnath.me/posts/bin-packing-np-hardness-approximation-algorithms/","summary":"A full pass through bin packing — the formal problem, why the decision version is NP-complete (and why the optimization version is strongly so), every classical approximation algorithm with its proven bound, the special cases that collapse back to polynomial time, and the backtracking/bitmask-DP techniques you\u0026rsquo;d actually use to solve small instances in a contest.","title":"Bin Packing — Why It's Hard, How to Approximate It, and When It Isn't Hard At All"},{"content":"The problem This one\u0026rsquo;s from Codeforces Round 1097 (Div. 1), problem B — Zhily and Barknights.\nThe gist: you\u0026rsquo;re given two arrays aaa and bbb of length nnn. You take a uniformly random permutation b′b\u0026#x27;b′ of bbb, define ci=ai⋅bi′c_i = a_i \\cdot b\u0026#x27;_ici​=ai​⋅bi′​, and you need the expected number of inversions in ccc, modulo 998244353998244353998244353. Constraints: n≤2000n \\le 2000n≤2000, but ∑n≤2000\\sum n \\le 2000∑n≤2000 across test cases, and ai,bi≤109a_i, b_i \\le 10^9ai​,bi​≤109.\nThe math for this problem was correct from my first submission. What was wrong was the data structure I used to implement it — that choice cost me two TLEs before I worked out exactly why, and by how much.\nThe idea (short version) Expected inversions is a linearity-of-expectation problem. For a fixed pair of positions i\u0026lt;ji \u0026lt; ji\u0026lt;j, you average over all ways to assign bbb-values to them. Instead of thinking permutation-by-permutation, you can restructure the sum so that you sweep iii from left to right, and for each iii, you compare ai⋅bja_i \\cdot b_jai​⋅bj​ against every ak⋅bla_k \\cdot b_lak​⋅bl​ pair seen so far (all inserted incrementally) — using a Fenwick tree / order-statistics structure to count how many prior values are bigger or smaller in O(log⁡n)O(\\log n)O(logn) instead of O(n)O(n)O(n) per comparison.\nConcretely, my final (accepted) solution keeps two structures:\nprodTree — holds every product ai⋅bja_i \\cdot b_jai​⋅bj​ seen for indices processed so far aTree — holds every raw aia_iai​ seen so far and for each new iii, before inserting it, it queries: \u0026ldquo;how many things bigger than my current candidate are already in here?\u0026rdquo; That count feeds directly into the running inversion total.\nI won\u0026rsquo;t paste the whole solve function in one block. I\u0026rsquo;ll go through it piece by piece further down, after covering the theory behind the data structure, since that\u0026rsquo;s what actually explains the TLE.\nAttempt #1: PBDS ordered_multiset My first working version used GNU\u0026rsquo;s Policy-Based Data Structures — specifically the tree container with tree_order_statistics_node_update, aka the thing every competitive programmer calls an \u0026ldquo;order statistics tree\u0026rdquo; or ordered_set / ordered_multiset.\ntypedef tree\u0026lt; pair\u0026lt;int, int\u0026gt;, null_type, less\u0026lt;pair\u0026lt;int, int\u0026gt;\u0026gt;, rb_tree_tag, tree_order_statistics_node_update\u0026gt; ordered_multiset; It compiled, gave the right answer on the sample cases, and I submitted it.\n381028293 TLE on test 3 4000 ms 145200 KB 381028795 TLE on test 3 4000 ms 157200 KB Two attempts, both failing on the same test. At that point I didn\u0026rsquo;t think much, later saw the math behind it.\nTo understand where the time was going, it helps to understand what ordered_multiset actually is under the hood. So let\u0026rsquo;s build up a Red-Black tree from the basics.\nPart 1 — Why a plain BST isn\u0026rsquo;t enough A Binary Search Tree is a tree where, for every node, everything in the left subtree is smaller and everything in the right subtree is bigger.\n5 / \\ 3 8 / \\ 1 4 Search, insert, and delete all walk down from the root, so their cost is proportional to the height of the tree. If you\u0026rsquo;re lucky and the tree stays bushy, height is about log⁡2n\\log_2 nlog2​n, and everything is fast.\nBut if you insert values in sorted order — 1, 2, 3, 4, 5 — the tree degenerates into a straight line:\n1 \\ 2 \\ 3 \\ 4 Now height equals nnn, not log⁡n\\log nlogn, and every operation costs O(n)O(n)O(n). This is the reason self-balancing trees exist: they force the height to stay logarithmic regardless of insertion order.\nPart 2 — The Red-Black invariants A Red-Black tree colors every node red or black and enforces five rules:\nEvery node is red or black. The root is black. Every null leaf is treated as black. A red node never has a red child — no two reds in a row on any path. Every path from a node to any of its null descendants passes through the same number of black nodes (its \u0026ldquo;black-height\u0026rdquo;). Rule 5 is the one doing the real work. It doesn\u0026rsquo;t force every path to be the same length — it forces the number of black nodes on every path to match. Combined with rule 4 (reds can\u0026rsquo;t stack), this caps how much longer any path can be than any other: at most a factor of 2.\nThe proof that height stays O(log⁡n)O(\\log n)O(logn) Define bh(x)\\text{bh}(x)bh(x) as the black-height of node xxx: the number of black nodes from xxx down to a null leaf (not counting xxx itself).\nLemma. A subtree rooted at xxx contains at least 2bh(x)−12^{\\text{bh}(x)} - 12bh(x)−1 internal nodes.\nProof, by induction on subtree height.\nBase case: xxx is a null leaf. bh(x)=0\\text{bh}(x) = 0bh(x)=0, subtree contains 20−1=02^0 - 1 = 020−1=0 nodes. True.\nInductive step: each child of xxx has black-height either bh(x)\\text{bh}(x)bh(x) (if the child is red) or bh(x)−1\\text{bh}(x) - 1bh(x)−1 (if the child is black). Either way, by the inductive hypothesis each child\u0026rsquo;s subtree has at least 2bh(x)−1−12^{\\text{bh}(x)-1} - 12bh(x)−1−1 nodes, so:\nsize(x)≥2(2bh(x)−1−1)+1=2bh(x)−1\\text{size}(x) \\ge 2\\left(2^{\\text{bh}(x)-1} - 1\\right) + 1 = 2^{\\text{bh}(x)} - 1size(x)≥2(2bh(x)−1−1)+1=2bh(x)−1■\\blacksquare■\nNow, because rule 4 forbids two reds in a row, at least half the nodes on any root-to-leaf path are black, so if hhh is the tree\u0026rsquo;s height:\nbh(root)≥h/2\\text{bh}(\\text{root}) \\ge h/2bh(root)≥h/2Plugging into the Lemma with nnn = total nodes:\nn≥2bh(root)−1≥2h/2−1n \\ge 2^{\\text{bh}(\\text{root})} - 1 \\ge 2^{h/2} - 1n≥2bh(root)−1≥2h/2−1h≤2log⁡2(n+1)h \\le 2\\log_2(n+1)h≤2log2​(n+1)That\u0026rsquo;s the guarantee. However you insert data, height never exceeds roughly twice log⁡2n\\log_2 nlog2​n.\nPart 3 — Fixing violations: rotations When you insert a new node, it always starts red (inserting black would instantly break rule 5 on one path; inserting red might break rule 4, which is locally repairable).\nIf the new node\u0026rsquo;s parent is black, nothing broke — done. If the parent is also red, you have two reds in a row, and you fix it based on the color of the \u0026ldquo;uncle\u0026rdquo; (the parent\u0026rsquo;s sibling):\nCase A — uncle is red. Just recolor: parent → black, uncle → black, grandparent → red. Then recheck one level higher, since the grandparent turning red might itself clash with its parent.\nCase B — uncle is black or missing. You need a rotation. A left rotation around node PPP with right child RRR: RRR takes PPP\u0026rsquo;s spot, PPP becomes RRR\u0026rsquo;s left child, and RRR\u0026rsquo;s old left subtree becomes PPP\u0026rsquo;s new right subtree.\nP R \\ / \\ R --\u0026gt; P (R\u0026#39;s old right) / \\ \\ (R\u0026#39;s (R\u0026#39;s old (R\u0026#39;s old left) old right) left) It\u0026rsquo;s three pointer reassignments — O(1)O(1)O(1) work — but it locally reshuffles the shape of the tree. A right rotation is the mirror image. Depending on whether the violation is a \u0026ldquo;straight line\u0026rdquo; or a \u0026ldquo;zig-zag,\u0026rdquo; you need one rotation plus a recolor, or two rotations plus a recolor.\nA worked example Insert 10, 20, 30, 15, 5 one at a time.\nInsert 10 — tree is empty, becomes root, forced black.\n10(B) Insert 20 — goes right of 10, colored red. Parent is black, no violation.\n10(B) \\ 20(R) Insert 30 — goes right of 20, red. Now 20(R) has red child 30(R) — violation. Uncle is nil (black), straight-line case → rotate left at 10.\n20(B) / \\ 10(R) 30(R) Insert 15 — 15 \u0026lt; 20, go left to 10; 15 \u0026gt; 10, becomes 10\u0026rsquo;s right child, red.\n20(B) / \\ 10(R) 30(R) \\ 15(R) 10(R) has red child 15(R) — violation. Uncle (30) is red → Case A: recolor 10 and 30 to black, 20 to red, then re-check from 20. Since 20 is root, force it back to black.\n20(B) / \\ 10(B) 30(B) \\ 15(R) Insert 5 — 5 \u0026lt; 20 → left to 10; 5 \u0026lt; 10 → becomes 10\u0026rsquo;s left child, red. Parent (10) is black, no violation.\n20(B) / \\ 10(B) 30(B) / \\ 5(R) 15(R) Tree is balanced, all 5 rules hold.\nPart 4 — Turning it into an order-statistics tree A plain std::set is a Red-Black tree, but it can\u0026rsquo;t answer \u0026ldquo;how many elements are smaller than xxx?\u0026rdquo; or \u0026ldquo;what\u0026rsquo;s the kkk-th smallest element?\u0026rdquo; in O(log⁡n)O(\\log n)O(logn) — because a node in a plain RB tree only knows its own key and its children\u0026rsquo;s pointers. It has no idea how many descendants it has.\ntree_order_statistics_node_update fixes this by attaching one extra integer to every node: subtree_size.\nint sizeOf(Node* n) { return n ? n-\u0026gt;size : 0; } void updateSize(Node* n) { if (n) n-\u0026gt;size = 1 + sizeOf(n-\u0026gt;left) + sizeOf(n-\u0026gt;right); } Every insert, delete, or rotation calls this update on the handful of nodes it touched. Since a rotation only changes the parent/child relationship of two nodes (xxx and yyy), only their sizes need recomputing — and in a specific order:\nvoid rotateLeft(Node* x) { Node* y = x-\u0026gt;right; x-\u0026gt;right = y-\u0026gt;left; if (y-\u0026gt;left) y-\u0026gt;left-\u0026gt;parent = x; y-\u0026gt;left = x; // ... parent pointer bookkeeping omitted for brevity ... updateSize(x); // x\u0026#39;s children changed -\u0026gt; fix x first updateSize(y); // y\u0026#39;s size depends on x\u0026#39;s corrected size } Why nothing above yyy needs updating: a rotation doesn\u0026rsquo;t add or remove any nodes from the tree — it just reshuffles which nodes are whose children. Every node outside {x,y,the subtree that moved}\\{x, y, \\text{the subtree that moved}\\}{x,y,the subtree that moved} still has exactly the same descendants it had before. Since size is purely a function of a node\u0026rsquo;s current children, only xxx and yyy can possibly have a new size, and yyy must be recomputed after xxx because yyy\u0026rsquo;s children now include the corrected xxx.\nThe two payoff operations order_of_key(q) — count of elements strictly less than qqq:\nint order_of_key(int q) { Node* x = root; int count = 0; while (x) { if (q \u0026gt; x-\u0026gt;key) { count += sizeOf(x-\u0026gt;left) + 1; x = x-\u0026gt;right; } else { x = x-\u0026gt;left; } } return count; } Correctness, by induction on the walk. If q\u0026gt;x→keyq \u0026gt; x{\\to}\\text{key}q\u0026gt;x→key, then every node in xxx\u0026rsquo;s left subtree is \u0026lt;x→key\u0026lt;q\u0026lt; x{\\to}\\text{key} \u0026lt; q\u0026lt;x→key\u0026lt;q (that\u0026rsquo;s size(left)\\text{size}(left)size(left) matches), and xxx itself is also \u0026lt;q\u0026lt; q\u0026lt;q (+1 more) — so we add exactly size(left)+1\\text{size}(left)+1size(left)+1 and recurse right, where any remaining smaller-than-qqq elements must live. If q≤x→keyq \\le x{\\to}\\text{key}q≤x→key, then xxx and its whole right subtree are ≥q\\ge q≥q, contributing nothing, and we recurse left. Both branches preserve the invariant \u0026ldquo;count so far is exactly correct for everything already visited,\u0026rdquo; so by induction the final count is exact. ■\\blacksquare■\nfind_by_order(k) — the kkk-th smallest element:\nNode* find_by_order(int k) { Node* x = root; while (x) { int L = sizeOf(x-\u0026gt;left); if (k \u0026lt; L) x = x-\u0026gt;left; else if (k == L) return x; else { k -= (L + 1); x = x-\u0026gt;right; } } return nullptr; } Both run in time proportional to the height of the tree, which we already proved is O(log⁡n)O(\\log n)O(logn). So on paper, this gives a fully indexable, rank-queryable balanced BST at O(log⁡n)O(\\log n)O(logn) per operation.\ncheck it\u0026rsquo;s uses here. Part 5 — Counting exactly how many operations my code did Here\u0026rsquo;s the structure of the hot loop in my first (TLE\u0026rsquo;d) solution:\nrep(i, 0, n) { rep(j, 0, n) { int g = prodTree.order_of_key({a[i]*b[j], INF}); int h = aTree.order_of_key({a[i], INF}); // ... accumulate into total ... } rep(j, 0, n) { prodTree.insert({a[i]*b[j], prodTimer++}); } aTree.insert({a[i], aTimer++}); } Let\u0026rsquo;s count every single operation exactly, for n=2000n = 2000n=2000 (the worst case, since ∑n≤2000\\sum n \\le 2000∑n≤2000 still allows one test case with n=2000n=2000n=2000).\nOperation How many times Which tree Max size that tree reaches order_of_key on prodTree n×n=n2n \\times n = n^2n×n=n2 prodTree n2n^2n2 order_of_key on aTree n×n=n2n \\times n = n^2n×n=n2 aTree nnn insert into prodTree n×n=n2n \\times n = n^2n×n=n2 prodTree n2n^2n2 insert into aTree nnn aTree nnn Total tree operations:\nTtotal=2n2 (prodTree)+(n2+n) (aTree)=3n2+nT_{\\text{total}} = 2n^2 \\;(\\text{prodTree}) + (n^2 + n)\\;(\\text{aTree}) = 3n^2 + nTtotal​=2n2(prodTree)+(n2+n)(aTree)=3n2+nFor n=2000n = 2000n=2000, n2=4,000,000n^2 = 4{,}000{,}000n2=4,000,000:\nTtotal=3(4,000,000)+2000=12,002,000 tree operationsT_{\\text{total}} = 3(4{,}000{,}000) + 2000 = 12{,}002{,}000 \\text{ tree operations}Ttotal​=3(4,000,000)+2000=12,002,000 tree operationsTwelve million operations isn\u0026rsquo;t a large number on its own — modern CPUs execute billions of instructions per second. The important question is what each of these operations actually costs on real hardware, not just in theory.\nCost per operation, using the height bound we proved prodTree grows up to n2=4,000,000n^2 = 4{,}000{,}000n2=4,000,000 entries:\nhprodTree≤2log⁡2(4,000,001)≈2×21.93≈43.9→44 pointer hopsh_{\\text{prodTree}} \\le 2\\log_2(4{,}000{,}001) \\approx 2 \\times 21.93 \\approx 43.9 \\rightarrow \\textbf{44 pointer hops}hprodTree​≤2log2​(4,000,001)≈2×21.93≈43.9→44 pointer hopsaTree grows up to n=2000n = 2000n=2000 entries:\nhaTree≤2log⁡2(2001)≈2×10.97≈21.9→22 pointer hopsh_{\\text{aTree}} \\le 2\\log_2(2001) \\approx 2 \\times 10.97 \\approx 21.9 \\rightarrow \\textbf{22 pointer hops}haTree​≤2log2​(2001)≈2×10.97≈21.9→22 pointer hopsTotal pointer traversal steps across the whole run:\n2n2 (prodTree ops)×44=8,000,000×44=352,000,0002n^2 \\text{ (prodTree ops)} \\times 44 = 8{,}000{,}000 \\times 44 = 352{,}000{,}0002n2 (prodTree ops)×44=8,000,000×44=352,000,000(n2+n) (aTree ops)×22=4,002,000×22≈88,044,000(n^2+n)\\text{ (aTree ops)} \\times 22 = 4{,}002{,}000 \\times 22 \\approx 88{,}044{,}000(n2+n) (aTree ops)×22=4,002,000×22≈88,044,000Grand total≈352,000,000+88,044,000=440,044,000≈4.4×108 pointer hops\\text{Grand total} \\approx 352{,}000{,}000 + 88{,}044{,}000 = 440{,}044{,}000 \\approx 4.4 \\times 10^8 \\text{ pointer hops}Grand total≈352,000,000+88,044,000=440,044,000≈4.4×108 pointer hopsTurning pointer hops into wall-clock seconds This is where the TLE actually comes from. A pointer hop in a tree made of heap-allocated nodes isn\u0026rsquo;t free the way an array index is. Every node was allocated separately by new, so nodes end up scattered across essentially random addresses in RAM. Walking parent → child → child means jumping between memory locations the CPU has no way to predict in advance.\nModern CPUs read memory in 64-byte cache lines and keep recently-used lines in small, extremely fast caches (L1, L2, L3). Rough costs on a typical 3 GHz machine:\nL1 cache hit: ~4 cycles ≈ 1 ns Main memory (cache miss): ~300 cycles ≈ 100 ns Because each pointer hop in our 4-million-node tree lands on an essentially random address, almost every hop is a cache miss. Using the pessimistic-but-realistic 100 ns/hop figure:\n4.4×108 hops×100 ns=44,000,000,000 ns=44 seconds4.4 \\times 10^8 \\text{ hops} \\times 100\\text{ ns} = 44{,}000{,}000{,}000 \\text{ ns} = 44 \\text{ seconds}4.4×108 hops×100 ns=44,000,000,000 ns=44 secondsOn top of that, every insert calls new under the hood — n2=4,000,000n^2 = 4{,}000{,}000n2=4,000,000 heap allocations, at roughly 20 ns each once you include allocator bookkeeping and rebalancing overhead:\n4,000,000×20ns=0.08 seconds4{,}000{,}000 \\times 20\\text{ns} = 0.08 \\text{ seconds}4,000,000×20ns=0.08 seconds— negligible compared to the 44 seconds above, but not zero.\nEstimated total: ~44 seconds. The actual time limit is 4 seconds — roughly 11x over budget, which matches what happened on both submissions: the judge simply hit the 4000ms cutoff.\nIn practice not every single hop costs the full 100 ns — the top few levels of an actively-used tree tend to stay in L2/L3 cache, which is why the real run finished in some large but finite time rather than a literal 44 seconds before Codeforces terminated it. But the order of magnitude, tens of seconds needed versus 4 allowed, is enough to explain the failure on its own.\nThe algorithmic complexity, O(n2log⁡n)O(n^2 \\log n)O(n2logn), was completely correct. The problem was that the constant hidden inside that log⁡n\\log nlogn was roughly 100x larger than it needed to be, purely because of how the data was laid out in memory.\nAttempt #2: swap the tree for a flat array The fix keeps the exact same math — same comparisons, same accumulation logic — and only changes the data structure. Instead of a Red-Black tree of heap nodes, I use coordinate compression plus a Fenwick tree (Binary Indexed Tree) backed by one contiguous vector\u0026lt;int\u0026gt;.\nFirst, compress every value I\u0026rsquo;ll ever query into a small rank:\nvi vals; rep(i, 0, n) { vals.pb(a[i]); rep(j, 0, n) vals.pb(a[i] * b[j]); } uni(vals); // sort + unique A tiny helper turns any real value into its compressed rank via binary search:\nauto get_id = [\u0026amp;](int x) { return lower_bound(all(vals), x) - vals.begin() + 1; }; The Fenwick tree implementation is simple — just an array and two loops:\nstruct Fenwick { int n; vector\u0026lt;int\u0026gt; tree; Fenwick(int n) : n(n), tree(n + 1, 0) {} void add(int i, int delta) { for (; i \u0026lt;= n; i += lsb(i)) tree[i] += delta; } int query(int i) { int sum = 0; for (; i \u0026gt; 0; i -= lsb(i)) sum += tree[i]; return sum; } }; And the hot loop barely changes shape at all — order_of_key becomes query, insert becomes add:\nrep(i, 0, n) { rep(j, 0, n) { int g = prodTree.query(get_id(a[i] * b[j])); int hii = sz_prod - g; int h = aTree.query(get_id(a[i])); int hiiiuu = sz_a - h; int diff = (hii - hiiiuu) % MOD; if (diff \u0026lt; 0) diff += MOD; total = (total + (diff * fact[n-2]) % MOD) % MOD; } rep(j, 0, n) { prodTree.add(get_id(a[i] * b[j]), 1); sz_prod++; } aTree.add(get_id(a[i]), 1); sz_a++; } (I also fixed a subtle negative-modulo bug along the way — diff can go negative before the % MOD, so it needs an explicit if (diff \u0026lt; 0) diff += MOD guard, and I precompute factorials once instead of recomputing an inverse in the inner loop.)\nThis submission passed:\n381029218 Accepted 3625 ms 92900 KB 3625 ms, inside the 4000 ms limit. The algorithmic complexity didn\u0026rsquo;t change from the first attempt, so the difference has to come from somewhere else.\nPart 6 — Why the Fenwick tree passes: same math, different constant\nThe Fenwick tree does the same number of \u0026ldquo;logical steps\u0026rdquo; as the PBDS tree — for n=2000n=2000n=2000, tree height was ≈44\\approx 44≈44 for the big structure and ≈22\\approx 22≈22 for the small one, and a Fenwick tree over the same number of elements needs the same ⌈log⁡2(size)⌉\\lceil \\log_2(\\text{size}) \\rceil⌈log2​(size)⌉ iterations per add/query, since each step strips off the lowest set bit. The total operation count Ttotal=3n2+n≈12T_{\\text{total}} = 3n^2+n \\approx 12Ttotal​=3n2+n≈12 million and total \u0026ldquo;step\u0026rdquo; count ≈4.4×108\\approx 4.4 \\times 10^8≈4.4×108 are essentially identical to before.\nThe difference is entirely in what each step costs, physically.\nA Fenwick tree is backed by one vector\u0026lt;int\u0026gt; allocated once, up front, as a single contiguous block of memory. Walking it means computing i -= lsb(i) or i += lsb(i) and jumping to a new array index — and because the whole array is one block, those jumps land inside memory the CPU has already loaded into cache, or can predict and prefetch, rather than jumping to a brand-new random heap address every single time.\nFor n=2000n=2000n=2000, the compressed value array vals has at most n+n2=2000+4,000,000≈4,000,000n + n^2 = 2000 + 4{,}000{,}000 \\approx 4{,}000{,}000n+n2=2000+4,000,000≈4,000,000 entries, so each Fenwick tree (prodTree, aTree) is a vector\u0026lt;int\u0026gt; of a few million 4-byte integers — a few tens of megabytes. That\u0026rsquo;s too big to fit entirely in L1 (usually 32–48 KB) or L2 (a few hundred KB to a few MB) cache, but:\nit fits comfortably in L3 cache on most modern CPUs (typically 8–32 MB), and even where it spills into RAM, sequential/strided access to one contiguous array is exactly the pattern hardware prefetchers are built to detect and pre-load ahead of time. Redoing the same wall-clock estimate as before, but now assuming most accesses hit cache (say, an average of ~2 ns per step instead of ~100 ns, blending a mix of L1/L2/L3 hits with the occasional real RAM miss):\n4.4×108 steps×2ns≈0.88 seconds4.4 \\times 10^8 \\text{ steps} \\times 2\\text{ns} \\approx 0.88 \\text{ seconds}4.4×108 steps×2ns≈0.88 secondsThat\u0026rsquo;s roughly 50x faster than the ~44 second PBDS estimate, for the same number of logical steps. It also removes the other overhead the tree had: no new call on every insert (the vector for each Fenwick tree is allocated exactly once, up front), no red-black rebalancing, no rotations, no recoloring, no extra size bookkeeping through parent pointers.\nThe actual submission ran in 3625 ms rather than my rough 0.88s estimate — real code always carries extra constant factor from the modular arithmetic, the lower_bound binary searches for coordinate compression, and I/O — but it lands in the same universe as the estimate, and critically, on the correct side of the 4-second wall, instead of ~10x over it.\nSummary Both solutions are O(n2log⁡n)O(n^2 \\log n)O(n2logn). Both perform roughly the same number of logical tree steps. The difference between the TLE and the accepted run came down to where the data physically lives in memory:\nA tree of individually new\u0026rsquo;d nodes is scattered across RAM, so each traversal involves multiple unpredictable cache misses, each costing on the order of 100 ns. A flat array backing a Fenwick tree is one contiguous block, so traversal is mostly cache hits and prefetch-friendly access patterns, each costing on the order of 1–2 ns. Same time complexity, but a 50–100x difference in the constant factor, entirely due to memory layout. ordered_set and ordered_multiset are useful — for smaller operation counts they\u0026rsquo;re simpler to write and fast enough. But once the operation count reaches into the millions in a tight loop, that per-operation constant becomes the deciding factor, and a flat array-based structure like a Fenwick tree or segment tree is usually the better choice.\nHere\u0026rsquo;s the minimal Fenwick tree itself, runnable and editable below. It reads n values followed by q inclusive [l, r] range-sum queries (0-indexed) and prints the sum for each — try changing the array or queries and hit Run, or use the test cases underneath to check it against a couple of known answers.\n#include \u0026lt;bits/stdc++.h\u0026gt; using namespace std; struct Fenwick { int n; vector\u0026lt;long long\u0026gt; tree; Fenwick(int n) : n(n), tree(n + 1, 0) {} void add(int i, long long delta) { for (++i; i \u0026lt;= n; i += i \u0026amp; (-i)) tree[i] += delta; } long long prefixSum(int i) { long long sum = 0; for (++i; i \u0026gt; 0; i -= i \u0026amp; (-i)) sum += tree[i]; return sum; } }; int main() { int n; cin \u0026gt;\u0026gt; n; Fenwick fen(n); for (int i = 0; i \u0026lt; n; i++) { long long x; cin \u0026gt;\u0026gt; x; fen.add(i, x); } int q; cin \u0026gt;\u0026gt; q; while (q--) { int l, r; cin \u0026gt;\u0026gt; l \u0026gt;\u0026gt; r; long long sum = fen.prefixSum(r) - (l ? fen.prefixSum(l - 1) : 0); cout \u0026lt;\u0026lt; sum \u0026lt;\u0026lt; \u0026#34;\\n\u0026#34;; } } Test cases ascending array, two ranges\nInput:\n5 1 2 3 4 5 2 0 2 1 4 Expected:\n6 14 includes a negative value\nInput:\n3 10 -5 7 1 0 2 Expected:\n12 ","permalink":"https://priyanshudebnath.me/posts/pbds-vs-fenwick-cache-tle/","summary":"My approach to Zhily and Barknights was correct from the start, but the PBDS ordered_multiset implementation TLE\u0026rsquo;d twice. This post covers how Red-Black trees and order statistics work, and why a Fenwick tree passed instead.","title":"Why My PBDS ordered_multiset Solution TLE'd (and the Fenwick Fix)"},{"content":"Hi, it\u0026rsquo;s my first blog. Please clap.\nI\u0026rsquo;ve been playing around with memory in C++ lately, which is a fun way of saying I crashed my program a lot and got mad at a variable that doesn\u0026rsquo;t have feelings.\nNot just using new and delete like a normal person. I wanted to know what they actually do underneath. Turns out, quite a lot. Turns out also, my computer has been quietly lying to me this whole time. More on that in a bit.\nWhere it started I read some of the usual stuff first. Blog posts. Docs. A couple of CppCon talks:\nHow to Write a Custom Allocator — Bob Steagall, CppCon 2017 An Allocator is a Handle to a Heap — Arthur O\u0026rsquo;Dwyer, CppCon 2018 How to Write a Heap Memory Profiler — Milian Wolff, CppCon 2019 All of it made sense on paper. Stack good, fast, small. Heap flexible, slower, manual cleanup. Cool story.\nBut I wanted proof. Not analogies. Actual proof that these two things exist as separate places in memory, right now.\nSo I wrote something :\n#include \u0026lt;iostream\u0026gt; using namespace std; int main() { // 1. Put some stuff on the Heap so we can see it int* massiveArray = new int[50000]; // 2. Put some stuff on the Stack int localNumber = 99; // 3. The Trap: The program will freeze here waiting for you to type int freeze; cin \u0026gt;\u0026gt; freeze; // Clean up delete[] massiveArray; return 0; } The trick is that cin \u0026gt;\u0026gt; freeze line. It freezes the program mid-run, on purpose. That gives me a window to go poke at the process while it\u0026rsquo;s still alive, instead of it finishing in a blink. (gemini told me)\nI ran it, and while it sat there waiting, I opened VMMap (a free tool from Sysinternals) and attached it to the running process.\nWhat VMMap actually showed me This is the exact physical layout of the address space Windows built for this one program. Here\u0026rsquo;s what stood out.\nThe free space is enormous. There\u0026rsquo;s a row labeled Free, sitting at roughly 128 terabytes. My machine does not have 128 TB of RAM. Nowhere close. This is virtual memory, laid bare. The OS hands every program a huge, clean, fake blueprint (or atleast that\u0026rsquo;s what I think)..\nThe heap is small, on purpose. I asked for new int[50000]. That\u0026rsquo;s 50,000 integers. The heap section in VMMap was only around 3.7 MB. The OS didn\u0026rsquo;t round up to something huge. It gave me close to exactly what I asked for, no more.\nThe stack has a hard ceiling. The stack section showed 2,048 KB. Exactly 2 MB. Fixed. That\u0026rsquo;s the whole budget for local variables and every nested function call on that thread. Blow through it, and the program dies with a stack overflow.\nReserved and used are two different things. The stack\u0026rsquo;s Size column said 2,048 KB. The Committed column right next to it said 32 KB. Windows reserved 2 MB but only actually wired up 32 KB of real RAM behind it. My program only used one small local variable, so that\u0026rsquo;s all it got. The rest of that 2 MB is not a fact, until something actually needs it.\nEvery address is right there. Scroll to the bottom table and there\u0026rsquo;s a line for Thread Stack, with a real hex address next to it, something like 00000005C2C00.... That\u0026rsquo;s the literal top of the stack for that thread. It\u0026rsquo;s a real number, on a real running process, on my machine.\nOkay, but what\u0026rsquo;s actually going on VMMap shows the result. So here\u0026rsquo;s the mechanism, as I understand it now.\nRegisters, quickly A register is a storage slot on the CPU chip itself. Not RAM. On the chip. There are very few of them, and each holds one number, usually 64 bits.\nThey\u0026rsquo;re fast because there\u0026rsquo;s no travel time. No bus, no memory controller, nothing external. Reading a register is about as close to instant as computing gets.\nx86-64 has 16 general-purpose registers. A few have jobs by convention:\nrax — holds return values. rbx — general-purpose, often kept safe across calls. rcx, rdx — general-purpose, also used for arguments. rsi, rdi — also used for arguments. Old names, from string-copy instructions decades ago. rsp — the stack pointer. Always points at the top of the stack. rbp — the base pointer. A fixed anchor inside the current function. r8–r15 — added when x86 went 64-bit. General-purpose, plus two more argument slots. rip — instruction pointer. The address of what runs next. rflags — status bits, set after comparisons and math. This is what if and loops read to decide where to jump. On Linux and macOS, function arguments go into rdi, rsi, rdx, rcx, r8, r9, in that order, for the first six. Anything past six spills onto the stack. The return value comes back in rax. That\u0026rsquo;s why your function with more than six arguements is slow, jokes on you!\nThere\u0026rsquo;s also a split between registers a function can freely trash (rax, rcx, rdx, rsi, rdi, r8–r11) and ones it has to save and restore if it touches them (rbx, rbp, r12–r15, rsp). That split is the only reason two functions, written by two different people, can call each other without stepping on each other\u0026rsquo;s data.\nThe stack, precisely The stack is a region of memory managed by one register: rsp.\nIt only grows and shrinks from one end. (LIFO) Last thing added, first thing removed. Because of that rule, nothing needs bookkeeping. rsp always knows exactly where the top is.\nGrowing it means subtracting from rsp. Shrinking it means adding back. One instruction. sub rsp, 32. That\u0026rsquo;s the entire cost.\nint square(int x) { int result = x * x; // lives on the stack return result; } int main() { int value = square(6); return 0; } Here\u0026rsquo;s what actually happens when square(6) runs:\n6 goes into rdi. call square pushes the return address, then jumps. Prologue: push rbp, mov rbp, rsp, sub rsp, 16. result lands at [rbp - 4]. The answer moves into rax. Epilogue: mov rsp, rbp, pop rbp, ret. (you can see this yourself deassembling your code)\nThe second square returns, that slot is done. The bits might still physically sit there for a while, but rsp has already moved past them. The program won\u0026rsquo;t touch that memory as valid again. This is exactly why returning a pointer to a local variable breaks — the address is real, the lease on it just expired.\nOn Linux, the default per-thread stack is usually 8 MB. My VMMap screenshot showed 2 MB, since that\u0026rsquo;s the Windows default. Either way, past that limit sits a guard page — memory deliberately left unmapped, so overflowing the stack crashes cleanly instead of quietly corrupting whatever\u0026rsquo;s next door.\nrbp, precisely rsp keeps moving during a function. rbp doesn\u0026rsquo;t. It gets set once, at the top of the function, and stays put. Every local variable is then just \u0026ldquo;so many bytes from rbp,\u0026rdquo; a number that never has to change mid-function.\nWhen a function calls another, the callee saves the caller\u0026rsquo;s rbp, sets up its own, and restores the old one before returning. That creates a chain — each saved rbp points back to the one before it, with a return address sitting right above it. Follow that chain, and you\u0026rsquo;ve rebuilt the entire call history. That\u0026rsquo;s exactly how debuggers print stack traces.\nOne catch: optimized builds often skip this (-fomit-frame-pointer, default at higher optimization levels) and track everything from rsp directly at compile time. rbp becomes just another free register. That\u0026rsquo;s why release-build stack traces sometimes look broken — the chain simply isn\u0026rsquo;t there anymore.\nThe heap, precisely No register owns the heap. No dedicated instruction. It\u0026rsquo;s entirely software — malloc in C, called internally by new in C++ — sitting on top of the OS.\nThe library gets memory from the kernel through two paths:\nbrk/sbrk — moves a single break pointer to extend one region. Older, used for smaller requests. mmap — asks for a fresh separate region. Used for bigger requests, past roughly 128 KB by default in glibc. Neither of those hands you physical RAM right away. They reserve address space and mark it valid in the page tables. Actual RAM only gets attached the first time you touch that memory — a page fault fires, the kernel finds a free physical page, zeroes it, and wires it in. This is exactly why Committed was so much lower than Size in my VMMap screenshot. Reserved isn\u0026rsquo;t the same as real.\nOn top of that, the allocator keeps its own records. Every chunk it hands out carries a small hidden header just before your pointer, noting its size. Free chunks get sorted into size buckets so searching is fast. When something is freed, neighboring free chunks get merged, to fight fragmentation.\nAll that searching, merging, and bookkeeping is why heap allocation costs more than stack allocation. If you want to know how it works, there is something called Bit Scan Forward. I don\u0026rsquo;t know in details tho. In multi-threaded programs it costs even more, since the heap is usually shared across threads and needs locking. The stack needs none of that — each thread already owns its own.\n#include \u0026lt;iostream\u0026gt; struct Player { int hp; char name[32]; }; int main() { Player* p = new Player{100, \u0026#34;test\u0026#34;}; std::cout \u0026lt;\u0026lt; p-\u0026gt;name \u0026lt;\u0026lt; \u0026#34; has \u0026#34; \u0026lt;\u0026lt; p-\u0026gt;hp \u0026lt;\u0026lt; \u0026#34; hp\\n\u0026#34;; delete p; } new does two things: calls operator new (which calls malloc, and throws instead of returning null on failure), then runs the constructor. delete reverses it — destructor first, deallocation second. Forget delete, and that memory never comes back. The stack cleans itself up automatically. The heap just sits there, unfreed, holding a grudge.\nFragmentation Only a heap problem, since the stack\u0026rsquo;s strict order makes it impossible there.\nExternal — enough free memory total, just not in one big enough piece. Internal — the allocator hands back more than you asked for, due to minimum sizes or alignment, and the extra is wasted. Why any of this matters Deep recursion or a giant local array overflows the stack. A big std::vector usually doesn\u0026rsquo;t, because its actual data lives on the heap. Only the small vector object sits on the stack. Returning a pointer to a local variable is broken, even when it compiles and even when it seems to work once. Custom allocators beat general-purpose ones for narrow use cases, because they skip all the bookkeeping a general allocator has to carry for every possible case. The toolkit Here\u0026rsquo;s what I actually used, split by platform.\nLinux, macOS, or WSL:\n# Disassemble the binary — look for push rbp / mov rbp, rsp / sub rsp, N objdump -d your_program # Live memory map of a running process cat /proc/self/maps cat /proc/\u0026lt;pid\u0026gt;/maps # Watch the program ask the OS for memory, live strace -e trace=mmap,brk ./your_program # Step through and watch registers move gdb ./your_program (gdb) break square (gdb) run (gdb) print $rsp (gdb) print $rbp (gdb) info registers Windows, no WSL:\n# VMMap (Sysinternals) — the tool behind this whole post # Run VMMap.exe, attach to your process, read the middle and bottom tables # Process Explorer (Sysinternals) — live view of a running process # Same suite, run procexp.exe # x64dbg — free debugger, step through assembly, watch every register live # Open your .exe, set a breakpoint, F9 to run, F8 to step # Already on Visual Studio? Nothing extra to install: # Debug -\u0026gt; Windows -\u0026gt; Disassembly # Debug -\u0026gt; Windows -\u0026gt; Registers None of this is required to understand the theory. But I had to write a blog, so you better read it (Implicit threat).\nClosing If I got something wrong here, tell me.\n","permalink":"https://priyanshudebnath.me/posts/stack-heap-registers/","summary":"Hi, it\u0026rsquo;s my first blog. I went looking for proof that the stack and heap actually exist, and found it.","title":"Stack, Heap, and the Registers That Finally Made Sense"},{"content":"","permalink":"https://priyanshudebnath.me/resume/","summary":"Download my CV.","title":"Resume"}]