Crazy Tea Party
Time Limit: 1000MS |
|
Memory Limit: 10000K |
Total Submissions: 7164 |
|
Accepted: 4864 |
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-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 required for all participants to sit in reverse order.
Sample Input
3
4
5
6
Sample Output
2
4
6
Source
Southeastern Europe 2003
Test instructions: n individuals surround a round table, each person can exchange positions next to each other, ask the last number of times after everyone's left and right opposite
Idea: http://m.blog.csdn.net/blog/u010893129/21127153
It is well known that the assumption is in the case of a row:
1 2 3 4 5 6 7
If the position is swapped:
7 6 5 4 3 2 1
Need to pass N (n-1)/2 times Exchange (bubbling method for Exchange)
Now the situation is a ring, how can I split the ring into two linear, then exchange, and then splicing together.
Examples are as follows:
1 2 3 4 5 6 7 (7 back 1)
If it is divided into 1 2 3, 4 5 6 72 parts, respectively, and then connected, is not satisfied with the condition.
After Exchange 3 2 1, 7 6 5 4
After the connection is: 3 2 1 7 6 5 4, in the loop, in fact, the position after the change of 7654321, is satisfied with the condition.
So the problem is solved:
Now the problem is that the length of the ring is assumed to be N, and if a screenshot is made, the number of exchanges is minimal.
Suppose that the ring of N is divided into a length of k, and the other length is a n-k segment.
Then the number of exchanges required is:
k* (k-1)/2
(n-k) * (n-k-1)/2
The sum is the total number of times that need to be exchanged.
The topic requires the least number of exchanges.
Then it's begging.
k* (k-1)/2+
(n-k) * (n-k-1)/2 minimum value.
It is easy to base the formula on the two-time function when the extremum is taken N/2. AC Code
#include <stdio.h>
#include <string.h>
int main ()
{
int t;
scanf ("%d", &t);
while (t--)
{
int n;
scanf ("%d", &n);
int K=N/2;
printf ("%d\n", (k* (k-1))/2+ (n-k) * (n-k-1)/2);
}
}