n participants of "crazy tea party" sit around the table. Each minute one pair of neighbors can change their places. Find the minimum time (in minutes) required for all participants to sit in reverse order (so that left neighbors would become right, and right - left).
Input. The first line is the amount of tests. Each next line contains one integer n (1 <= n <= 32767) - the amount of crazy tea participants.
Output. For each number n of participants to crazy tea party print on the standard output, on a separate line, the minimum time required for all participants to sit in reverse order.
Sample Input
3
4
5
6
Sample Output
2
4
6
然后考虑将环转换为非环。即(k k-1 .. 1) (n .. k+1)例如:1 2 3 4 5 6的目标可以是 6 5 4 3 2 1,也可以是 (4 3 2 1 )(6 5)。
相当于进行2个冒泡排序,2个冒泡排序的n相等时,和最小
int main()
{
int M;
scanf("%d",&M);
while(M--){
int n,k;
scanf("%d",&n);
if(n%2==0)
k=(n/2-1)*(n/2);
else
k=((n-1)/2)*((n-1)/2-1)/2+((n+1)/2)*((n+1)/2-1)/2;
printf("%d\n",k);
}
return 0;
}
本文探讨了疯狂茶会问题:一群人围坐一圈,通过相邻两人互换位置的方式,求所有人位置反转所需的最短时间。文章给出了算法实现,并解释了如何通过两个冒泡排序过程来解决此问题。
322

被折叠的 条评论
为什么被折叠?



