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. 文本信息“welcome to java programming!”

    import javax.swing.JOptionPanepublic class welcome {public static void main(string[] arg){JOptionPan ...

  2. Admob(6.12.x)符号未定义错误的解决方法(IOS)

    在升级Admob的SDK版本到6.12.x时, 按照官方文档操作(https://developers.google.com/mobile-ads-sdk/docs/#ios), 添加如下framew ...

  3. visualSVN server库迁移

    首先,VisualSVN Server Manager,包含两个路径,一个是安装路径,例如本机:C:\Program Files\VisualSVN Server,一个是库路径,例如本机:C:\Rep ...

  4. 关于Integer缓冲

    默认的时候,-128--127范围内的数会被缓冲 但是jvm启动的时候可以修改启动参数-Djava.lang.Integer.IntegerCache.high=2000 来修改上限,但是下限是不可以 ...

  5. iOS中实现多线程的技术方案

    pthread 实现多线程操作 代码实现: void * run(void *param) {    for (NSInteger i = 0; i < 1000; i++) {         ...

  6. Great writers inspire

    William Shakespeare Beowulf (Anglo-Saxon) Jonathan Swift Stephen Duck William Blake George Elliot Ka ...

  7. APP store 官方统计工具的常见的Q&A

    Apple最近在iTunesConnect里最新发布了官方统计工具,提供了现有友盟统计平台和自有统计平台无法统计的数据,具有自己的独有特点,尤其是下面几个最让人头疼的流量分析转化,可以在App Ana ...

  8. 修改Centos SSH远程端口

    1. 在防火墙添加开放端口10000:本机防火墙和云防火墙 本机防火墙:-A INPUT -m state --state NEW -m tcp -p tcp --dport 10000 -j ACC ...

  9. RelativeLayout相对布局中拖放控件的办法

    相对布局中拖了一个控件以后,要拖放第二个空间,死活拖不进去.仔细查看了鼠标的状况,发现要把第二个控件拖到第一个控件的周围,才能成功.果然是相对布局.

  10. Oracle普通索引,唯一索引,主键的区别

    索引是我们经常使用的一种数据库优化手段,适当的业务操作场景使用适当的索引方案,可以显著的提升系统整体查询性能,当然用户体验也随之提高. 在Oracle中,唯一性索引(Unique Index)是我们经 ...