"Displacement, reasoning" UVa 1315-creaz Tea Party

Source: Internet
Author: User

D secription

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

Test instructions: There are n people sitting in a circle. Adjacent can be exchanged, ask at least how many times to reach reverse order, that is, left and right adjacent to the number exchange.

Analysis: From the middle part, it is obvious 1~ (N/2) to the left interchange, (N/2) ~n to the right exchange. Like what:
n = 6 o'clock:
1 2 3 | 4 5 6
1 3 2 | 5 4 6
3 1 2 | 5 6 4 (above 3, 4 move to both sides respectively)
3 2 1 | 6 5 4 (2,5 move to both sides)
  
The above completed reverse order transformation;

n = 7 o'clock:
1 2 3 4 | 5 6 7
...
4 1 2 3 | 6 7 5
...
4 3 1 2 | 7 6 5
4 3 2 1 | 7 6 5
So each number needs to be exchanged for (even) 0, ..., (N/2)-1, (N/2) -1,...,2,1,0
(odd) 0, ..., (N/2), (N/2) -1,...,2,1,0
Sum can be.

"Code": The code may be slightly cumbersome. I can simplify it.
1#include <iostream>2#include <cstdio>3 using namespacestd;4 intMain ()5 {6     intT scanf"%d", &T);7      while(t--)8     {9         intN, ans =0; scanf"%d", &n);Ten         if(n%2) One         { A             intnum = n/2; -Ans = (1+ (num-1)) * (num-1); -Ans + =num; the         } -         Else -         { -             intnum = n/2; +Ans = (1+ (num-1)) * (num-1); -         } +cout << ans <<Endl; A     } at     return 0; -}



"Displacement, reasoning" UVa 1315-creaz Tea Party

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.