adsijf's blog

By adsijf, history, 3 years ago, In English

UVa 1315

Someone help me solve this problem please, I don't know how to find formula for this problem

  • Vote: I like it
  • -11
  • Vote: I do not like it

»
3 years ago, # |
Rev. 4   Vote: I like it 0 Vote: I do not like it

Let $$$n = 2q + r$$$, where $$$q = \lfloor \frac{n}{2} \rfloor$$$, and $$$r = n\mod2$$$. The minimum number of times two neighbors swap their positions such that all $$$n$$$ participants sit around the table in reverse order is related to the number of adjacent pair swap operations required to reverse an array. Reversing an array of size $$$k$$$ can be done in $$$k-1$$$ iterations, and requires $$$k-1, k-2, \ldots, 1$$$ swap operations, respectively. The total number of swap operations is then equal to $$$k \choose 2$$$. The answer should be to partition the participants into two equal or almost equal sized arrays: the first includes participants $$$1, 2, \ldots, q$$$, and the second includes participants $$$q+1, q+2, \ldots, 2q+r$$$. The total number of swap operations is then equal to $$$q \choose 2$$$+ $$$q+r \choose 2$$$ = $$$q(q+r-1)$$$.

Note that reversing the $$$n$$$ participants without partitioning requires $$$n \choose 2$$$ = $$$q(2q+2r-1)$$$ which is larger than the minimum value. In general, partitioning into $$$k$$$ and $$$n-k$$$ arrays and reversing each array independently requires $$$k \choose 2$$$+ $$$n-k \choose 2$$$ = $$$n \choose 2$$$ — $$$k(n-k)$$$ swap operations. It is simple to prove that the minimum value is achieved when $$$k = q$$$ and $$$k(n-k) = q(q+r)$$$.