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. 各种主流数据库的比较(所以说我觉得Oracle这个keng?入的不错?)

    随着计算机技术不断发展,各种数据库编程工具也随着发展,使当今的大多数程序开发人员可以摆脱枯燥无味的用计算机指令或汇编语言开发软件,而是利用一系列高效的.具有良好可视化的编程工具去开发各种数据库软件,从 ...

  2. HDU 2676 Network Wars 01分数规划,最小割 难度:4

    http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1676 对顶点i,j,起点s=1,终点t=n,可以认为题意要求一组01矩阵use ...

  3. DatagridView的CellLeave光标离开响应事件,实现某列数字自动求和

    //光标离开DatagridView,循环获取DatagridView的每一行的第3列的值,相加传给重量 private void dgpz_dataGridView_CellLeave(object ...

  4. SuperGridControl 使用小技巧

    1.显示行号 superGridControl1.PrimaryGrid.ShowRowGridIndex = true; 2.允许调整行头的宽度 superGridControl1.PrimaryG ...

  5. Spring中的Jdbc事务管理

    Spring提供了对事务的声明式事务管理,只需要在配置文件中做一些配置,即可把操作纳入到事务管理当中,解除了和代码的耦合. Spring声明式事务管理,核心实现就是基于Aop. Spring声明式事务 ...

  6. “人少也能办大事”---K2 BPM老客户交流会

    主题:固铂轮胎工作流项目分享-K2 SmartForm下的工作流快速开发 嘉宾:王彦(固铂轮胎IT资深经理) 国内业务规模越来越大,流程越来越复杂,跨部门跨组织的流程纸质审批非常复杂,内控的要求越来越 ...

  7. Program C 暴力求解

    Description   A ring is composed of n (even number) circles as shown in diagram. Put natural numbers ...

  8. Windows 8.1 Update 2更新了什么?

    Windows 8.1的第二个更新将于8月12日(周二补丁日)发布,官方命名是“8月更新”(August Update).但是之前我们已经知道Windows 8.1 Update 2不可能重新提供开始 ...

  9. objective-c strong导致内存泄漏简单案例

    例如: @interface Test:NSObject{ id __strong obj_; } -(void) setObject:(id __strong)obj; @end @implemen ...

  10. JS学习第一课

    1.js 按照编写顺序执行 2.输出使用document.write. 3.申明数组 var array = [1,2,3,5] ;  var arrStr = ["sgsg",& ...