题意根据题目中给的冒泡排序写出每个元素交换过程中该元素位置左右最大差距;

分析:因为题目中冒泡程序从后向前遍历的,假设第i个元素左边有k个比i小的数,那么i必定会向右移动k位,我们用k1记住i+k,用k2记住i最终移到的位置a[i],用k3记住i的初始位置i,那么左右的最大值和最小值一定在k1,k2,k3中产生,此处不做证明,自己可以仔细想想。

现在主要问题在于寻找第i位置的左区间有几个比a[i]大的就可以了,解决办法:用线段树的节点储存次在节点左右区间的a[i]的个数,初始化为0,从a[n]到a[1]查询(1,a[i]-1)区间的数的个数,在把a[i]扔到线段树中更新就可以了。

代码如下:

#include <iostream>
#include <stdio.h>
#include <math.h>
#include <map>
#include <string.h>
#include <algorithm>
#define LL long long
using namespace std;
const int N=100010;
int g[4*N];
void creat(int k,int l,int r)
{
if(l==r)
{
g[k]=0;
return ;
}
int mid=(l+r)/2;
creat(k<<1,l,mid);
creat(k<<1|1,mid+1,r);
g[k]=0;
}
int finds(int k,int l,int r,int L,int R)
{
if(l>=L&&r<=R)
{
return g[k];
}
int mid=(l+r)/2;
int k1=0,k2=0;
if(mid>=L)
{
k1=finds(k<<1,l,mid,L,R);
}
if(mid<R)
{
k2=finds(k<<1|1,mid+1,r,L,R);
}
return k1+k2;
}
void updata(int k,int l,int r,int x)
{
if(l==r&&l==x)
{
g[k]=1;
return ;
}
int mid=(l+r)/2;
if(x<=mid)
{
updata(k<<1,l,mid,x);
g[k]+=1;
}
if(x>mid)
{
updata(k<<1|1,mid+1,r,x);
g[k]+=1;
}
return ;
}
int main()
{
int t,n,i,j,h=1,b[N],a[N],c[N];
cin>>t;
while(t--)
{
scanf("%d",&n);
for(i=1;i<=n;i++)
{
scanf("%d",&a[i]);
}
creat(1,1,n);
for(i=n;i>0;i--)
{
if(a[i]==1)
b[i]=0;
else
b[i]=finds(1,1,n,1,a[i]-1);
updata(1,1,n,a[i]);
}
printf("Case #%d: ",h++);
for(i=1;i<=n;i++)
{
int k1=b[i]+i;
int k2=i;
int k3=a[i]; c[a[i]]=max(max(k1,k2),k3)-min(min(k1,k2),k3);
}
for(i=1;i<=n;i++)
{
if(i==n)
printf("%d",c[i]);
else
printf("%d ",c[i]);
}
printf("\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 树状数组

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

  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. 2016暑假多校联合---Rikka with Sequence (线段树)

    2016暑假多校联合---Rikka with Sequence (线段树) Problem Description As we know, Rikka is poor at math. Yuta i ...

随机推荐

  1. OpenCV学习笔记八:opencv_photo模块

    一,简介: 该库用于数码照片的处理,处于发展中,目前只包含如下算法: //! restores the damaged image areas using one of the available i ...

  2. Uva1025 A Spy in the Metro

    #include <iostream> #include <cstring> #include <cstdio> using namespace std; ]; ] ...

  3. PDP2

    1. 程序入口 配置文件config.xml中: 2. index.html中显示,引入的cordova.js 就说明了 cordova apps 3. 看 main.ts

  4. 《从零开始学Swift》学习笔记(Day 18)——有几个分支语句?

    原创文章,欢迎转载.转载请注明:关东升的博客       分支语句又称条件语句,Swift编程语言提供了if.switch和guard三种分支语句. if语句 由if语句引导的选择结构有if结构.if ...

  5. 超哥mysql数据库部分blog整理:

    总目录:mysql数据库阶段学习目录 https://www.cnblogs.com/clschao/articles/10065275.html Day1. 1.数据库初识 https://www. ...

  6. api文档的书写

    写文档写要与写代码一样,增加复用. 比如 model 说明就只需要一个,api中含有哪些字段,就在api说明中增加到那些 models 的链接. 使用 sophinx 如何生成目录 .. toctre ...

  7. IOS自动布局的Problem

    今天为做一个小小的footerView我的心脏差点气出来... 第一步 新建一个view xib文件 第二步 新建一个UIview的类 第三步 在UIview中间放一个Lable,添加约束,水平垂直居 ...

  8. python错误笔记

    1.print "hello world!";SyntaxError:Missing parentheses in call to ‘paint’ . Did you mean p ...

  9. python 获取exception 名字

    def func(): list = [] usr = input('username:') pwd = input('password:') try: list[4] # 这个是调用不了的,因为列表 ...

  10. MYSQL中case when then else end 用法

    条件语句 delimiter \\CREATE PROCEDURE proc_if ()BEGIN      declare i int default 0;   if i = 1 THEN      ...