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.

Source

2016 Multi-University Training Contest 4

##题意:

对于N的一个全排列做一遍冒泡排序,求每个元素所到达的最右端和最左端的差.


##题解:

一开始想当然以为每个元素在冒泡的过程中只会往单一方向移动,所以原始位置和最终位置之差即为所求.
不过很快看到大部分队伍都挂掉了, 意识到上述算法有问题.
对于数据:1 5 3 4 2
按以上思路:0 3 0 0 3
而实际模拟一遍:0 3 1 1 3

考虑元素i,它的右边有多少个比i小的元素,就会右移多少次; 左边有多少个比i大的元素,就会左移多少次.
所以分别记录每个元素往两边的逆序数,即为左移和右移的次数.
又根据冒泡的过程,对于i,一定要把i右边比i小的数移到左边后,才会考虑i的左移:这说明所有的右移操作都先于左移操作.
综上可以求得每个元素达到的最右端和最左端.
记录逆序数这里用线段树实现,也可用更简洁的树状数组.


##代码:
``` cpp
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define LL int
#define mid(a,b) ((a+b)>>1)
#define eps 1e-8
#define maxn 101000
#define mod 100000007
#define inf 0x3f3f3f3f
#define IN freopen("in.txt","r",stdin);
using namespace std;

int n;

struct Tree

{

int left,right;

LL sum; /sum为区间和,可改为最大值最小值等/

}tree[maxn<<2]; /四倍大小/

/递归建树/

void build(int i,int left,int right)

{

tree[i].left=left;

tree[i].right=right;

if(left==right){
tree[i].sum=0;
return ;
} int mid=mid(left,right); build(i<<1,left,mid);
build(i<<1|1,mid+1,right); tree[i].sum=tree[i<<1].sum+tree[i<<1|1].sum;

}

/单点修改,d为改变量,两函数可共存/

void update(int i,int x,LL d)

{

if(tree[i].left==tree[i].right){

tree[i].sum = d;

return;

}

int mid=mid(tree[i].left,tree[i].right);

if(x<=mid) update(i<<1,x,d);
else update(i<<1|1,x,d); tree[i].sum=tree[i<<1].sum+tree[i<<1|1].sum;

}

/区间结果查询/

LL query(int i,int left,int right)

{

if(tree[i].leftleft&&tree[i].rightright)

return tree[i].sum;

int mid=mid(tree[i].left,tree[i].right);

if(right<=mid) return query(i<<1,left,right);
else if(left>mid) return query(i<<1|1,left,right);
else return query(i<<1,left,mid)+query(i<<1|1,mid+1,right);

}

int pos[maxn];

int num[maxn];

int _left[maxn];

int _right[maxn];

int main(int argc, char const *argv[])

{

//IN;

int t; cin >> t; int ca = 1;
while(t--)
{
cin >> n; for(int i=1; i<=n; i++) {
int x; scanf("%d", &x);
num[i] = x;
pos[x] = i;
} build(1,1,n);
for(int i=1; i<=n; i++) {
int x = num[i];
_left[x] = 0;
_left[x] = query(1, x, n);
update(1, x, 1);
}
build(1,1,n);
for(int i=n; i>=1; i--) {
int x = num[i];
_right[x] = 0;
_right[x] = query(1, 1, x);
update(1, x, 1);
} printf("Case #%d: ", ca++);
for(int i=1; i<=n; i++) {
int ma = max(pos[i], pos[i]+_right[i]);
int mi = min(pos[i], pos[i]+_right[i]-_left[i]);
printf("%d%c", abs(ma-mi), i==n?'\n':' ');
}
} return 0;

}

HDU 5775 Bubble Sort (线段树)的更多相关文章

  1. HDU 5775 Bubble Sort(线段树)(2016 Multi-University Training Contest 4 1012)

    原址地址:http://ibupu.link/?id=31 Problem Description P is a permutation of the integers from 1 to N(ind ...

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

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

  3. hdu 5775 Bubble Sort 树状数组

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

  4. 多校hdu-5775 Bubble sort(线段树)

    题意根据题目中给的冒泡排序写出每个元素交换过程中该元素位置左右最大差距: 分析:因为题目中冒泡程序从后向前遍历的,假设第i个元素左边有k个比i小的数,那么i必定会向右移动k位,我们用k1记住i+k,用 ...

  5. 【归并排序】【逆序数】HDU 5775 Bubble Sort

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5775 题目大意: 冒泡排序的规则如下,一开始给定1~n的一个排列,求每个数字在排序过程中出现的最远端 ...

  6. HDU 5775 Bubble Sort

    对于一个数,可以记录3个位置:初始位置,终点位置,最右边的位置. 初始位置和终点位置容易计算.最多边的位置即为初始状态下该数的位置+该数之后还有多少数比该数小. 三个位置中的min即为leftpos, ...

  7. HDU 5775:Bubble Sort(树状数组)

    http://acm.hdu.edu.cn/showproblem.php?pid=5775 Bubble Sort Problem Description   P is a permutation ...

  8. hdu 5700区间交(线段树)

    区间交 Time Limit: 8000/4000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submiss ...

  9. Snacks HDU 5692 dfs序列+线段树

    Snacks HDU 5692 dfs序列+线段树 题意 百度科技园内有n个零食机,零食机之间通过n−1条路相互连通.每个零食机都有一个值v,表示为小度熊提供零食的价值. 由于零食被频繁的消耗和补充, ...

随机推荐

  1. hadoop2 环境的搭建(手动HA)

    1.手工切换ha的环境的搭建(比hadoop1多出来journalnode的配置) namenode:hadoop110和hadoop111 datanode:hadoop112.hadoop113. ...

  2. java 菱形

    //画菱形 一半 for(int hs=1;hs<11;hs++) //行数 { //画空格 for(int kg = 9; kg >= hs; kg--) //空格数 { System. ...

  3. EBS报表输出文件格式控制

    具体使用方法:1.添加用户参数p_conc_request_id2.在BeforeReport trigger中添加srw.user_exit('FND SRWINIT');          和Af ...

  4. Eclipse中Python插件PyDev的安装与配置流程

    安装PyDev插件的两种安装方法: 方法1.下载地址:http://sourceforge.net/projects/pydev/files/,将下载的PyDev解压(目前最新版本 PyDev 4.5 ...

  5. uva12034Race

    递推,组合. 考虑第一名有i个人,则f[n]=sum(C(n,i)*f[n-i]),递推即可.. #include<cstdio> #include<algorithm> #i ...

  6. POJ 3687 Labeling Balls【拓扑排序 优先队列】

    题意:给出n个人,m个轻重关系,求满足给出的轻重关系的并且满足编号小的尽量在前面的序列 因为输入的是a比b重,但是我们要找的是更轻的,所以需要逆向建图 逆向建图参看的这一篇http://blog.cs ...

  7. sublime3 常用功能总结

    介绍几个常见的功能: l 自动完成:自动完成的快捷键是Tab和Enter,如果在html文件中,输入cl按下tab或Enter,即可自动补全为class=””:加上zencoding后,更是如虎添翼, ...

  8. directdraw显示yuv视频,出现屏保时,yuv显示不出来,表面丢失

    原因是: DDrawSurface 丢失, DDraw表面在很多情况下都会丢失(如:启动其他全屏独占程序,屏保,或锁屏时), 表面丢失其实就是表面所使用的内存或显存被DirectDraw系统释放, 分 ...

  9. http请求返回响应码的意思

    HTTP 状态响应码 意思详解/大全 HTTP状态码(HTTP Status Code)是用以表示网页服务器HTTP响应状态的3位数字代码.它由 RFC 2616 规范定义的,并得到RFC 2518. ...

  10. 【DFS,双向】NYOJ-20-吝啬的国度

    [题目链接:NYOJ-20] 很巧妙,要好好想想 #include <iostream> #include <stdio.h> #include <vector> ...