Crazy Tea Party
Time Limit: 1000MS |
|
Memory Limit: 10000K |
|
|
|
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 re Quired for all participants to sit in reverse order.
Sample Input
3456
Sample Output
246
Source
Southeastern Europe 2003
test instructions is simple, n individuals (1~n) sit on a table, each minute adjacent two people can exchange positions, for a minimum of a few minutes to make the order into reverse;
Seemingly ordinary ideas no breakthrough, simple to find the law can not find, but we may find that the title only requires the final order for reverse, but not the position can not be changed; if there are 5 people: 1 2 3 4 5; After the transformation is 3 2 1 5 4; You see, the result is in reverse order. The position of the person starting with the number 1 can be arbitrary, but once the position on both sides of the 1 position is determined, if the last 1 is in the position numbered K, then the last K is at the beginning of the position numbered 1, the order becomes: K~1, n~k+1;
We know a bubble sort needs to change N (n-1)/2; Here it can be divided into two parts, 1~k transformation, need to transform K* (k-1)/2, the remaining number of n-k need to transform (n-k) * (n-k-1)/2; Total k* (k-1 )/2+(n-k) * (n-k-1)/2, decomposition into: k^2-nk+ (n*n-n)/2;n for the number of known, the minimum value of the two-tuple equation (4ac-b^2)/4a; The answer is n/2* (n/2-1); But for the odd and even unknown of N, The idea has been provided to this, the odd and even problem should not be difficult to solve; look at the code:
#include <cstdio> #include <cmath> #include <algorithm> #include <iostream> #include < Cstring>using namespace Std;int Main () { int t,n; scanf ("%d", &t); while (t--) { scanf ("%d", &n); if (n%2==0) printf ("%d\n", n/2* (n/2-1)); else printf ("%d\n", n/2* ((n+1)/2-1)); } return 0;}
Poj-crazy Tea Party, a very good math problem ~ ~ ~