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的功能也有所完善 这里面没 ...
随机推荐
- html postMessage 创建聊天应用
应用说明: 这个例子演示如何在门户页面以iframe方式嵌入第三方插件,示例中使用了一个来域名下的应用部件,门户页面通过postMessage来通信.iframe中的聊天部件通过父页面标题内容的闪烁来 ...
- 删除指定表的所有索引,包括主键索引,唯一索引和普通索引 ,适用于sql server 2005,
原文:删除指定表的所有索引,包括主键索引,唯一索引和普通索引 ,适用于sql server 2005, --删除指定表中所有索引 --用法:declare @tableName varchar(100 ...
- sql简单实用的统计汇总案例参考
USE [PM]GO/****** 对象: StoredProcedure [dbo].[LfangSatstics] 脚本日期: 08/24/2013 10:57:48 ******/SET ...
- MVC之前的那点事儿系列进入CLR
MVC之前的那点事儿系列(1):进入CLR MVC之前的那点事儿系列,是笔者在2012年初阅读MVC3源码的时候整理的,主要讲述的是从HTTP请求道进入MVCHandler之前的内容,包括了原创,翻译 ...
- (转载)Log4Net 在多层项目中的使用小记
(原创)Log4Net 在多层项目中的使用小记 这几天刚好在调整一个项目,把一些自己不是很清楚的东西先试验一下,这篇文章主要是对我在项目中需要使用Log4Net的一些记录.网上有很多相关的教程,但是各 ...
- iOS基础 - 单元测试
单元测试(unit testing):对软件中最小可测试单元进行检查和验证.一般面向过程的语言中,基本单元为函数,面向对象的语言中,基本单元通常是类,其实对于一个手机上的app来说基本单元也可以是一个 ...
- ASP.NET中页面传值
一.目前在ASP.NET中页面传值共有这么几种方式: 1.表单提交, <form action= "target.aspx" method = "post&q ...
- 【转】Objective-C并发编程:API和挑战
并发指的是在同一时间运行多个任务.在单核CPU的情况下,它通过分时的方式实现,如果有多个CPU可用,则是真正意义上的多个任务“并行”执行了. OS X和iOS提供了多个API支持并发编程.每个API都 ...
- MVC中验证码
MVC中验证码的实现(经常用,记录备用) 一.目录 1.多层架构+MVC+EF+AUTOFAC+AUTOMAPPER: 2.MVC中验证码的实现(经常用,记录备用) 3.Ligerui首页的快速搭 ...
- TD中{text-overflow:ellipsis;} 用法
Styles: table{ table-layout:fixed; } table td{ text-overflow:ellipsis;overflow:hidden;white-space: n ...