UVA12004 Bubble Sort

Check the following code which counts the number of swaps of bubble sort.

int findSwaps( int n, int a[] )
{
int count = 0, i, j, temp, b[100000];
for( i = 0; i < n; i++ ) {
b[i] = a[i];
}
for( i = 0; i < n; i++ ) {
for( j = 0; j < n - 1; j++ ) {
if( b[j] > b[j+1] ) {
temp = b[j];
b[j] = b[j+1];
b[j+1] = temp;
count++;
}
}
}
return count;
}

You have to find the average value of ’count’ in the given code if we run findSwaps() infinitely many times using constant ’n’ and each time some random integers (from 1 to n) are given in array a[]. You can assume that the input integers in array a[] are distinct.

Input

Input starts with an integer T (≤ 1000), denoting the number of test cases. Each test case contains an integer n (1 ≤ n ≤ 105) in a single line.

Output

For each case, print the case number and the desired result. If the result is an integer, print it. Otherwise print it in ‘p/q’ form, where p and q are relative prime.

Sample Input

2
1
2

Sample Output

Case 1: 0
Case 2: 1/2

思路

一句话题意:求长度为n的排列的期望逆序对数。

很简单,\(f(n)=f(n-1)+\frac{n-1}2=\frac{n\times(n-1)}4,f(1)=0\)。

为什么呢?假设把\(n\)插入长度\((n-1)\)的排列,有\(n\)种方法。期望增加的逆序对数就是\(\frac{1+2+...n-1}n=\frac{n\times (n-1)}{2n}=\frac{n-1}2\)

所以\(f(n)=f(n-1)+\frac{n-1}2\)

很简单吧?别忘了开long long

代码

#include<bits/stdc++.h>
using namespace std;
#define LL long long int T, i;
LL n; int main(){
scanf( "%d", &T );
for ( int i = 1; i <= T; ++i ){
scanf( "%lld", &n );
n = n * ( n - 1 ) / 2;
if ( n & 1 ) printf( "Case %d: %lld/2\n", i, n );
else printf( "Case %d: %lld\n", i, n / 2 );
}
return 0;
}

「UVA12004」 Bubble Sort 解题报告的更多相关文章

  1. 「SP25784」BUBBLESORT - Bubble Sort 解题报告

    SP25784 BUBBLESORT - Bubble Sort 题目描述 One of the simplest sorting algorithms, the Bubble Sort, can b ...

  2. 「ZJOI2016」大森林 解题报告

    「ZJOI2016」大森林 神仙题... 很显然线段树搞不了 考虑离线操作 我们只搞一颗树,从位置1一直往后移动,然后维护它的形态试试 显然操作0,1都可以拆成差分的形式,就是加入和删除 因为保证了操 ...

  3. 「SCOI2016」背单词 解题报告

    「SCOI2016」背单词 出题人sb 题意有毒 大概是告诉你,你给一堆n个单词安排顺序 如果当前位置为x 当前单词的后缀没在这堆单词出现过,代价x 这里的后缀是原意,但不算自己,举个例子比如abc的 ...

  4. 「NOI2015」寿司晚宴 解题报告

    「NOI2015」寿司晚宴 这个题思路其实挺自然的,但是我太傻了...最开始想着钦定一些,结果发现假了.. 首先一个比较套路的事情是状压前8个质数,后面的只会在一个数出现一次的再想办法就好. 然后发现 ...

  5. 「SCOI2015」国旗计划 解题报告

    「SCOI2015」国旗计划 蛮有趣的一个题 注意到区间互不交错,那么如果我们已经钦定了一个区间,它选择的下一个区间是唯一的,就是和它有交且右端点在最右边的,这个可以单调队列预处理一下 然后往后面跳拿 ...

  6. 「SDOI2014」向量集 解题报告

    「SDOI2014」向量集 维护一个向量集合,在线支持以下操作: A x y :加入向量 \((x, y)\): Q x y l r:询问第 \(L\) 个到第 \(R\) 个加入的向量与向量 \(( ...

  7. 「FJOI2016」神秘数 解题报告

    「FJOI2016」神秘数 这题不sb,我挺sb的... 我连不带区间的都不会哇 考虑给你一个整数集,如何求这个神秘数 这有点像一个01背包,复杂度和值域有关.但是你发现01背包可以求出更多的东西,就 ...

  8. 「JLOI2015」骗我呢 解题报告?

    「JLOI2015」骗我呢 这什么神仙题 \[\color{purple}{Link}\] 可以学到的东西 对越过直线的东西翻折进行容斥 之类的..吧? Code: #include <cstd ...

  9. 「JLOI2015」城池攻占 解题报告

    「JLOI2015」城池攻占 注意到任意两个人的战斗力相对大小的不变的 可以离线的把所有人赛到初始点的堆里 然后做启发式合并就可以了 Code: #include <cstdio> #in ...

随机推荐

  1. laravel 5 自定义全局函数,怎么弄呢?

    在app/Helpers/(目录可以自己随便来) 下新建一个文件 functions.php 在functions.php 中加入这个方法 然后在 bootstrap/autoload.php 中添加 ...

  2. Jquery常用方法汇总(转)

    https://blog.csdn.net/lucky___star/article/details/87883888

  3. @noi.ac - 506@ 强连通分量

    目录 @description@ @solution@ @accepted code@ @details@ @description@ 有一天你学了一个叫能求出有向图中所有的强连通分量的算法,你决定将 ...

  4. Project Euler Problem 15-Lattice paths

    组合数,2n中选n个.向右走有n步,向下走有n步.共2n步.有n步是向右走的,计算向右走的这n步的所有情况,即C(2n,n). 或者,每一步,只能从右边或者上边走过来,只有这两种情况,即step[i] ...

  5. 2019-11-17-dotnet-C#-获取本机外网-IP-地址

    title author date CreateTime categories dotnet C# 获取本机外网 IP 地址 lindexi 2019-11-17 16:38:10 +0800 201 ...

  6. laravel5 怎么获取数组形式的数据

    当构建 JSON API 时,您可能常常需要把模型和关联对象转换成数组或JSON.所以Eloquent里已经包含了这些方法.要把模型和已载入的关联对象转成数组,可以使用 toArray方法: $use ...

  7. H3C 帧中继配置示例

  8. [转]【译】.NET Core 3.0 中的新变化

    .NET Core 3.0 是 .NET Core 平台的下一主要版本.本文回顾了 .Net Core 发展历史,并展示了它是如何从基本支持 Web 和数据工作负载的版本 1,发展成为能够运行 Web ...

  9. Mysql5.5升级到5.7的过程已经踩到的坑

    https://blog.csdn.net/u014534986/article/details/79699750 故事是这样子的,我们公司有几台老的mysql版本是5.5的,最近项目做了一些升级增加 ...

  10. springboot2.x整合JPA

    项目结构 pom <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www. ...