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. 在Ubuntu上为Android系统内置C可执行程序测试Linux内核驱动程序(老罗学习笔记2)

    在前一篇文章中,我们介绍了如何在Ubuntu上为Android系统编写Linux内核驱动程序.在这个名为hello的Linux内核驱动程序中,创建三个不同的文件节点来供用户空间访问,分别是传统的设备文 ...

  2. gulp edm测试

    gulp工具的 gulp-mailgun 插件可以将你的html代码,通过mailgun服务器来发送,用于测试,用这个工具发送邮件最适合不过了. 首先我们需要引入gulp和gulp-mailgun模块 ...

  3. fzu Problem 2140 Forever 0.5(推理构造)

    题目:http://acm.fzu.edu.cn/problem.php?pid=2140 题意: 题目大意:给出n,要求找出n个点,满足: 1)任意两点间的距离不超过1: 2)每个点与(0,0)点的 ...

  4. POJ 2455 - Secret Milking Machine

    原题地址:http://poj.org/problem?id=2455 题目大意:给出一个N个点的无向图,中间有P条边,要求找出从1到n的T条通路,满足它们之间没有公共边,并使得这些通路中经过的最长的 ...

  5. bzoj2878

    又是环套树dp,这次不是我擅长的类型 首先考虑树上的暴力,肯定是穷举起点然后以起点为根dp 我们用g[i]表示以点i为期望走的路径总长,答案就是1/n*Σ(g[i]/d[i]) (d[i]表示点度数) ...

  6. bzoj1132

    每次都选最左边的点,然后以这个点为原点 统计和这个点构成的三角形面积和 不难想到极角排序然后由叉积很容易求出 shl ; eps=1e-8; var i,j,k,m,n:longint; x,y:.. ...

  7. Js获取日期时间及其它操作

    var myDate = new Date();myDate.getYear();        //获取当前年份(2位)myDate.getFullYear();    //获取完整的年份(4位,1 ...

  8. Java [Leetcode 225]Implement Stack using Queues

    题目描述: Implement the following operations of a stack using queues. push(x) -- Push element x onto sta ...

  9. Java [Leetcode 70]Climbing Stairs

    题目描述: You are climbing a stair case. It takes n steps to reach to the top. Each time you can either ...

  10. erl0001-Erlang 设计原则 process port io

    Erlang原理 (转载自ITEYE cryolite博客 ps:精彩)by Robert Virding This is a description of some of the basic pro ...