CSA Round #50 (Div. 2 only) Min Swaps(模拟)

传送门

题意

给出一个排列,定义\(value为\sum_{i=1}^{n-1}abs(f[i+1]-f[i])\)
\(swap(a[i],a[j])(i≠j)为一次交换\),询问最少的交换次数使得value最大

分析

如果f[i+1]>f[i],答案就+f[i+1]-f[i];
如果f[i+1]<f[i],答案就+f[i]-f[i+1];
那么我们可以找到一个pretty good solution
定义 L 为 f[i]<=n/2, R 为 f[i]>n/2,
得到L R L R L R L R
或 R L R L R L R L
可以发现2~n-1的R加了2次,L减了2次,1和n加或减1次,那么必定是
将n/2和n/2+1放在(1和n)或(n和1)的位置

  • If 2 is not first, bring it there
  • If 3 is not the last, bring it there
  • Swap every pair of 0 and 1 that are not on their positions
posted @ 2017-09-28 10:55  遗风忘语  阅读(132)  评论(0编辑  收藏  举报