UVALive 2756 Crazy tea party

这道题目考察的n个不同的数环形排列,每次相邻两个数交换位置,这样由正序转变成逆序所需操作的最小次数t。

公式:环形排列:t= n/2*(n/2 - 1)/2 + (n+1)/2* ((n+1)/2 - 1)/2

在这里在补充下线性排列的公式:t=n*(n-1)/2

 1 #include <iostream>
 2 using namespace std;
 3
 4 int main ()
 5 {
 6     int t;
 7     cin >> t;
 8     while ( t-- )
 9     {
10         int n;
11         cin >> n;
12         int m = ( n / 2 ) * ( n / 2 - 1 ) / 2 + ( ( n + 1 ) / 2 ) * ( ( n + 1 ) / 2 - 1 ) / 2;
13         cout << m << endl;
14     }
15     return 0;
16 }

原文地址:https://www.cnblogs.com/ljy08163268/p/11706005.html

时间: 2024-10-05 05:04:43

UVALive 2756 Crazy tea party的相关文章

《Crazy tea party》

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

FZOJ 1157 Crazy Tea Party

OJ题目:click here~~ 题目分析:1--n按顺序围成一个圈,1与n相邻.交换相邻两个数算1步.至少需要多少步,得到一个逆方向的1--n的圈. 分两半,使用冒泡排序,排成逆序的交换次数之和即为结果. AC_CODE int f(int n){ return n*(n - 1)/2; } int main(){ int n , t; cin >> t; while(t--){ cin >> n; int ans = 0; if(n&1) ans = f(n/2) +

uvalive 2756 环形排列颠倒的次数

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,

uva1315 Crazy tea party(找规律)

题意就是说把顺时针排的1到n换成逆时针排的需要的最少交换步数. 如果是线形的一串数,需要的交换次数就是个冒泡排序的交换次数:n*(n-1)/2,或者用a[i]=(i-1)+a[i-1]推出来. 对于环形,切成两个线形就行了,通过观察规律知:越接近平均切开越好. #include<iostream> #include<cstdio> #include<cstdlib> #include<cstring> #include<string> #incl

数论 UVALive 2756

这道题目考察的n个不同的数环形排列,每次相邻两个数交换位置,这样由正序转变成逆序所需操作的最小次数t. 公式:环形排列:t= n/2*(n/2 - 1)/2 + (n+1)/2* ((n+1)/2 - 1)/2 在这里在补充下线性排列的公式:t=n*(n-1)/2 #include<iostream>using namespace std;int main(){ int t; cin>>t; while(t--) { int n,ans; cin>>n; ans=(n/

UVA1315 - Crazy tea party(推导)

题目链接 题意:n个人坐成环形,相邻的两个可以交换位置,求最少交换次数使得序列相反. 思路:类似与冒泡排序,可以将环形序列拆成两个序列,分别进行冒泡.当n为奇数时,分为n/2与n/2 + 1,所以ans = (n / 2) * (n / 2 - 1) / 2 + (n / 2) * (n / 2 + 1) / 2,当n为偶数时,分为两个n/2, 所以ans = (n / 2) * (n / 2 - 1). 代码: #include <iostream> #include <cstdio&

【置换,推理】UVa 1315 - Creaz tea party

Dsecription 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 be

POJ 题目1455Crazy tea party(数学)

Crazy tea party Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 7164   Accepted: 4864 Description n participants of << crazy tea party >> sit around the table. Each minute one pair of neighbors can change their places. Find the mi

POJ-Crazy tea party,很好的一道数学题~~~

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 f