Uva 1315-crazy Tea Party Problem Solving report (rule-finding)

Source: Internet
Author: User
Tags time limit
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
        ();
}


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.