Description
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
参考别人的
把1 2 3 4 5换成5 4 3 2 1或 3 2 1 5 4都满足题意;
即把其当作一个环处理,所以进行分段】
当n为偶数 每份n/2个 花费时间(n/2)*(n/2-1)/2
当n为奇数,一份n/2,另一份n+1/2,代入n*(n-1)/2即可;
处理一下就得到下面公式
#include<stdio.h> int main() { int n,m; scanf("%d",&n); while(n--) { scanf("%d",&m); printf("%d ",m/2*(m/2-1)/2+(m+1)/2*((m+1)/2-1)/2); } return 0; }