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

Bubble Sort

Problem 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.

 
题意:求出1-n里面各个数在冒泡排序里面可以达到的最右距离和最左距离之差是多少。
 #include <cstring>
#include <cstdio>
#include <algorithm>
#include <iostream>
#include <cmath>
using namespace std;
#define N 100005
/*
树状数组
从右边往左扫看右边有多少个数是小于当前位置的数的,
然后可以达到的最右距离就是最初始的位置加上小于它的数的数量
可以达到的最左距离是min(最初始的位置,i)
所以最右减去最左就是i的答案了.
*/
int bit[N];
int num[N], tmp[N], r[N]; int lowbit(int x)
{
return x & (-x);
} void update(int x)
{
while(x <= N) {
bit[x] += ;
x += lowbit(x);
}
} int query(int x)
{
int ans = ;
while(x) {
ans += bit[x];
x -= lowbit(x);
}
return ans;
} int main()
{
int t;
scanf("%d", &t);
for(int cas = ; cas <= t; cas++) {
memset(bit, , sizeof(bit));
int n;
scanf("%d", &n);
for(int i = ; i <= n; i++){
scanf("%d", num+i);
tmp[num[i]] = i;
}
for(int i = n; i > ; i--) {
update(num[i]);
r[num[i]] = query(num[i] - );
} printf("Case #%d: ", cas);
for(int i = ; i <= n; i++) {
if(i != n) printf("%d ", abs(tmp[i]+r[i] - min(tmp[i], i)));
else printf("%d\n", abs(tmp[i]+r[i] - min(tmp[i], i)));
}
}
return ;
}

HDU 5775:Bubble Sort(树状数组)的更多相关文章

  1. hdu 5775 Bubble Sort 树状数组

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

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

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

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

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

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

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

  5. HDU 5775 Bubble Sort (线段树)

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

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

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

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

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

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

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

  9. hdu_5775_Bubble Sort(树状数组)

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

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

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

随机推荐

  1. Vhost Architecture

    在前面的文章中在介绍virtio机制中,能够看到在通常的应用中一般使用QEMU用户态程序来模拟I/O訪问,而Guest中的数据要通过Guest到Host Userspace的第一次拷贝,再经过Host ...

  2. AngularJS 页面计算

    <div ng-app="multipliedCaculateApp" ng-controller="multipliedCaculateController as ...

  3. 【转载】MySQL Replication 环境安装与配置

    安装[root@msr01 ~]# yum install mysql-serverInstalled:mysql-server.x86_64 0:5.1.73-3.el6_5 Dependency ...

  4. 【已解决】Android Studio下,gradle project sync failed 错误

    原文:[已解决]Android Studio下,gradle project sync failed 错误 Android studio下突然报错 gradle project sync failed ...

  5. 【WPF】SnapsToDevicePixels与UseLayoutRounding二者到底有什么区别?供参考

    原文:[WPF]SnapsToDevicePixels与UseLayoutRounding二者到底有什么区别?供参考 MSDN上解释了一大堆,二者对比来看,并不能发现什么明显的区别,微软爸爸也不知道多 ...

  6. 自绘LISTVIEW的滚动条(Delphi实现)

    因项目需要准备对LISTVIEW的滚动条进行自绘.于是在网上搜了一下,问题没解决,却搜出一篇令人不愉快的帖子 .确实,那时候实力是不够的,但现在应该是没问题了,为这个目的才不断磨练自己的. LISTV ...

  7. UWP开发-获取设备唯一ID

    EasClientDeviceInformation deviceInfo = new EasClientDeviceInformation(); this.showDeviceInfo.Items. ...

  8. linux下编译qt5.6.0静态库(使用./configure --help来看看都有哪些参数。超详细,有每一个模块的说明。如果改变了安装的目录,需要到安装目录下的bin目录下创建文件qt.conf)(乌合之众)good

    linux下编译qt5.6.0静态库 linux下编译qt5.6.0静态库 configure生成makefile 安装选项 Configure选项 第三方库: 附加选项: QNX/Blackberr ...

  9. Delphi 使用双缓冲解决图片切换时的闪烁问题 good

    var TempCanvas: TCanvas; BufDC: HDC; BufBitmap: HBITMAP; begin // 创建一个与显示设备兼容的内存设备 BufDC := CreateCo ...

  10. Linux编辑器Vim和Emacs入门

    sudo 命令 debian系统没有自带,需要安装: apt-get install sudo 安装位置为 /usr/bin/sudo,对应配置文件为 /etc/sudoers sudoers授权格式 ...