1315-crazy Tea Party
Time limit:3.000 seconds
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-participants to sit-reverse order (so, left neighbors would bec ome 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 re Quired for all participants to sit in reverse order. Sample Input
3
4
5
6
Sample Output
2
4
6
Problem Solving Report: Reverse the Order of n participants, exchanging only two adjacent persons at a time.
Analysis, 1 people and 2 people do not operate, 3 people when the 2, 3 Exchange order can be. 4 people will be 1, 2 interchange, 3,4 interchange.
So, every time we divide n individuals into two groups of N/2 and N-N/2, each group is reversed. A recursive look, the response to a sequence of M-length requires m* (M-1)/2 operations. So the code is as follows:
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
int cal (int n)
{
return n (n-1)/2;
}
void work ()
{
int n;
scanf ("%d", &n);
printf ("%d\n", Cal (N/2) +cal (N-N/2));
}
int main ()
{
int T;
scanf ("%d", &t);
while (t--) work
();
}