HDU 5775 Bubble Sort (线段树)

时间:2021-05-05 23:43:48

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;

}