hdu 1890 Robotic Sort(splay 区间反转+删点)
题意:
给你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 区间反转+删点)的更多相关文章
- HDU 1890 - Robotic Sort - [splay][区间反转+删除根节点]
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1890 Time Limit: 6000/2000 MS (Java/Others) Memory Li ...
- HDU 1890 Robotic Sort | Splay
Robotic Sort Time Limit: 6000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) [Pr ...
- HDU 1890 Robotic Sort (splay tree)
Robotic Sort Time Limit: 6000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Tota ...
- 数据结构(Splay平衡树):HDU 1890 Robotic Sort
Robotic Sort Time Limit: 6000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Tota ...
- HDU 1890 Robotic Sort(splay)
[题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=1890 [题意] 给定一个序列,每次将i..P[i]反转,然后输出P[i],P[i]定义为当前数字i ...
- HDU1890 Robotic Sort Splay tree反转,删除
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1890 题目中涉及数的反转和删除操作,需要用Splay tree来实现.首先对数列排序,得到每个数在数列 ...
- hdu 1890 Robotic Sort
原题链接:http://acm.hdu.edu.cn/showproblem.php?pid=1890 如下: #include<cstdio> #include<cstdlib&g ...
- hdu1890 Robotic Sort (splay+区间翻转单点更新)
Problem Description Somewhere deep in the Czech Technical University buildings, there are laboratori ...
- 算法模板——splay区间反转 2
实现功能:同splay区间反转 1(基于BZOJ3223 文艺平衡树) 这次改用了一个全新的模板(HansBug:琢磨了我大半天啊有木有),大大简化了程序,同时对于splay的功能也有所完善 这里面没 ...
随机推荐
- Effective C++(19) 设计class犹如设计type
问题聚焦: 这一节不涉及代码,但是我们需要明确的一点是,思想比代码要重要得多. 设计优秀的classes是一项艰巨的工作,就像设计好的types一样. 我们应该带着和“语言设计 ...
- 【Oracle】-【COMMIT对索引的影响】-从trace看COMMIT对索引的影响
之前看过老杨http://yangtingkun.itpub.net/post/468/231000的一篇文章,讲述了INSERT操作对全文索引无操作,但DELETE时为了防止删除的数据仍能通过索引的 ...
- Markdown 代码测试
Mou Overview Mou, the missing Markdown editor for web developers. Syntax Strong and Emphasize strong ...
- 网际协议:无连接数据报交付(IPv4)
一.数据报格式:
- c# in deep 之LINQ简介(1)
前两天公司进了一批书,在借阅jon skeet的c# in deep收获颇大,本书特点是介绍了不同版本的c#所增加的新特性.今天先写一下书中对linq的描述. 很多初学者在使用VS2010或2013写 ...
- 释放c盘空间
Win7的系统引导盘用着用着会越来越小.怎么办呢?我以前在网上查过资料,说是找个工具加大C盘.我加了,从原来的20G加到现在的35G.用了一段时间后,空间又只剩几百M了.难道又要加?? 后来,在网上找 ...
- 程序媛也会画图 之 在ubuntu下用GIMP制作gif
动画是什么?就是几张图片很快的顺序播放嘛,今天就画画gif小图吧,以后在演示效果图的时候也用的上. 1.准备一个小图 就用小绿人好了 2.打开GIMP 3.新建一个图片打开 文件 ->新建输入 ...
- queue,指针求最短路的区别
这里以spfa为例://都用邻接表存边: 指针: int h=1,t=1; q[h]=x; while(h<=t){ int u=q[h]; vis[u]=0; for(int i=head[u ...
- 2013.4.A
=-=最近简直忙疯了.....两眼泪汪汪的...一个星期又磨磨蹭蹭的刷了一套 1.java_c 题1 Java vs C++ [问题描述] Java和C++两种语言的辩手都可以相互争论数小时去证明他们 ...
- GoLang获取struct的tag
GoLang获取struct的tag内容:beego的ORM中也通过tag来定义参数的. 获取tag的内容是利用反射包来实现的.示例代码能清楚的看懂! package main import ( &q ...