004. Multiplying Adjacent Elements in a Permutation
Given a permutation (a1,a2,⋯,an) of {1,2,⋯,n}, maximize a1a2+a2a3+⋯+an−1an+ana1.
Let Mn=max(a1a2+a2a3+⋯+an−1an+ana1). Note that Mn of a permutation is always the same as the ones of its cyclic permutations. Observe the amount of change updating {1,2,⋯,n−1} to {1,2,⋯,n}. When n is inserted between ak and ak+1, the amount of change is nak+nak+1−akak+1=nak+nak+1−akak+1+n2−n2=n2−{n2−n(ak+ak+1)+akak+1}=n2−(n−ak)(n−ak+1)≤n2−{n−(n−1)}{n−(n−2)}=n2−2
where 1≤k≤n−1. Although k+1 must be 1 when k=n−1, the above inequality is always valid since the permutation can be cyclic keeping Mn−1. So, the maximum amount of change is n2−2, which means Mn−Mn−1=n2−2. Also, this only happens when n is inserted between n−1 and n−2 in a situation where n−1 and n−2 are adjacent. From M2=a1a2+a2a1=4, M3M4Mn⟹Mn=M2+32−2=M3+42−2⋮=Mn−1+n2−2=M2+(32−2)+(42−2)+⋯+(n2−2)=4+(12+22+⋯+n2)−(12+22)−2(n−2)=(12+22+⋯+n2)−2n+3=6n(n+1)(2n+1)−2n+3(n≥2)
Enumerating Mn starting from n=3, M3M4M5Mn⟸1,3,2⟸1,3,4,2⟸1,3,5,4,2⋮=⎩⎨⎧1,3,⋯,n−3,n−1,n,n−2,⋯,4,2(n is even)1,3,⋯,n−2,n,n−1,n−3,⋯,4,2(n is odd)
Keep going!Keep going ×2!Give me more!Thank you, thank youFar too kind!Never gonna give me up?Never gonna let me down?Turn around and desert me!You're an addict!Son of a clapper!No wayGo back to work!This is getting out of handUnbelievablePREPOSTEROUSI N S A N I T YFEED ME A STRAY CAT