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. celery 计划任务使用

    流程: 用户提交任务 --- > Celery --- > Broker 中间商(可以是数据库,redis) ---> 最后让celery 中的 worker 执行任务 1 单独使用 ...

  2. @noi.ac - 171@ 立方体

    目录 @description@ @solution@ @accepted code@ @details@ @description@ TonyFang 打算送你一些立方体. 你需要在 [1, n] ...

  3. Pytorch实现MNIST(附SGD、Adam、AdaBound不同优化器下的训练比较) adabound实现

     学习工具最快的方法就是在使用的过程中学习,也就是在工作中(解决实际问题中)学习.文章结尾处附完整代码. 一.数据准备  在Pytorch中提供了MNIST的数据,因此我们只需要使用Pytorch提供 ...

  4. jq实现简单手风琴效果

    文章地址:https://www.cnblogs.com/sandraryan/ 利用slideUp slideDown动画 <!DOCTYPE html> <html lang=& ...

  5. HTML静态网页---标签

    一. 创建HTML: (一) body的属性: bgcolor 页面背景色 background   背景壁纸.图片 text   文字颜色 topmargin   上边距 leftmargin    ...

  6. hdu 4146 Flip Game

    Flip Game Time Limit: 15000/5000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)Total ...

  7. Nutch2.3 编译

    $ antBuildfile: build.xmlTrying to override old definition of task javac ivy-probe-antlib: ivy-downl ...

  8. Python--day40--全局解释器锁

    1,起一百个线程和起一百个进程所花的时间对比(开启效率的较量): import time from threading import Thread from multiprocessing impor ...

  9. CSS3 box-sizing 盒子布局

    在CSS中盒模型被分为两种,第一种是W3C的标准模型,第二种是IE怪异盒模型.不同之处在于后者的宽高定义的是可见元素框的尺寸,而不是元素框的内容区尺寸.目前对于浏览器大多数元素都是基于W3C标准的盒模 ...

  10. linux设备驱动文件结构

    struct file, 定义于 <linux/fs.h>, 是设备驱动中第二个最重要的数据结构. 注意 file 与用户空间程序的 FILE 指针没有任何关系. 一个 FILE 定义在 ...