链接:http://acm.hdu.edu.cn/showproblem.php?pid=1214
把每个桌子分成两半,分开考虑。
比如,圆桌7个人的话,就分成一个4人的,一个3人的单向序列...分别算出把每个序列反向排序需要的最少交换次数(6,3)。。两个的和即为结果9 ,,
再求每个长度为 i 的单向序列的时候,求出把第一个移动到最后一个需要的交换次数,再加上 把长度为(i-1)的单向序列反向需要的移动次数即可
#include <iostream> using namespace std; int a[20000]; int main() { int n; int i; a[2]=1; for(i=3;i<20000;i++) a[i]= (i-1) + a[i-1]; while(cin>>n) { if(n%2==0) cout<<2*a[n/2]<<endl; else cout<<a[(n-1)/2]+a[(n+1)/2]<<endl; } return 0; }