多校hdu-5775 Bubble sort(线段树)
题意根据题目中给的冒泡排序写出每个元素交换过程中该元素位置左右最大差距;
分析:因为题目中冒泡程序从后向前遍历的,假设第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(线段树)的更多相关文章
- 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 ...
- HDU 5775 Bubble Sort(冒泡排序)
p.MsoNormal { margin: 0pt; margin-bottom: .0001pt; text-align: justify; font-family: Calibri; font-s ...
- HDU 5775 Bubble Sort (线段树)
Bubble Sort 题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5775 Description P is a permutation of t ...
- hdu 5775 Bubble Sort 树状数组
Bubble Sort 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5775 Description P is a permutation of t ...
- 【归并排序】【逆序数】HDU 5775 Bubble Sort
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5775 题目大意: 冒泡排序的规则如下,一开始给定1~n的一个排列,求每个数字在排序过程中出现的最远端 ...
- HDU 5775 Bubble Sort
对于一个数,可以记录3个位置:初始位置,终点位置,最右边的位置. 初始位置和终点位置容易计算.最多边的位置即为初始状态下该数的位置+该数之后还有多少数比该数小. 三个位置中的min即为leftpos, ...
- HDU 5775:Bubble Sort(树状数组)
http://acm.hdu.edu.cn/showproblem.php?pid=5775 Bubble Sort Problem Description P is a permutation ...
- hdu 5700区间交(线段树)
区间交 Time Limit: 8000/4000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submiss ...
- 2016暑假多校联合---Rikka with Sequence (线段树)
2016暑假多校联合---Rikka with Sequence (线段树) Problem Description As we know, Rikka is poor at math. Yuta i ...
随机推荐
- js事件绑定的方法
废话不多少,直接上代码 第一种 <body> <div style="width:400px;height:400px;background:blueviolet" ...
- tomcat启动不起来,不知原因,没有报错日志,控制台一闪而过 怎么办
在startup.bat文件中 编辑,在最后一行回车 加上一个词pause,暂停,然后再启动就看见控制台的错误信息啦,然后就自己解决吧
- LeetCode Problem 9:Palindrome Number回文数
描述:Determine whether an integer is a palindrome. Do this without extra space. Some hints: Could nega ...
- lua(仿单继承)
--lua仿单继承 Account = { balance = } function Account:new(o) o = o or {} setmetatable(o, self)--Account ...
- Problem H. Hotel in Ves Lagos
Problem H. Hotel in Ves Lagos Input le: hotel.in Output le: hotel.out Time limit: 1 second Memory li ...
- docker-compose安装confluence
1.首先安装docker-compose pip install docker-compose 安装完成提示: 2.编写mysql-confluence-compose ...
- angularjs 发送ajax请求的问题
在angularjs中使用 ajax 如果使用 jquery的 ajax发送请求会遇到结果返回了,但是页面的值却没有改变,如: $scope.queryNameMatch = function() { ...
- js实现文字列表无缝向上滚动
body{font-size:12px} #demo{overflow:hidden; height:80px; width:280px; margin:90px auto; position:rel ...
- 【Java工程师之路】[1-2.2]Java10个面向对象设计原则
面向对象设计原则是OOPS(Object-Oriented Programming System,面向对象的程序设计系统)编程的核心,但大多数Java程序员追逐像Singleton.Decorator ...
- python默认参数不能定义为可变对象类型
python的默认参数只会在函数定义时被确定,而不是每次调用时重新确定,所以,一旦在函数中修改了默认参数,则在随后的调用中都会生效 由于这个特性,在定义函数时,如果默认参数使用可变的对象类型,如空列表 ...