Uva1315-crazy Tea Party (derivation)

Source: Internet
Author: User

Question Link


N people sit in a ring, and the adjacent two can exchange positions. The minimum number of exchanges leads to the opposite of the sequence.

Idea: similar to Bubble sorting, the circular sequence can be split into two sequences for bubble respectively. When N is an odd number, it is divided into n/2 and N/2 + 1, so ans = (n/2) * (N/2-1) /2 + (n/2) * (N/2 + 1)/2. When n is an even number, it is divided into two n/2, so ans = (n/2) * (N/2-1 ).

Code:

#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>using namespace std;int n;int main() {    int cas;    scanf("%d", &cas);    while (cas--) {        scanf("%d", &n);            if (n == 1 || n == 2)              printf("0\n");        else {            int ans;            if (n % 2 == 0) {                ans = (n / 2) * (n / 2 - 1);                printf("%d\n", ans);            }            else {                ans = (n / 2) * (n / 2 - 1) / 2 + (n / 2) * (n / 2 + 1) / 2;                printf("%d\n", ans);             }         }     }    return 0;}


Uva1315-crazy Tea Party (derivation)

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.