zoukankan      html  css  js  c++  java
  • 《Crazy tea party》

    Description

    Download as PDF
     

    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 for all participants to sit in reverse order (so that left neighbors would become 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
    

     题意:n个人顺时针围成一个圈;

       欲将其按逆时针围成圈;

       每次只能移动一对相邻的人;

       问至少需要移动多少次;

    题解:由于这是一个环;

       将其折中取半来移动;

       最后便能使步骤最少;

    代码:

      

     1 #include <stdio.h>
     2 #include <iostream>
     3 #include <cmath>
     4 #include <algorithm>
     5 #include <cstring>
     6 
     7 using namespace std;
     8 
     9 int main()
    10 {
    11    // freopen("ACM.txt","r",stdin);
    12     int t,n;
    13     cin>>t;
    14     while(t--)
    15     {
    16         int ans=0;
    17         cin>>n;
    18         int x=n/2;
    19         for(int i=1;i<=x;i++)
    20         {
    21             ans+=x-i;
    22         }
    23         for(int i=x+1;i<=n;i++)
    24         {
    25             ans+=n-i;
    26         }
    27         cout<<ans<<endl;
    28     }
    29 }
    View Code
     
  • 相关阅读:
    mysql索引的选择
    A、B两个线程交替打印1 -- 100
    dubbo服务暴露
    1
    java无锁化编程一:目录
    如何实现自定义同步组件
    服务器的性能监控
    关于shiro安全框架实现同一用户同一时刻仅可在一个地址登录的技术实现
    关于Spring的Quartz定时器设定
    JAVA之Mybatis基础入门二 -- 新增、更新、删除
  • 原文地址:https://www.cnblogs.com/M-D-LUFFI/p/4084312.html
Copyright © 2011-2022 走看看