# As one order of three numbers counts for less, the best arrangement gains one block at a time

How should we arrange a long list of distinct numbers to make two particular orders common among its triples? Pick any three positions and read their entries from left to right. Give one point for *low, high, middle* (132) and \(t\) points for *middle, low, high* (213). When both orders count equally, two descending blocks suffice: the entries decrease within each block, but every entry in the second exceeds every entry in the first. When only 132 counts, the best arrangement has infinitely many blocks in the limit of a long list. Neither extreme tells us how to arrange the list when both orders count but one matters more.

We find that two blocks remain optimal until the credit for 213 falls to about 0.31945. Below that weight, the best arrangement passes through every finite block count from three onward. Each new block starts at the left edge with zero width and grows as the weight falls. The other blocks persist, and their widths change continuously.

## Counting triples in descending blocks

A pattern's *density* is the fraction of all triples of positions with that order. The score to maximize is the density of 132 plus \(t\) times the density of 213, as the list length tends to infinity. Since only ranks matter, we can write the list as a permutation, an ordering of the integers from one to its length. A permutation built from descending blocks in increasing order of value is called *layered*, and its blocks are called layers. In \(21\,|\,543\), for instance, one entry from the first layer and two from the second always give 132. Two from the first and one from the second give 213.

Why consider only layered permutations? Albert and colleagues [1, Theorem 2.2] proved that for any collection of layered patterns, allowing repeated patterns, some layered permutation at each length maximizes their total number of occurrences counted with multiplicity. For a rational weight \(t=p/q\), take \(q\) copies of 132 and \(p\) copies of 213. At fixed length, each arrangement's score is a straight-line function of \(t\). The maximum over finitely many such lines is continuous, whether we consider all arrangements or only layered ones. Thus the two maxima, equal at every rational weight, agree at every real weight.

Albert and colleagues also gave the geometric optimum for 132 alone [1]: removing its largest block leaves a scaled copy of the arrangement. At equal weights [1, Theorem 2.5 and Remark 2.6], reordering the blocks leaves the score unchanged. With unequal weights, a large block supplies many decreasing pairs, and it matters which side supplies the third entry. Kenyon and colleagues studied which pattern densities can occur together, including those for the increasing triple 123 and the decreasing triple 321 [2]. General results on layered patterns [3] and bounds derived from relations among counts of small patterns (flag algebras) [4] leave open which layered arrangements maximize this unequal-weight score.

Scale positions in the list to the interval from zero to one. The cuts between blocks now define a partition of that interval. A block occupying \([a,b]\) supplies an inverted pair (two entries in decreasing order) with probability proportional to \((b-a)^2\). For 132 the third entry must lie to its left, in length \(a\); for 213 it must lie to its right, in length \(1-b\). Any one of the three sampled entries can be the entry outside the pair. Writing the limiting score as \(3S_t\) separates this factor of three from the sum over blocks:
\[
 3S_t=3\sum_{[a,b]}(b-a)^2[a+t(1-b)].
\]
Maximizing this formula over partitions gives the limiting maximum over all permutations. For lists of length \(n\), using exact finite pair counts instead of squared widths changes the normalized score by at most a constant times \(1/n\), uniformly over arrangements. Conversely, integer block lengths can approximate any fixed finite partition as the list grows.

## The optimal partitions

Figure 1 shows where to cut the list at each weight. Each layer count has its own interval of weights. At a transition the new layer still has width zero, so the smaller count applies.

![Interior boundaries of the optimal partition plotted against decreasing weight. Each new boundary emerges from position zero.](results/boundaries.png)

Figure 1. Read from left to right as 213 loses weight. At a fixed weight each curve marks a cut in the list; the vertical distances between cuts, including the ends at zero and one, are the layer widths. The dotted lines mark the first four transitions. The curves are numerical evaluations of the construction below.

| Layers before → after | Certified weight interval |
|---|---|
| 2 → 3 | (0.319448, 0.319449) |
| 3 → 4 | (0.063817, 0.063818) |
| 4 → 5 | (0.018898, 0.018899) |
| 5 → 6 | (0.006346, 0.006347) |

Table 1. The first four transitions as the weight falls. Each open interval brackets an exact transition; the proof does not use rounded weights. Farther down the sequence, each transition weight approaches 36.6% of the preceding one, the same fraction of the geometric 132 arrangement that remains after removing its largest layer.

To recover the widths themselves, we need just one adjustable length. Choose the first width, grow the other layers by a fixed rule, and vary the first width until the right edge lands at one. For \(0<t<1\), define three coefficients and an upper bound \(U\) on the first width: \(A=1+2t,\ B=2+t,\ d=1-t,\ U=2t/A\). Start with first width \(s\) in \((0,U]\) and endpoint \(x=s\). From a width \(u\) ending at \(x\), put \(H=t+dx\) and append the width
\[
 v=\frac{H+\sqrt{(H-Au)^2+Ad u^2}}{A}.                         \tag{1}
\]
Replace \((x,u)\) by \((x+v,v)\). Denote the endpoint after \(m\) layers by \(X_m(s,t)\); the recurrence can run past one.

### Theorem 1 (weighted packing)

For each weight \(0<t<1\), exactly one layered partition attains the limiting maximum over all permutations. To find its layer count, label the transition from \(m\) to \(m+1\) layers by its weight \(\tau_m\), and set \(\tau_1=1\). For \(m\ge2\) there is a unique \(\tau_m\in(0,1)\) with \(X_m(U,\tau_m)=1\), where \(U\) is evaluated at \(\tau_m\). These weights decrease strictly to zero.

On \([\tau_m,\tau_{m-1})\) the unique optimal partition has \(m\) positive widths, given by (1) with the unique \(s\in(0,U]\) reaching one at step \(m\). At \(\tau_m\) the first width is \(U\); starting the longer construction at \(s=0\) gives one zero followed by exactly the same \(m\) positive widths. Substituting these widths into \(3S_t\) gives the maximum weighted density at that weight. The successive transition weights satisfy \(\tau_{m+1}/\tau_m\to1/(1+\sqrt3)\).

For example, the first transition is the unique root in \(0<t<1\) of \(1-4t+4t^2-4t^3=0\). At equal weights (\(t=1\)), let \(u\) run over the layer widths. The solution is two halves: \(S_1=\sum u^2(1-u)\le\sum u/4\), with equality only for halves.

At \(t=0\) the cuts accumulate at the left edge: each interval \([a,b]\) has the same endpoint ratio \(a/b=q=(\sqrt3-1)/2\). To see why this ratio is optimal, divide each interval’s contribution to \(S_0\) by \(b^3-a^3\). The quotient is \(r(1-r)/(1+r+r^2)\) for \(r=a/b\), uniquely maximized at \(q\). Summing the bound gives equality for the geometric partition and density \(2\sqrt3-3\).

## The boundary of achievable densities

How much of one order can we gain at the expense of the other? Plot the density \(z\) of 213 on the horizontal axis and the density \(p\) of 132 on the vertical axis. For each weight, the maximum score \(M(t)=\max(p+tz)\) gives a straight line \(p=M(t)-tz\). Every achievable pair lies on or below it, and the optimal partition supplies a point on it. This is why \(M(t)\) is called a support function. Figure 2 traces those points for \(0\le t\le1\). Reversing the list and replacing each rank by its opposite (smallest by largest, and so on) interchanges the patterns. Reflecting the curve across the diagonal therefore gives the other half for positive weights. This argument applies regardless of whether the achievable region is convex.

![Supported density pairs, with 213 on the horizontal axis and 132 on the vertical axis.](results/density-boundary.png)

Figure 2. With equal weights both densities are \(3/8\). At the 132-only endpoint the geometric widths \((1-q)q^j\), \(j\ge0\) from right to left, give \(p=2\sqrt3-3\) and \(z=3(1-q)^2[(1-q^2)^{-1}-(1-q^3)^{-1}]\). The first two changes in layer count are marked. At each change the tangent slope is still \(-t\).

Write \(p_t,z_t\) for the constructed densities. The widths vary continuously across each transition once the zero width is included, as proved below. Thus \(p_t,z_t\) are continuous at every interior weight. For \(t<r\), optimality at the two weights gives
\[
 z_t\le [M(r)-M(t)]/(r-t)\le z_r .
\]
Consequently \(M'(t)=z_t\). Every maximizing pair, even one from a nonlayered construction, must have this same second coordinate: its affine score touches \(M\) from below, so their derivatives agree. Its first coordinate is then \(M(t)-tz_t\).

Different interior weights give different points on the curve. If two weights gave the same point, the layered partition giving that point at one weight would be optimal at the other. Move only the last interior cut to the right, with adjacent widths \(u<v\). The derivative of \(z/3\) is \(2uv-u^2>0\), since only the term \(u^2v\) varies. At an optimum, moving the cut has zero first derivative for the score \(p+tz\). Since the derivative of \(z\) is positive, only one weight can meet that condition. Hence \(z_r>z_t\) for \(r>t\); the secant slope \((p_r-p_t)/(z_r-z_t)\) lies between \(-r\) and \(-t\) by the two optimality inequalities. The boundary has a tangent even where the layer count changes.

The supported points also converge to the geometric endpoint as \(t\downarrow0\). To see this, take any sequence of weights tending to zero and keep the block boundaries, including both ends, at each weight. These closed sets have a convergent subsequence: the space of nonempty closed subsets of the interval is compact in the Hausdorff metric, which measures how far a point in either set can be from the nearest point in the other. Along this subsequence both densities converge to those of the limiting cut set. To see why, separate the gaps between cuts by width: gaps of width above \(\epsilon\) have convergent endpoints (choose \(\epsilon\) different from every limiting gap width), and gaps of width at most \(\epsilon\) contribute at most \(3\epsilon\) to either density. Since the reward \(tz\) vanishes uniformly, the limiting partition maximizes \(p\). Equality in the geometric bound above requires the gaps to exhaust the interval up to measure zero and each gap to have \(a/b=q\). Such gaps have no positive accumulation point of endpoints (their lengths are \((1-q)b\)); starting from 1 therefore forces exactly the geometric cut set. Every convergent subsequence gives the same density pair.

## Why the recurrence describes every optimum

The proof has three steps. First we show that a best layered arrangement exists and that each of its cuts must obey (1). Then we show how the requirement that the last layer end at one selects a single arrangement. Finally we show that as the weight falls, each layer count gives way to the next exactly once.

### Lemma 1 (finite maximizers)

For \(0<t<1\) the layered supremum is attained. Every maximizing layered limit has finitely many layers, with strictly increasing widths, first width \(s\le U\), and stationary interior cuts (the derivative of the score with respect to each cut is zero).

A positive reward for 213 puts a lower bound on the total width of two neighboring layers. Splitting \([a,b]\) into consecutive widths \(u,v\) gains
\[
 uv\,[v+tu-2(a+t(1-b))]
\]
in \(S_t\). If \(u+v<U\) this is negative, since \(a+t(1-b)\ge t(1-u-v)\). Start with any finite partition and merge a pair whenever its total width is below \(U\). Each merge removes one layer and increases the score, so the process stops. If \(k\) widths survive, the disjoint pairs \((1,2),(3,4),\ldots\) each occupy length at least \(U\); hence \(\lfloor k/2\rfloor U\le1\). Thus all finite scores are bounded by the supremum over at most \(N=\lfloor2/U+1\rfloor\) widths. Pad with zeros to view these partitions in one closed, bounded set of possible width vectors, the simplex \(\sum_{i=1}^N u_i=1,\ u_i\ge0\). The score is a polynomial there, so it attains its supremum. Discarding zero widths gives a finite partition with this same score.

An infinite partition cannot tie the finite maximum. Describe a layered limit by its closed set of cuts in \([0,1]\), including both ends. Each gap between cuts is a descending layer; any length left over represents an increasing part and supplies no inverted pairs. Approximate its score by keeping all layers wider than \(\epsilon\) and dividing the remaining length into pieces no wider than \(\epsilon\). Both the omitted and the replacement pieces contribute at most \(\epsilon\) to \(S_t\), since the sum of their squared widths is at most \(\epsilon\) times their total length. This approximation bounds every layered score by the finite maximum.

To see why every infinite cut set scores strictly less, note that its cuts have an accumulation point. We can therefore choose three distinct cuts \(a<c<b\) with \(b-a<U\). Keep them in every finite approximation; no retained gap can cross any of them. First merge pieces within each of \([a,c]\) and \([c,b]\), then merge those two intervals. Each merge improves the score because its span is below \(U\). The last one alone gains a fixed \(\delta>0\): its two widths are always \(c-a\) and \(b-c\), and its outer endpoints are always \(a,b\). Even the improved partition scores at most the finite maximum. The original approximations therefore fall short by at least \(\delta\), and so does their limit.

For \(t<1\), swapping adjacent widths \(u,v\) to put the smaller first improves the score if they were out of order: the contribution that distinguishes the orders is \((1-t)uv(v-u)\) in favor of \((u,v)\). Splitting a tiny initial piece from the first width \(s\) would increase the score if \(s>2t(1-s)\); thus \(s\le U\).

To determine the next width, fix the outer endpoints \(a,b\) and vary the common cut \(x\), so \(u=x-a,\ v=b-x\). Only \(u^2[a+t(1-x)]+v^2[x+t(1-b)]\) changes. At an interior maximum its derivative vanishes, giving
\[
 P(v)=Av^2-2H(v-u)-Bu^2=0.
\]
Since \(P(u)=-du^2<0\) and the quadratic opens upward, its two roots lie on opposite sides of \(u\). The swap inequality requires \(v\ge u\), so the larger root, (1), is forced. Applying this at successive cuts shows that every optimizer with \(m\) positive widths is obtained by starting at some \(0<s\le U\) and stopping with \(X_m(s,t)=1\). A one-layer partition has zero score, hence cannot win. We now ask how many choices of starting width and layer count satisfy that endpoint condition.

## Selecting the endpoint at a fixed weight

The next two steps use a computer to prove inequalities over continuous ranges of weights and widths. For each small rectangle of inputs, interval arithmetic encloses every possible value of a function between rational bounds, rounding the lower bound down and the upper bound up. The resulting finite list of inequalities is the certificate. An [interval verifier](analysis/certify.py) checks it in seconds. The arithmetic is given under Verification.

### Lemma 2 (uniqueness of the endpoint)

For \(0<t\le .32\), \(X_m(s,t)\) increases strictly with \(s\in[0,U]\) for every \(m\). Exactly one positive starting width and layer count then reach one. For \(.32<t<1\) only two layers can maximize, and their widths are unique.

For small weights, increasing the initial width always moves the final endpoint to the right. To see why this remains true after arbitrarily many steps, measure each width relative to the local scale \(H\). Write \(w=u/H\), and set
\[
 h=\sqrt{(1-Aw)^2+Adw^2},\quad f=(1+h)/A,\quad R=1+df,\quad F=f/R .
\]
The new width is \(Hf\), the new \(H\) is \(HR\), and the new \(w\) is \(F\). Initially \(s/(t+ds)\le2/3\). Once in \([0,2/3]\), the relative width stays there: \(Bw\le2\) implies \(h\le1\), hence \(F\le2/(A+2d)=2/3\).

Let \(R_i=H_i/H_{i-1}\), with \(H_0=t\) and \(H_i=t+dX_i\). The relative width of layer \(i\) is \(w_i=(1-1/R_i)/d\). It follows that the logarithmic derivative of \(R_{i+1}\) with respect to \(R_i\) is
\[
 L(t,w_i)=\frac{(Bw_i-1)(1-dw_i)}{h R}.
\]
Here the subscript denotes a partial derivative: \(f_w=(Bw-1)/h\). Writing \(L_i=L(t,w_i)\), the chain rule gives the derivative of \(\log(H_m/t)\) with respect to \(\log R_1\) as \(1+L_1+L_1L_2+\cdots+L_1\cdots L_{m-1}\).

We use the following rational bounds on the whole invariant interval; the certificate below specifies the finite arithmetic. For \(0\le t\le .32\), \(-.56\le L\le .26\) and \(0\le L(t,F(t,w))\le .30\). Only the first factor \(L_1\) can be negative, because every later width is an image under \(F\). Bounding the remaining products by a geometric series, the log derivative is therefore at least \(1-.56/(1-.30)>0\). As \(R_1=1+ds/t\), every \(X_m\) is strictly increasing in \(s\) here for positive \(t\). Moreover
\[
 X_m(0,t)=X_{m-1}(U,t)\quad(m\ge2).
\]
Indeed, starting with zero in (1) appends \(U\) at the first step, leaving exactly the state \((x,u)=(U,U)\). Write \(E_m=X_m(U,t)\). By continuity and strict increase, the image of \(0<s\le U\) at step \(m\ge2\) is precisely \((E_{m-1},E_m]\). Here \(E_1=U<1\); each added width is at least \(t/A\), so \(E_m\) increases without bound. These half-open intervals thus tile \((U,\infty)\), with each shared endpoint belonging only to the shorter branch. Exactly one of these intervals contains the target endpoint, one. This selects the layer count; strict increase selects the starting width. Since a maximizer exists by Lemma 1, it must be this partition.

At higher weights it suffices to show that three layers would already extend past one. On \([.32,.5]\) the certificate gives \(-.68\le L\le .43\). Thus even the three-step derivative is positive: \(1+L_1(1+L_2)\ge1-.68(1+.43)>0\). Its minimum endpoint is \(X_3(0,t)=X_2(U,t)>1\): inserting \(u=U, v=1-U\) into \(P\) gives \((1-4t+4t^2-4t^3)/A^2\), negative there. In fact up to \(t=1/2\), \(1-U\ge U\), so the sign of this strictly decreasing numerator distinguishes the two sides of the first transition.

For \(1/2\le t<1\), three widths would require \(s<1/3\) and the second width at most \((1-s)/2\), by increasing order. But at \(u=x=s, v=(1-s)/2\) the value of \(4P(v)\) is
\[
 (1-2t)+(-6+12t)s+(5-14t)s^2 .
\]
It interpolates linearly between \(-2s^2\) at \(t=1/2\) and \(-(1-3s)^2\) at one. It is strictly negative, so the second width must be larger. Thus no optimal partition in this range has room for three layers. For two layers the logarithm of the score \(S_t=s(1-s)(1-ds)\) is strictly concave on \((0,1)\), so there is only one interior stationary point. Also \(X_2(U,t)>1\) for \(t>1/2\) since \(U>1/2\) and the next width is larger.

## Ordering the transitions in weight

### Lemma 3 (one crossing per layer count)

For each \(m\ge2\), \(X_m(U,t)=1\) has exactly one root in \(0<t<1\), crossing from below to above. These roots decrease strictly to zero.

To see why a layer count cannot disappear and return as the weight changes, start the normalized iteration at \(w_0=0\). The first true width is then \(U\); write
\[
 D_m(t)=t\prod_{i=0}^{m-1} R(t,w_i)=t+d X_m(U,t),\qquad w_{i+1}=F(t,w_i).
\]
Since \(d=1-t\), the endpoint reaches one exactly when \(D_m(t)\) crosses one. For \(m=2\) uniqueness follows from the cubic above. For \(m\ge3\) all crossings lie below .08: the certificate gives \(D_3>1\) on \([.08,.32]\), while above .32 already \(X_2(U,t)>1\).

On \([0,.08]\) the certificate gives \(R\ge2\) and, with subscripts denoting partial derivatives,
\[
 |F_t|\le .18,\quad |F_w|\le .15,\quad
 |(\log R)_t|\le2.12,\quad |(\log R)_w|\le .36.
\]
Hence \(|w_i'|\le .18/(1-.15)<.22\) uniformly, and each factor has total log derivative of magnitude less than 2.3. At *any* crossing \(t\le2^{-m}\), so
\[
 (\log D_m)'\ge 1/t-2.3m\ge2^m-2.3m>0\qquad(m\ge3).
\]
For each fixed \(m\ge3\), continuity gives \(D_m(0)=0\), whereas \(D_m(.08)\ge D_3(.08)>1\). There is at least one crossing. A second upward crossing would require a return to one from above, which the positive derivative at every root rules out: the first return after an upward crossing would have nonpositive left derivative. Since \(D_{m+1}>D_m\) for \(0<t<1\), the roots decrease strictly. The bound \(\tau_m\le2^{-m}\) for \(m\ge3\) gives their limit.

To finish Theorem 1, for \(t\le .32\) the condition \(E_{m-1}<1\le E_m\) from Lemma 2 now says exactly \(\tau_m\le t<\tau_{m-1}\) (using \(E_1<1\)). Higher weights are already covered by the two-layer case. Within each interval the widths vary continuously: if weights converge, any subsequential limit of their starting widths must solve the limiting endpoint equation, whose solution on the branch is unique. At a transition the two endpoint solutions are \(U\) on the shorter branch and zero on the longer branch. Formula (1) then gives the same positive widths in the limit from either side. In the high-weight two-layer case continuity follows likewise from the unique maximum on the compact interval of possible first widths.

The spacing also has a limit. Put \(C_m=\prod_{i<m}R(0,w_i(0))\). The same derivative estimate gives \(|\log(\tau_m C_m)|\le2.3m\tau_m\to0\). At zero each application of \(F\) reduces the distance between any two relative widths to at most .15 times their previous distance, so \(C_{m+1}/C_m\) tends to the growth factor \(R_*\) at the unique width left unchanged by \(F\). Substituting \(w=(R_*-1)/R_*\), \(f=R_*-1\) in \(f^2-2f+2w-2w^2=0\) gives \(R_*^2-2R_*-2=0\) since \(R_*>1\). Therefore \(\tau_{m+1}/\tau_m\to1/(1+\sqrt3)\). The infinite 132 construction's tail fraction thus controls the spacing of finite-layer transitions as well.

## Verification

The derivative bounds cover whole intervals between sample points. We enclosed the result of each operation between multiples of \(10^{-9}\), rounding products and reciprocals outward and using integer square roots. Rectangles have sides \(1/500\) in \(t\) and \(1/750\) in \(w\); for the three-factor lower bound on \(D_3\) only \(t\) is subdivided, in thousandths. For each rectangle we enclosed \(h,f,R,F,L\) in the order defined above. For the second-step bound we evaluated \(L\) on the same weight interval with the enclosure for \(F\) as its width interval. For the derivatives we used \(f_t=(w^2-2f^2)/(2h)\) (differentiate \(Af^2-2f+2w-Bw^2=0\)), \(F_t=(f_t+f^2)/R^2\), \(F_w=f_w/R^2\), and \(R_t=df_t-f, R_w=df_w\). The certificate also brackets the four numerical roots reported above. These bounds certify the transitions directly from the endpoint equations.

As an independent check, we maximized the exact weighted counts over all ways to divide lists of lengths 503, 2003 and 5003 into layers. In a list of final length \(n\), a block of width \(b-a\) at endpoint \(b\) contributes \(\binom{b-a}{2}[a+t(n-b)]\), counting its inverted pairs with all possible singleton positions. For each endpoint a dynamic program tries every possible start of the last block, adds its contribution to the best count for the preceding blocks, and keeps the largest sum. At the eight weights .005, .012, .035, .110, .280, .350, .470 and .810, both longer lists gave the predicted layer count in every case, and each width was within two entries of its continuum prediction.

The layer counts describe the long-list limit: at length 503 and weight .005 the finite calculation gave five layers instead of six. A very small layer in the limiting arrangement can disappear at finite length. The uniqueness result concerns layered arrangements. Other arrangements that tie at an interior weight must give the same two densities, but the proof does not classify their structure.

## References

[1] M. H. Albert, M. D. Atkinson, C. C. Handley, D. A. Holton and W. Stromquist. [On packing densities of permutations](https://doi.org/10.37236/1622). *Electronic Journal of Combinatorics* 9 (2002), R5.

[2] R. Kenyon, D. Král’, C. Radin and P. Winkler. [Permutations with fixed pattern densities](https://doi.org/10.1002/rsa.20882).

[3] J. de Oliveira Bastos and L. N. Coregliano. [Packing densities of layered permutations and the minimum number of monotone sequences in layered permutations](https://dmtcs.episciences.org/articles/1516).

[4] J. Sliačan and W. Stromquist. [Improving bounds on packing densities of 4-point permutations](https://arxiv.org/abs/1704.02959).
