Bubble Sort

题目连接:

http://acm.hdu.edu.cn/showproblem.php?pid=5775

Description

P is a permutation of the integers from 1 to N(index starting from 1).

Here is the code of Bubble Sort in C++.

for(int i=1;i<=N;++i)

for(int j=N,t;j>i;—j)

if(P[j-1] > P[j])

t=P[j],P[j]=P[j-1],P[j-1]=t;

After the sort, the array is in increasing order. ?? wants to know the absolute values of difference of rightmost place and leftmost place for every number it reached.

Input

The first line of the input gives the number of test cases T; T test cases follow.

Each consists of one line with one integer N, followed by another line with a permutation of the integers from 1 to N, inclusive.

limits

T <= 20

1 <= N <= 100000

N is larger than 10000 in only one case.

Output

For each test case output “Case #x: y1 y2 … yN” (without quotes), where x is the test case number (starting from 1), and yi is the difference of rightmost place and leftmost place of number i.

Sample Input

2

3

3 1 2

3

1 2 3

Sample Output

Case #1: 1 1 2

Case #2: 0 0 0

Hint

In first case, (3, 1, 2) -> (3, 1, 2) -> (1, 3, 2) -> (1, 2, 3)

the leftmost place and rightmost place of 1 is 1 and 2, 2 is 2 and 3, 3 is 1 and 3

In second case, the array has already in increasing order. So the answer of every number is 0.

Hint

题意

给你一个1-n的排列,然后让你跑冒牌排序,问你这个数曾经到过的最左边和最右边的位置的差是多少

题解:

考虑一个位置上的数字c在冒泡排序过程的变化情况。c会被其后面比c小的数字各交换一次,之后c就会只向前移动。数组从右向左扫,树状数组维护一下得到每个值右边有多少个比其小的值,加上原位置得到最右位置,最左位置为初始位置和最终位置的最小值。

时间复杂度\(O(n\ lg\ n)\)

代码

#include<bits/stdc++.h>
using namespace std;
const int maxn = 1e5+7;
int cas = 0;
int a[maxn],b[maxn];
int ans[maxn];
int l[maxn],r[maxn];
int d[maxn];
int lowbit(int x){
return x&(-x);
}
void update(int x){
for(int i=x;i<maxn;i+=lowbit(i)){
d[i]++;
}
}
int get(int x){
int ans = 0;
for(int i=x;i;i-=lowbit(i))
ans+=d[i];
return ans;
}
void solve(){
memset(d,0,sizeof(d));
vector<pair<int,int> >V;
int n;scanf("%d",&n);
for(int i=1;i<=n;i++){
scanf("%d",&a[i]);
V.push_back(make_pair(a[i],i));
}
sort(V.begin(),V.end());
for(int i=0;i<V.size();i++){
ans[i+1]=max(V[i].second,V[i].second+get(maxn-1)-get(V[i].second))-min(V[i].first,V[i].second);
update(V[i].second);
}
printf("Case #%d:",++cas);
for(int i=1;i<=n;i++){
printf(" %d",ans[i]);
}
printf("\n");
} int main(){
int t;
scanf("%d",&t);
while(t--)solve();
return 0;
}

hdu 5775 Bubble Sort 树状数组的更多相关文章

  1. 2016 Multi-University Training Contest 4 Bubble Sort(树状数组模板)

    Bubble Sort 题意: 给你一个1~n的排列,问冒泡排序过程中,数字i(1<=i<=n)所到达的最左位置与最右位置的差值的绝对值是多少 题解: 数字i多能到达的最左位置为min(s ...

  2. HDU 5775 L - Bubble Sort 树状数组

    给定一段冒泡排序的代码,要求输出每个数字能到达的最右边的位置和最左边的位置的差 因为那段冒泡排序的代码是每次选取一个最小的数,放在左边的,所以,每个数最多能到达右边的位置应该是起始位置i+右边有多少个 ...

  3. HDU 5775 Bubble Sort(冒泡排序)

    p.MsoNormal { margin: 0pt; margin-bottom: .0001pt; text-align: justify; font-family: Calibri; font-s ...

  4. HDU 5775 Bubble Sort (线段树)

    Bubble Sort 题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5775 Description P is a permutation of t ...

  5. HDU 3333 | Codeforces 703D 树状数组、离散化

    HDU 3333:http://acm.hdu.edu.cn/showproblem.php?pid=3333 这两个题是类似的,都是离线处理查询,对每次查询的区间的右端点进行排序.这里我们需要离散化 ...

  6. HDU 3333 - Turing Tree (树状数组+离线处理+哈希+贪心)

    题意:给一个数组,每次查询输出区间内不重复数字的和. 这是3xian教主的题. 用前缀和的思想可以轻易求得区间的和,但是对于重复数字这点很难处理.在线很难下手,考虑离线处理. 将所有查询区间从右端点由 ...

  7. HDU 3333 Turing Tree (树状数组)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3333 题意就是询问区间不同数字的和. 比较经典的树状数组应用. //#pragma comment(l ...

  8. hdu_5775_Bubble Sort(树状数组)

    题目链接:hdu_5775_Bubble Sort 题意: 让你找每一个数在冒泡排序中最右边和最左边的位置的差值 题解: 还是官方题解,讲的已经很清楚了 1012 Bubble Sort 考虑一个位置 ...

  9. HDU 4325 Flowers(树状数组+离散化)

    http://acm.hdu.edu.cn/showproblem.php?pid=4325 题意:给出n个区间和m个询问,每个询问为一个x,问有多少个区间包含了x. 思路: 因为数据量比较多,所以需 ...

随机推荐

  1. Codeforces 295 B. Greg and Graph

    http://codeforces.com/problemset/problem/295/B 题意: 给定一个有边权的有向图.再给定一个1~n的排列. 按排列中的顺序依次删除点,问每次删除后,所有点对 ...

  2. Linux - awk 文本处理工具一

    AWK AWK是一个优良的文本处理工具,Linux及Unix环境中现有的功能最强大的数据处理引擎之一:awk经过改进生成的新的版本nawk,gawk,现在默认linux系统下日常使用的是gawk,用命 ...

  3. tf.range()函数

    range()函数用于创建数字序列变量,有以下两种形式: range(limit, delta=1, dtype=None, name='range') range(start, limit, del ...

  4. 运算符,比如+, -, >, <, 以及下标引用[start:end]等等,从根本上都是定义在类内部的方法。

    python解释器在碰到+号运算符时,会调用加号前面的对象的__add__方法 class a: def __add__(self,b): print "ghh" aa=a() a ...

  5. eclipse中可以导入其它工具编写的RobotFramework脚本吗?

    在Robotframework的官方网站中,提供了非常多的编辑RF的工具.比如Ride,eclipse,sublime,notepad++等. 网上查到的资料,大部分都是Ride这个编辑工具的使用.在 ...

  6. mybatis关联查询数据模型分析——(七)

    1.     数据模型分析思路 1.每张表记录的数据内容 分模块对每张表记录的内容进行熟悉,相当 于你学习系统 需求(功能)的过程. 2.每张表重要的字段设置 非空字段.外键字段 3.数据库级别表与表 ...

  7. 学习mysql replication

  8. MYSQL问题解决

    1. MySQL错误日志里出现: 140331 10:08:18 [ERROR] Error reading master configuration 140331 10:08:18 [ERROR] ...

  9. .NET下获取应用程序目录的一些方法

    今天在Console Application下搞了一个小功能,期间需要获取当前应用程序的根目录,试了很多方式,都不能直接获取到,没有像Server.MapPath()这类的方法来方便地使用. 下面列举 ...

  10. js async await 终极异步解决方案

    既然有了promise 为什么还要有async await ? 当然是promise 也不是完美的异步解决方案,而 async await 的写法看起来更加简单且容易理解. 回顾 Promise Pr ...