「UVA12004」 Bubble Sort 解题报告
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 解题报告的更多相关文章
- 「SP25784」BUBBLESORT - Bubble Sort 解题报告
SP25784 BUBBLESORT - Bubble Sort 题目描述 One of the simplest sorting algorithms, the Bubble Sort, can b ...
- 「ZJOI2016」大森林 解题报告
「ZJOI2016」大森林 神仙题... 很显然线段树搞不了 考虑离线操作 我们只搞一颗树,从位置1一直往后移动,然后维护它的形态试试 显然操作0,1都可以拆成差分的形式,就是加入和删除 因为保证了操 ...
- 「SCOI2016」背单词 解题报告
「SCOI2016」背单词 出题人sb 题意有毒 大概是告诉你,你给一堆n个单词安排顺序 如果当前位置为x 当前单词的后缀没在这堆单词出现过,代价x 这里的后缀是原意,但不算自己,举个例子比如abc的 ...
- 「NOI2015」寿司晚宴 解题报告
「NOI2015」寿司晚宴 这个题思路其实挺自然的,但是我太傻了...最开始想着钦定一些,结果发现假了.. 首先一个比较套路的事情是状压前8个质数,后面的只会在一个数出现一次的再想办法就好. 然后发现 ...
- 「SCOI2015」国旗计划 解题报告
「SCOI2015」国旗计划 蛮有趣的一个题 注意到区间互不交错,那么如果我们已经钦定了一个区间,它选择的下一个区间是唯一的,就是和它有交且右端点在最右边的,这个可以单调队列预处理一下 然后往后面跳拿 ...
- 「SDOI2014」向量集 解题报告
「SDOI2014」向量集 维护一个向量集合,在线支持以下操作: A x y :加入向量 \((x, y)\): Q x y l r:询问第 \(L\) 个到第 \(R\) 个加入的向量与向量 \(( ...
- 「FJOI2016」神秘数 解题报告
「FJOI2016」神秘数 这题不sb,我挺sb的... 我连不带区间的都不会哇 考虑给你一个整数集,如何求这个神秘数 这有点像一个01背包,复杂度和值域有关.但是你发现01背包可以求出更多的东西,就 ...
- 「JLOI2015」骗我呢 解题报告?
「JLOI2015」骗我呢 这什么神仙题 \[\color{purple}{Link}\] 可以学到的东西 对越过直线的东西翻折进行容斥 之类的..吧? Code: #include <cstd ...
- 「JLOI2015」城池攻占 解题报告
「JLOI2015」城池攻占 注意到任意两个人的战斗力相对大小的不变的 可以离线的把所有人赛到初始点的堆里 然后做启发式合并就可以了 Code: #include <cstdio> #in ...
随机推荐
- @codechef - TREEPATH@ Decompose the Tree
目录 @description@ @solution@ @accepted code@ @details@ @description@ 给定一棵无根树,每个节点上都写了一个整数. 你的任务就是统计有多 ...
- QQ第三方登录报错error=-1
qq 第三方登录报错error=-1 再次实例化qc类.
- NodeMCU快速上云集锦
摘要: 上至智慧园区项目,下至 `Hello world`,基于开源 MQTT 协议,阿里云 IoT 用户使用 NodeMCU 完成了不少 IoT 项目,以下为项目和教程集锦,欢迎大家一起上手试试. ...
- 神经网络入门——8XOR感知器
XOR 感知器 XOR 感知器就是一个这样的逻辑门:输入相同返回 0 ,输入不同返回 1.与之前的感知器不同,这里并不是线性可分的.要处理这类较为复杂的问题,我们需要把感知器连接起来. 我们用 ...
- 给radio添加点击事件
1.单独给每个radio添加点击事件 <fieldset id="form-gra-time"> <legend>请选择日期粒度:</legend&g ...
- KMPnext数组运用、最小循环节问题
http://www.cnblogs.com/jackge/archive/2013/01/05/2846006.html http://www.cnblogs.com/wuyiqi/archive/ ...
- 【codeforces 766A】Mahmoud and Longest Uncommon Subsequence
time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
- 查看laravel版本
方法1: 使用php artisan --version ,只要能看懂这个命令的人一定已经具有初步的Laravel知识.再介绍一种不需要命令,直接去文件中去查看的方法. 方法2: 在项目文件中找ven ...
- ZOJ 1276 "Optimal Array Multiplication Sequence"(最优矩阵链乘问题+区间DP)
传送门 •题意 矩阵 A(n×m) 和矩阵 B(m×k) 相乘,共做 n×m×k 次乘法运算: 给你 n 个矩阵,求这 n 个矩阵的最优结合方式,使得做的总乘法运算次数最少: •题解 定义dp(i,j ...
- H3C Hosts文件