Incorrect solution passes H1 lol, how to hack it?

Revision en1, by SkyWave2024, 2024-11-25 05:32:11

Code: 293163701

First, notice that

  • a string like $$$\texttt{RDRDRD}$$$ will change the array $$$[a,a+1,\ldots,b]$$$ to $$$[a+1,\ldots,b,a]$$$. Let's call this operation shift.
  • a string like $$$\texttt{RRRDDD}$$$ will change the array $$$[a,a+1,\ldots,b]$$$ to $$$[b,b-1,a,a+1,\ldots,b-2]$$$. Let's call this operation shuffle.

Now let's start sorting the array. Suppose $$$a$$$ is a permutation of $$$1 \sim n$$$. The basic idea is, in the $$$i$$$-th turn move $$$i$$$ to the end.

Let's consider the $$$i$$$-th turn, let $$$i$$$'s current position be $$$pos$$$. Now the array looks like $$$[\ldots,i,\ldots,1,2,3,\ldots,i-1]$$$.

  • In most cases, you can do a shuffle for $$$a[1,pos + 2]$$$. Now we are on $$$(pos+2,pos+2)$$$ and $$$a_{pos+2} = i$$$, then do a shift for $$$a[pos+2,n]$$$ is ok. To do this, we should have $$$pos \le n - i - 1$$$ so that the shuffle won't affect the $$$1,2,\ldots,i-1$$$ part.
  • Otherwise, Let's first do a shuffle for $$$a[1,pos]$$$, and shift for $$$a[pos,n]$$$. Now $$$a_n$$$ is a random number. Then shift the whole array and we have $$$a_n = i$$$. Then shuffle the whole array, now we have $$$a_1 = i$$$ and $$$a_n = i - 1$$$, do another shift for whole array is ok. For example, we want to move $$$4$$$ to the end, we do $$$[6,5,4,1,2,3] \to [4,5,1,2,3,6] \to [5,1,2,3,6,4] \to [4,6,5,1,2,3] \to [6,5,1,2,3,4]$$$.

Please note that the first case costs us $$$1$$$ operation, but the second one costs us $$$4$$$. As a result, we may need more than $$$2n+4$$$ operations to sort finish it. Now, if we use more than $$$2n+4$$$ operations, shift $$$a[1,i]$$$, shuffle $$$a[i,n]$$$ before everything begins. Try each $$$i$$$ until we use less than $$$2n+4$$$ operations, and it get $$$\color{green}{\text{Accepted}}$$$!

I believe it can be hacked, but i don't know how. Can anyone hack it :)

History

 
 
 
 
Revisions
 
 
  Rev. Lang. By When Δ Comment
en1 English SkyWave2024 2024-11-25 05:32:11 1785 Initial revision (published)