题目链接: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. nginx 502 Bad Gateway 错误问题收集

    nginx 502 Bad Gateway 错误问题收集 (2010-11-18 13:51:37) 转载▼ 标签: 杂谈 分类: 工作 nginx 502 Bad Gateway 错误问题收集 因为 ...

  2. SQL Server 行版本控制

    什么时候开启行版本? 当发现系统中存在大量读写阻塞的时候,就是我们开启行版本的时候了.要点:写写阻塞的场景行版本技术解决不了 开启方式: -- 设置为单用户 ALTER DATABASE MyTest ...

  3. Nutch搜索引擎Solr简介及安装

    Nutch搜索引擎(第2期)_ Solr简介及安装   1.Solr简介 Solr是一个高性能,采用Java5开发,基于Lucene的全文搜索服务器.同时对其进行了扩展,提供了比Lucene更为丰富的 ...

  4. 10.26最后的模拟DAY2 改造二叉树[中序遍历+严格递增的最长不下降子序列]

    改造二叉树 [题目描述] 小Y在学树论时看到了有关二叉树的介绍:在计算机科学中,二叉树是每个结点最多有两个子结点的有序树.通常子结点被称作“左孩子”和“右孩子”.二叉树被用作二叉搜索树和二叉堆.随后他 ...

  5. IIS Express添加MIME映射

    最近在使用fontawesome字体时,在浏览器控制台看到 fontawesome-webfont.woff2?v=4.3.0 无法访问的错误,检查了一下文件确实存在并且路径也对,这就奇怪了! 在控制 ...

  6. [转]LLVM MC Project

    Intro to the LLVM MC Project The LLVM Machine Code (aka MC) sub-project of LLVM was created to solve ...

  7. Android 屏幕截图(底层实现方式)

    加载底层库ScreenCap.java: public class ScreenCap { static { System.loadLibrary("scrcap"); } sta ...

  8. ArcGIS多面体(multipatch)解析(一)

    从几何学的角度来说,再复杂的三维形状都可以通过足够多,足够小的三角形组合起来表达.比如矩形可以表达成两个三角形,如下图: 圆柱形可以由多个三角形排列而成: 但ArcGIS中的多面体构成并没有采用这种“ ...

  9. Emacs助力PowerShell

    Emacs助力PowerShell 阅读目录 1 下载安装Emacs windows版本 2 下载el文件和配置Emacs加载PowerShell 3 体验用Emacs来执行和编辑PowerShell ...

  10. iOS 动画类型 笔记

    #pragma mark Core Animation - (IBAction)buttonPressed1:(id)sender { UIButton *button = (UIButton *)s ...