Bubble Sort

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 853    Accepted Submission(s): 504

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.

 
Author
FZU
 
Source
 

题意:冒泡排序中  求每一个位置的上的数 移动的最左端与最右端的距离差值

题解:因为在冒泡排序中,每次都将序列中最小的数向前移动,所以位置i上的数只会与在它之后的并小于它的数交换

树状数组倒序遍历求求小于i位置数的个数x  所有最右端为r=i+x;对于左边界l=min(原位置,升序排列之后的位置);

但是注意 此题中的n个数都是不同的

可以这么想,在一次冒泡过程中,只有当前未排序的最大元素才有可能向右移动,其余元素均会向左移动;

那么一个元素ai 的最左端位置为l= i-num(左端比ai大的数的个数) r=max(原位置,升序排列之后的位置)

两种思路都是可以的 代码是按照第二种写的的
 
另外,刚开始有一个错误的思路 
两次使用树状数组,记录ai左边大于ai的个数xl,ai右边小于的ai的个数xr
l=i-xl;  r=i+xr
hack 数据
5
5 4 2 3 1
 /******************************
code by drizzle
blog: www.cnblogs.com/hsd-/
^ ^ ^ ^
O O
******************************/
//#include<bits/stdc++.h>
#include<iostream>
#include<cstring>
#include<cstdio>
#include<map>
#include<algorithm>
#include<queue>
#include<cmath>
#define ll __int64
#define PI acos(-1.0)
#define mod 1000000007
using namespace std;
#define NN 100000
int tree[];
int t;
int n;
int a[];
struct node
{
int l;
int r;
} N[],MM[];
struct pan{
int v;
int pos;
}M[];
bool cmp(struct pan aa,struct pan bb)
{
return aa.v<bb.v;
}
int lowbit(int t)
{
return t&(-t);
}
void add(int i,int v)
{
for(; i<=NN; i+=lowbit(i))
tree[i]+=v;
}
int sum(int i)
{
int ans=;
for(; i>; i-=lowbit(i))
ans+=tree[i];
return ans;
}
int main()
{
while(~scanf("%d",&t))
{
for(int i=; i<=t; i++)
{
scanf("%d",&n);
for(int j=; j<=n; j++)
{
scanf("%d",&a[j]);
M[j].pos=j;
M[j].v=a[j];
N[j].l=j;
N[j].r=j;
}
for(int j=; j<=n; j++)
tree[j]=;
for(int j=; j<=n; j++)
{
add(a[j],);
N[j].l=N[j].l-(j-sum(a[j]));
}
sort(M+,M++n,cmp);
for(int j=;j<=n;j++)
N[M[j].pos].r=max(j,N[M[j].pos].r);
for(int j=;j<=n;j++)
{
MM[a[j]].l=N[j].l;
MM[a[j]].r=N[j].r;
}
for(int j=; j<=n; j++)
{
if(j==)
printf("Case #%d: %d",i,MM[j].r-MM[j].l);
else
printf(" %d",MM[j].r-MM[j].l);
}
printf("\n");
}
}
return ;
}

HDU 5775 树状数组的更多相关文章

  1. Bubble Sort HDU - 5775 树状数组

    //每个数字只会被它后面的比它小的数字影响,且会向右移动相应个数的位置 //比如:6 4 3 5 2 1 .4后面比它小的有 三个,因此它的最右边位置就是当前位置 +3,即5 //如果该数字本身在标准 ...

  2. hdu 4638 树状数组 区间内连续区间的个数(尽可能长)

    Group Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Subm ...

  3. hdu 4777 树状数组+合数分解

    Rabbit Kingdom Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) T ...

  4. HDU 2852 (树状数组+无序第K小)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2852 题目大意:操作①:往盒子里放一个数.操作②:从盒子里扔掉一个数.操作③:查询盒子里大于a的第K小 ...

  5. HDU 4911 (树状数组+逆序数)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4911 题目大意:最多可以交换K次,就最小逆序对数 解题思路: 逆序数定理,当逆序对数大于0时,若ak ...

  6. hdu 5792(树状数组,容斥) World is Exploding

    hdu 5792 要找的无非就是一个上升的仅有两个的序列和一个下降的仅有两个的序列,按照容斥的思想,肯定就是所有的上升的乘以所有的下降的,然后再减去重复的情况. 先用树状数组求出lx[i](在第 i ...

  7. HDU 1934 树状数组 也可以用线段树

    http://acm.hdu.edu.cn/showproblem.php?pid=1394 或者是我自己挂的专题http://acm.hust.edu.cn/vjudge/contest/view. ...

  8. 2018 CCPC网络赛 1010 hdu 6447 ( 树状数组优化dp)

    链接:http://acm.hdu.edu.cn/showproblem.php?pid=6447 思路:很容易推得dp转移公式:dp[i][j] = max(dp[i][j-1],dp[i-1][j ...

  9. 【模板】HDU 1541 树状数组

    http://acm.hdu.edu.cn/showproblem.php?pid=1541 题意:给你一堆点,每个点右一个level,为其右下方所有点的数量之和,求各个level包含的点数. 题解: ...

随机推荐

  1. DataGridView复选框实现全选功能,并取被选中的某行某列的值(三)

    目标: 一.选中全选这个复选框,会选中第一列所有的复选框 拉过来一个CheckBox控件(CheckBox1)覆盖在第一列的标题上,文本值:全选 方法:双击上面拉的CheckBox控件,进入其事件 p ...

  2. 创建一个ROS msg

    1. msg •msg:msg文件是简单的文本文件,用于描述ROS中消息(消息的各个参数项).用于为不同的编程语言生成有关消息的源代码. •srv:描述服务的文件,由两部分组成:请求和反馈: msg文 ...

  3. bitmapData

    一些常用接口: clone(): 得到位图数据的拷贝: 用途:深复制位图 draw(source :IBitmapDrawable...): source 要绘制到 BitmapData 对象的显示对 ...

  4. .net 小问题集结

    1 .net中新建的项目,调试时,提示"由于未在web.config文件中启用调试,因此无法在调试模式下运行该页.您希望做什么?" 解决办法: 在web.config文件中,将&l ...

  5. 获取一个 app 的 URL Scheme 的方法:

    获取一个 app 的 URL Scheme 的方法: 上这个网站 URL Schemes 查一下相应的 app 的 URL Scheme 是否有被收录 第一种方法没找到的话,把相应的 app 的 ip ...

  6. 3D中的切线空间简介

    转自:http://www.cnblogs.com/cxrs/archive/2009/10/25/1589515.html 1. 什么是Tangent space? Tangent space和wo ...

  7. Ubuntu 14.10 下CPU实时监控mpstat命令详解

    简介 mpstat是Multiprocessor Statistics的缩写,是实时系统监控工具.其报告与CPU的一些统计信息,这些信息存放在/proc/stat文件中.在多CPUs系统里,其不但能查 ...

  8. Ubuntu 14.10 下安装java反编译工具 jd-gui

    系统环境,Ubuntu 14.10 ,64位 1 下载JD-GUI,网址http://221.3.153.126/1Q2W3E4R5T6Y7U8I9O0P1Z2X3C4V5B/jd.benow.ca/ ...

  9. Cobub Razor

    Cobub Razor - 开源移动应用统计分析平台

  10. 0816 1459 json & pickle ,目录导入,目录规范

    ---恢复内容开始--- 1.json & pickle 磁盘上只能存储字符串或二进制数据,直接存字典.列表.元组等是存不了的,所以需要把各种数据转换成字符串格式,然后再存到硬盘. 直接将一个 ...