多校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 ...
随机推荐
- OpenCV学习笔记二:OpenCV模块一览
注:本系列博客基于OpenCV 2.9.0.0 一,一览图: 二,模块: /* 基础库 */ 1,opencv_core(链接) ,opencv最基础的库.包含exception,point,rect ...
- cocos2d-x:Layer::setPosition
如果Node的实际类型是Layer或者其派生类, setPosition是不是有猫腻? std::string menuImage = "menu.png"; auto menuI ...
- 43、android:screenOrientation
android:screenOrientationThe orientation of the activity's display on the device. The value can be a ...
- LeetCode Problem 169: Majority Element查找多数元素
描述:Given an array of size n, find the majority element. The majority element is the element that app ...
- 《从零开始学Swift》学习笔记(Day 23)——尾随闭包
原创文章,欢迎转载.转载请注明:关东升的博客 闭包表达式可以作为函数的参数传递,如果闭包表达式很长,就会影响程序的可读性.尾随闭包是一个书写在函数括号之后的闭包表达式,函数支持将其作为最后一个参数调用 ...
- mailing list的原理
1 发往mailing list邮箱的邮件会被所有订阅了该邮箱的人收到 说白了,就是一种邮件群发机制,为了简化群发,不是将所有的收件人放到收件人列表中,而是发往总的邮箱即可. 2 要向该mailing ...
- python系列三:python3运算符
'''python 没有自增运算符python 中,变量是以内容为基准而不是像 c 中以变量名为基准,所以只要你的数字内容是5,不管你起什么名字,这个变量的 ID 是相同的,同时也就说明了 pytho ...
- 关于like %%的优化思路
测试数据:2亿行,被筛选出的数据,3KW多行. 众所周知 like %str%无法走索引,但是我们如果实在是有这种需求要达到like '%str%'的筛选目的,怎么优化好一些呢? 以下是我的一些思考: ...
- Linux三种网络
Host-Only 桥接
- Django CSRF 原理分析
原文链接: https://blog.csdn.net/u011715678/article/details/48752873 参考链接:https://blog.csdn.net/clark_fit ...