题目链接:hdu 1890 Robotic Sort

题意:

给你n个数,每次找到第i小的数的位置,然后输出这个位置,然后将这个位置前面的数翻转一下,然后删除这个数,这样执行n次。

题解:

典型的splay区间翻转+删点。

我们把数据排序,然后记录一下每个数原来的位置,然后splay建树的时候用原来的位置来对应,这样val[i].second就直接是这个数在splay中的那个节点。

(当然你也可以普通建树,然后手动记录位置)。

然后我们把要找的那个数对应的节点旋转到根,然后根左边的size+i就是当前数的答案,然后翻转一下前面的数,再删除根。

 #include<bits/stdc++.h>
#define F(i,a,b) for(int i=a;i<=b;++i)
using namespace std; const int N=1e5+;
int n,a[N],size[N],ch[N][],f[N],tot,root;bool rev[N];
pair<int,int>val[N]; void rev1(int x){if(!x)return;swap(ch[x][],ch[x][]);rev[x]^=;} void pb(int x){
if(rev[x]){
rev1(ch[x][]);
rev1(ch[x][]);
rev[x]=;
}
} void up(int x){
size[x]=;
if(ch[x][])size[x]+=size[ch[x][]];
if(ch[x][])size[x]+=size[ch[x][]];
} void rotate(int x){
int y=f[x],w=ch[y][]==x;
ch[y][w]=ch[x][w^];
if(ch[x][w^])f[ch[x][w^]]=y;
if(f[y]){
int z=f[y];
if(ch[z][]==y)ch[z][]=x;
if(ch[z][]==y)ch[z][]=x;
}
f[x]=f[y];ch[x][w^]=y;f[y]=x;up(y);
} void splay(int x,int w){
int s=,i=x,y;a[]=x;
while(f[i])a[++s]=i=f[i];
while(s)pb(a[s--]);
while(f[x]!=w){
y=f[x];
if(f[y]!=w){if((ch[f[y]][]==y)^(ch[y][]==x))rotate(x);else rotate(y);}
rotate(x);
}
if(!w)root=x;
up(x);
} void newnode(int &r,int fa,int k)
{
r=k,f[r]=fa,rev[r]=,ch[r][]=ch[r][]=;
} void build(int &x,int l,int r,int fa){
int mid=(l+r)>>;
newnode(x,fa,mid);
if(l<mid)build(ch[x][],l,mid-,x);
if(r>mid)build(ch[x][],mid+,r,x);
up(x);
return;
} int getmax(int x)
{
pb(x);
while(ch[x][])x=ch[x][],pb(x);
return x;
} void delroot()
{
if(ch[root][])
{
int m=getmax(ch[root][]);
splay(m,root);
ch[m][]=ch[root][];
f[ch[root][]]=m;
root=m,f[m]=,up(m);
}else root=ch[root][],f[root]=;
} int main(){
while(scanf("%d",&n),n)
{
F(i,,n)scanf("%d",&val[i].first),val[i].second=i;
sort(val+,val++n),build(root,,n,);
F(i,,n-)
{
splay(val[i].second,);
rev1(ch[root][]);
printf("%d ",i+size[ch[root][]]);
delroot();
}
printf("%d\n",n);
}
return ;
}

hdu 1890 Robotic Sort(splay 区间反转+删点)的更多相关文章

  1. HDU 1890 - Robotic Sort - [splay][区间反转+删除根节点]

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1890 Time Limit: 6000/2000 MS (Java/Others) Memory Li ...

  2. HDU 1890 Robotic Sort | Splay

    Robotic Sort Time Limit: 6000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) [Pr ...

  3. HDU 1890 Robotic Sort (splay tree)

    Robotic Sort Time Limit: 6000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Tota ...

  4. 数据结构(Splay平衡树):HDU 1890 Robotic Sort

    Robotic Sort Time Limit: 6000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Tota ...

  5. HDU 1890 Robotic Sort(splay)

    [题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=1890 [题意] 给定一个序列,每次将i..P[i]反转,然后输出P[i],P[i]定义为当前数字i ...

  6. HDU1890 Robotic Sort Splay tree反转,删除

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1890 题目中涉及数的反转和删除操作,需要用Splay tree来实现.首先对数列排序,得到每个数在数列 ...

  7. hdu 1890 Robotic Sort

    原题链接:http://acm.hdu.edu.cn/showproblem.php?pid=1890 如下: #include<cstdio> #include<cstdlib&g ...

  8. hdu1890 Robotic Sort (splay+区间翻转单点更新)

    Problem Description Somewhere deep in the Czech Technical University buildings, there are laboratori ...

  9. 算法模板——splay区间反转 2

    实现功能:同splay区间反转 1(基于BZOJ3223 文艺平衡树) 这次改用了一个全新的模板(HansBug:琢磨了我大半天啊有木有),大大简化了程序,同时对于splay的功能也有所完善 这里面没 ...

随机推荐

  1. 开篇ASP.NET MVC 权限管理系列

    开篇 [快乐编程系列之ASP.NET MVC 权限管理系列]一.开篇   用了好长一段时间的ASP.NET MVC,感觉MVC真的是很好用,最近一年左右做了两个中小型项目,觉得还是很多地方不是很熟悉的 ...

  2. QQ三方登录步骤详解

    首先,登录QQ互联:http://connect.qq.com/intro/login  ,注册成为开发者 选择申请加入,并创建你的应用. 创建成功后可以获取到appid和appkey 在网站的主页引 ...

  3. XMPP and SIP

    过去一年多,一直关注这方面的技术和发展,这里有一个简单的介绍,我觉得比较简洁明了.我做了一点翻译,还有我的一些评估.       SIP vs XMPP (Jabber) SIP and XMPP a ...

  4. Dynamics CRM 2013 报表开发:安装开发工具

    最近项目需要开发报表,顺便看了下,首先需要配置开发环境.需要的工具为: 1.Business Intelligence Development Studio 可下载Sql Server 的安装包,选择 ...

  5. WCF 自承载

    WCF 自承载 提供源码 一.WCF 简单介绍 Windows Communication Foundation(WCF)是由微软发展的一组数据通信的应用程序开发接口,是一套通讯接口.现在比较流行的S ...

  6. Apache HttpServer Installing the apache2.2 service <OS 5>拒绝访问. :Failed to open the WinNT service manager

    Installing the apache2.2 service<OS 5>拒绝访问.  :Failed to open the WinNT service manager 只需要于管理员 ...

  7. Jquery EasyUI中treegrid

    Jquery EasyUI中treegrid的中右键菜单和一般按钮同时绑定事件时的怪异事件 InChatter系统开源聊天模块前奏曲   最近在研究WCF,又因为工作中的项目需要,要为现有的系统增加一 ...

  8. CloudFormation

    亚马逊云服务之CloudFormation   亚马逊的Web Service其实包含了一套云服务.云服务主要分为三种: IaaS: Infrastructure as a service,基础设施即 ...

  9. WPF专业编程指南 - 那些根元素<>的解释

    <Window x:Class="Chapter1_WpfApplication1.MainWindow" xmlns="http://schemas.micros ...

  10. thinkphp实现自动登录

    网页上经常有一些自动登录的checkbox,勾选后,下次进入该网站,无需登录,即可执行一些需要登录才能执行的操作.上班无事,用thinkphp做了下 1 下面是一个很普通的form表单,有一个chec ...