多校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 ...
随机推荐
- windows cmd 命令
dir 查看文件,参数:/Q显示文件及目录属系统哪个用户,/T:C显示文件创建时间,/T:A显示文件上次被访问时间,/T:W上次被修改时间 set 显示当前所有的环境变量 find 文件名 查找某文件 ...
- MySQL索引使用:字段为varchar类型时,条件要使用''包起来
结论: 当MySQL中字段为int类型时,搜索条件where num='111' 与where num=111都可以使用该字段的索引.当MySQL中字段为varchar类型时,搜索条件where nu ...
- Assertion (software development) -- 断言
Wiki: In computer programming, an assertion is a predicate (a true–false statement) placed in a prog ...
- Java+selenium自动化测试基础
Java+selenium maven配置 maven的配置,但还需要建立maven的本地库,修改apach-maven的setting.xml http://www.cnblogs.com/haoa ...
- 巨蟒python全栈开发django7:多表增加和查询
1.回顾内容&&补充 补充1: 补充2: 这个选择的是第二个解释器. 选择第一个的话,只是针对当前的项目,如果再开新项目的话,需要重新下载安装相关的包. 点击保存,因为我们注释掉了,创 ...
- Token bucket
w https://en.wikipedia.org/wiki/Token_bucket
- python面试题(七)
1 什么是局域网.广域网.城域网? ①局域网LAN(Local Area Network):一般指覆盖范围在10公里以内,一座楼房或一个单位内部的网络.由于传输距离直接影响传输速度,因此,局域网内的通 ...
- 协程 Gevent
# 协程应用:爬虫 from gevent import monkey;monkey.patch_all() import gevent import requests import time def ...
- c# HttpClient获取网页源码
#region 获取网页源码 public static string HttpClientGetHtmls(string url) { try { var client = new HttpClie ...
- 深度学习:Keras入门(二)之卷积神经网络(CNN)(转)
转自http://www.cnblogs.com/lc1217/p/7324935.html 1.卷积与神经元 1.1 什么是卷积? 简单来说,卷积(或内积)就是一种先把对应位置相乘然后再把结果相加的 ...