Repovive
  • Dashboard
  • Ranking
  • Submissions
  • Discussion
  1. Contests
  2. Starter Round 9
  3. 25D

My submissions

/
/
/
/

Announcements

My submissions

/
/
/
/

Announcements

Solve
Author

The shape of the prefix sums

Let p0=0p_0=0p0​=0 and pi=a1+⋯+aip_i=a_1+\cdots+a_ipi​=a1​+⋯+ai​. We need a permutation ppp of 1,…,n1,\ldots,n1,…,n with −1≤pi−pi−1≤bi-1\le p_i-p_{i-1}\le b_i−1≤pi​−pi−1​≤bi​.

Whenever the permutation decreases, it must decrease by exactly one: a larger drop is forbidden, and equal values would repeat a prefix sum.

Suppose its first value is rrr. It must begin r,r−1,…,1r,r-1,\ldots,1r,r−1,…,1. If it increases before reaching 111, it can never return to the remaining smaller values: moving down to them would require passing through a value already used.

The same argument applies to the unused values. Thus every valid permutation is obtained by splitting 1,2,…,n1,2,\ldots,n1,2,…,n into consecutive blocks and reversing each block. For example, blocks [1,2][1,2][1,2], [3][3][3], [4,5,6][4,5,6][4,5,6] give 2,1,3,6,5,42,1,3,6,5,42,1,3,6,5,4.

Inside a block, ai=−1a_i=-1ai​=−1, which always satisfies its bound. Only the jumps at block starts need to be checked.