点此看题面

大致题意: 给你\(n\)个数。第一次找到最小值所在位置\(P_1\),翻转\([1,P_1]\),第二次找到剩余数中最小值所在位置\(P_2\),翻转\([2,P_2]\),以此类推。求\(P_1,P_2,...,P_n\)的值。

关于洛谷上的四倍经验

这题在洛谷上是一道四倍经验题(目前看来是两黑两紫):

初始化

这题应该是一道比较裸的\(Splay\)题。

首先,我们将原数组排序一遍,记下每一次操作的位置

有一个细节,题目要求相同值要先取位置靠前的(没注意到这点结果狂\(WA\)不止)。

然后便是建树。

注意,\(Splay\)建树的过程中我们一般会在序列左右各加一个多余节点,方便后面取出一段区间进行操作。

操作

每一次操作,我们把要操作的位置先\(Splay\)到根,然后就可以得出答案即为此时左子树的\(Size\),记其为\(ans\)。

注意是\(Size\)而不是\(Size+1\),要考虑到我们在序列左边加的那个多余节点已经使\(Size\)比实际存在的节点个数多\(1\)了。

然后,按照题目要求,我们要翻转区间\([i,ans]\)。

直接将\(i\)号节点和\(ans+2\)号节点分别旋到根节点和根节点的右儿子,然后翻转根节点的右儿子的左儿子即可。

代码

#include<bits/stdc++.h>
#define Type template<typename I>
#define N 100000
#define swap(x,y) (x^=y^=x^=y)
#define INF 1e9
using namespace std;
int n;
struct Data
{
int pos,val;
inline friend bool operator < (Data x,Data y) {return x.val^y.val?x.val<y.val:x.pos<y.pos;}
}a[N+5];
class Class_FIO
{
private:
#define Fsize 100000
#define tc() (A==B&&(B=(A=Fin)+fread(Fin,1,Fsize,stdin),A==B)?EOF:*A++)
#define pc(ch) (FoutSize^Fsize?Fout[FoutSize++]=ch:(fwrite(Fout,1,Fsize,stdout),Fout[(FoutSize=0)++]=ch))
#define Isi(x) (typeid(x).name()==typeid(1).name())
#define Isc(x) (typeid(x).name()==typeid('a').name())
int Top,FoutSize;char ch,*A,*B,Fin[Fsize],Fout[Fsize],Stack[Fsize];
public:
Class_FIO() {A=B=Fin;}
Type inline void read(I& x) {x=0;while(!isdigit(ch=tc()));while(x=(x<<3)+(x<<1)+(ch&15),isdigit(ch=tc()));}
Type inline void write(I x)
{
if(Isi(x)) {while(Stack[++Top]=x%10+48,x/=10);while(Top) pc(Stack[Top--]);}
if(Isc(x)) pc(x);
}
template<typename I,typename... A> inline void read(I& x,A&... y) {read(x),read(y...);}
template<typename I,typename... A> inline void write(I x,A... y) {write(x),write(y...);}
inline void clear() {fwrite(Fout,1,FoutSize,stdout),FoutSize=0;}
}F;
class Class_Splay//Splay
{
private:
#define SIZE N
#define PushUp(x) (node[x].Size=node[node[x].Son[0]].Size+node[node[x].Son[1]].Size+1)
#define Rever(x) (swap(node[x].Son[0],node[x].Son[1]),node[x].Rev^=1)
#define PushDown(x) (node[x].Rev&&(Rever(node[x].Son[0]),Rever(node[x].Son[1]),node[x].Rev=0))
#define Which(x) (node[node[x].Father].Son[1]==x)
#define Connect(x,y,d) (node[node[x].Father=y].Son[d]=x)
#define Split(x,y) (Splay(get_pos(x),rt),Splay(get_pos((y)+2),node[rt].Son[1]),node[node[rt].Son[1]].Son[0])
int rt;
struct Tree
{
int Size,Rev,Father,Son[2];
}node[SIZE+5];
inline void Rotate(int x,int& k)
{
register int fa=node[x].Father,pa=node[fa].Father,d=Which(x);PushDown(fa),PushDown(x),
(fa^k?node[pa].Son[Which(fa)]=x:k=x),node[x].Father=pa,Connect(node[x].Son[d^1],fa,d),Connect(fa,x,d^1),PushUp(fa),PushUp(x);
}
inline void Splay(int x,int& k) {register int fa;while(x^k) fa=node[x].Father,fa^k&&(Rotate(Which(x)^Which(fa)?x:fa,k),0),Rotate(x,k);}
inline void Build(int l,int r,int& rt)
{
register int mid=l+r>>1;
if(node[rt=mid].Size=1,!(l^r)) return;
l<mid&&(Build(l,mid-1,node[rt].Son[0]),node[node[rt].Son[0]].Father=rt),
r>mid&&(Build(mid+1,r,node[rt].Son[1]),node[node[rt].Son[1]].Father=rt),
PushUp(rt);
}
inline int get_pos(int rk)
{
register int x=rt;
while(x)
{
if(PushDown(x),node[node[x].Son[0]].Size>=rk) x=node[x].Son[0];
else if(!(rk-=node[node[x].Son[0]].Size+1)) return x;
else x=node[x].Son[1];
}
}
public:
inline void Init(int len) {Build(1,len+2,rt);}
inline int GetAns(int x)
{
register int k,ans;
Splay(a[x].pos+1,rt),ans=node[node[rt].Son[0]].Size,k=Split(x,ans),Rever(k);//找到ans,然后翻转
return ans;//返回答案
}
#undef SIZE
}Splay;
int main()
{
register int i,p;
for(F.read(n),i=1;i<=n;++i) F.read(a[a[i].pos=i].val);//读入
for(sort(a+1,a+n+1),Splay.Init(n),i=1;i<=n;++i) F.write(p=Splay.GetAns(i),' ');//初始化排序+依次操作
return F.clear(),0;
}

【BZOJ3506】[CQOI2014] 排序机械臂(Splay)的更多相关文章

  1. [BZOJ3506] [Cqoi2014] 排序机械臂 (splay)

    Description 同OJ1552 Input Output Sample Input Sample Output HINT Source Solution Q:哎不是同一道题吗为什么分两篇博客来 ...

  2. bzoj3506 [Cqoi2014]排序机械臂

    bzoj3506 此题是一道比较简单的spaly题目. 用splay维护序列,将每个点排到对应的位置之后删除,这样比较容易区间翻转. 我的指针写法在洛谷上AC了,但在bzoj上RE. #include ...

  3. BZOJ1552[Cerc2007]robotic sort&BZOJ3506[Cqoi2014]排序机械臂——非旋转treap

    题目描述 输入 输入共两行,第一行为一个整数N,N表示物品的个数,1<=N<=100000. 第二行为N个用空格隔开的正整数,表示N个物品最初排列的编号. 输出 输出共一行,N个用空格隔开 ...

  4. [bzoj1552][Cerc2007]robotic sort&&[bzoj3506][Cqoi2014]排序机械臂

    非常垃圾的一道平衡树,结果被日了一天.很难受嗷嗷嗷 首先不得不说网上的题解让我这个本来就不熟悉平衡树的彩笔很难受——并不好理解. 还好Sinogi大佬非常的神,一眼就切掉了,而且用更加美妙的解法. 题 ...

  5. 洛谷P3165 [CQOI2014]排序机械臂 Splay维护区间最小值

    可以将高度定义为小数,这样就完美的解决了优先级的问题. Code: #include<cstdio> #include<algorithm> #include<cstri ...

  6. 【BZOJ3506】排序机械臂(Splay)

    [BZOJ3506]排序机械臂(Splay) 题面 神TMBZOJ没有题面,感谢SYC的题面 洛谷的题面也不错 题解 对于每次旋转的物体 显然可以预处理出来 现在只要模拟旋转操作就行了 至于在哪里放标 ...

  7. P3165 [CQOI2014]排序机械臂

    题目描述 为了把工厂中高低不等的物品按从低到高排好序,工程师发明了一种排序机械臂.它遵循一个简单的排序规则,第一次操作找到高度最低的物品的位置 P1P_1P1​ ,并把左起第一个物品至 P1P_1P1 ...

  8. 洛谷P3165 [CQOI2014]排序机械臂

    题目描述 为了把工厂中高低不等的物品按从低到高排好序,工程师发明了一种排序机械臂.它遵循一个简单的排序规则,第一次操作找到摄低的物品的位置P1,并把左起第一个至P1间的物品反序:第二次找到第二低的物品 ...

  9. 【洛谷 P3165】 [CQOI2014]排序机械臂 (Splay)

    题目链接 debug了\(N\)天没debug出来,原来是找后继的时候没有pushdown... 众所周知,,Splay中每个编号对应的节点的值是永远不会变的,因为所有旋转.翻转操作改变的都是父节点和 ...

随机推荐

  1. docker 部署公司阿里云服务器 (一)

    持续更新... 背景环境: 阿里云ecs服务器 centos7.4          公网地址:xx.xx.xx.xx      内网地址:172.16.77.4 阿里云RDS 阿里云 Redis 第 ...

  2. Django media的设置

    django在定义模型时需要一些上传的文件,例如图片 class Banner(models.Model): """ 轮播图models titles 标题 images ...

  3. 主席树-----动态开点,不hash

    POJ - 2104 第k大 #include <cstdio> #include <cstdlib> #include <cstring> #include &l ...

  4. C# List(T).Reverse 方法 顺序反转

    using System; using System.Collections.Generic; public class Example { public static void Main() { L ...

  5. [转]jQuery TextBox Water Mark with asp.net

    本文转自:http://naspinski.net/post/jQuery-TextBox-Water-Mark-with-aspnet.aspx I stole majority of this c ...

  6. Murano Weekly Meeting 2016.06.28

    Meeting time: 2016.June.28 1:00~2:00 Chairperson:  Kirill Zaitsev, from Mirantis Meeting summary: 1. ...

  7. Collections练习之对字符串先折半,再取最长的一个

    不多说,直接上干货! 代码需求 由 [aa, abcde, cba, cba, nbaa, zzz] 变成 max=abcde CollectionsDemo.java package zhouls. ...

  8. 输入一个正整数n (1<n<=10),生成 1个 n*n 方阵 求出对角线之和

    #define _CRT_SECURE_NO_WARNINGS #include <Windows.h> #include <stdio.h> #include <std ...

  9. c++隐式类型转换和explicit

    什么是隐式转换? 众所周知,C++的基本类型中并非完全的对立,部分数据类型之间是可以进行隐式转换的. 所谓隐式转换,是指不需要用户干预,编译器私下进行的类型转换行为.很多时候用户可能都不知道进行了哪些 ...

  10. ubuntu下mysql安装(server、client、dev),开启、停止和重启,及常见错误

    转自:ubuntu下mysql安装(server.client.dev),开启.停止和重启,及常见错误 1. 在ubuntu下安装server和client很简单: (1)安装server apt-g ...