题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1214
中文题目。。。
因为是圆形的,假如有5个人,初始是1,2,3,4,5;那么,可以排成5,4,3,2,1,也可以排成3,2,1,5,4;
循环的队列是不用区分第一个在哪里的。最简单的方法是把队列分成两个队列,这两个队列长度最接近。
假设一个队列有n个人,那么逆序需要交换n*(n-1)/2次
代码如下:
///2014.4.1
///hdu1214
#include <iostream>
#include <cstdio>
using namespace std;
int main()
{
// freopen("in","r",stdin);
// freopen("out","w",stdout);
int n;
while( cin>>n ){
int n1,n2;
int num = 0;
n1 = n/2;
n2 = n-n1;
num += n1*(n1-1)/2 + n2*(n2-1)/2;
cout<<num<<endl;
}
return 0;
}