刷题总结:排序机械臂(石室中学oj)(splay)

题目:

题目描述

为了把工厂中高低不等的物品按从低到高排好序,工程师发明了一种排序机械臂。它遵循一个简单的排序规则,第一次操作找到最低的物品位置 P1,并把从左起第 1 个至第 P1 个之间的物品反序;第二次找到第二低的物品的位置 P2,并把左起第二个至第 P2 个之间的物品反序……最终所有的物品都会被排好序。 

上图给出一个示例,第一次操作前,最低物品在位置 4,于是把第 1 至第 4 个物品反序;第二次操作前,第二低的物品在位置 6,于是把第 2 至第 6 的物品反序……

你的任务是编写一个程序,确定操作序列,即每次操作前第 i 低的物品所在的位置Pi,以便机械臂工作。需要注意的是,如果有高度相同的物品,必须保证排序后他们的相对位置关系与初始时相同。 

输入格式

第一行包含一个正整数 n,表示需要排序的物品数量。 
第二行包含 n 和空格分隔的整数 ai,表示每个物品的高度。 

输出格式

输出一行包含 n 个空格分隔的整数 Pi。 

样例数据 1

输入  [复制]

 

 


3 4 5 1 6 2

输出

4 6 4 5 6 6

样例数据 2

输入  [复制]

 

 


3 3 2 1

输出

4 2 4 4

备注

【数据范围】
对于 30% 的数据:1≤n≤1000 
对于 100% 的数据:1≤n≤100000,1≤ai≤2×109 

心得:

  利用splay进行区间反转模版题:若要反转i-j内的区间(包括i,j)先找到下标为i-1的点splay到根节点,再找到j+1的点splay到i-1下方,将j+1的左子树的子树进行swap即可(注意打标记)

代码:

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<ctime>
#include<cctype>
#include<string>
#include<cstring>
#include<algorithm>
using namespace std;
const int N=100005;
int father[N],son[N][2],tag[N],size[N],root,tot,num[N];
int n,rank[N];
struct node
{  
  int x,id;
}a[N];
int R()
{
  char c;int f=0;
  for(c=getchar();c<'0'||c>'9';c=getchar());
  for(;c<='9'&&c>='0';c=getchar())
    f=(f<<3)+(f<<1)+c-'0';
  return f;
}
bool cmp(node a,node b)
{
  if(a.x!=b.x)
    return a.x<b.x;
  else  return a.id<b.id;
}
inline void pushdown(int now)
{
  if(tag[now]&&now)
  {
    swap(son[now][0],son[now][1]);
    tag[son[now][0]]^=1;tag[son[now][1]]^=1;tag[now]=0;
  }
}
inline void update(int now)
{
  if(now)
  {
    size[now]=1;
    pushdown(now);
    if(son[now][0]) size[now]+=size[son[now][0]];
    if(son[now][1]) size[now]+=size[son[now][1]];
  }
}
inline int get(int now)
{
  return son[father[now]][1]==now;
}
inline void rotate(int now)
{
  pushdown(father[now]),pushdown(now);
  int fa=father[now],ofa=father[fa],which=get(now);
  son[fa][which]=son[now][which^1],father[son[fa][which]]=fa;
  son[now][which^1]=fa,father[fa]=now,father[now]=ofa;
  if(ofa)  son[ofa][son[ofa][1]==fa]=now;
  update(fa);
  update(now);
}
inline void splay(int now,int to)
{
  while(father[now]!=to)
  {
    if(father[father[now]]!=to)
      rotate(get(father[now])==get(now)?father[now]:now);
    rotate(now);
  }
  if(!to)  root=now;
}
inline void insert(int x)
{
  int now=root,last=0;
  while(true)
  {
    pushdown(now);
    if(!now)
    {
      now=++tot;size[now]=1;num[rank[x]]=tot;father[now]=last;
      if(last)
        son[last][1]=now;update(last);
      splay(now,0);
      break;
    }  
    last=now;
    now=son[now][1];
  }
}
inline int find(int x)
{
  int now=root;
  while(true)
  {
    pushdown(now);
    if(x<=size[son[now][0]])  now=son[now][0];
    else
    {
      int temp=size[son[now][0]]+1;
      if(x==temp)  {return now;}
      x-=temp;now=son[now][1];
    }
  }
}
int main()
{
  //freopen("a.in","r",stdin);
  n=R();
  for(int i=1;i<=n;i++)
    a[i].x=R(),a[i].id=i;  
  sort(a+1,a+n+1,cmp);
  for(int i=1;i<=n;i++)  rank[a[i].id+1]=i;
  for(int i=1;i<=n+2;i++)  insert(i);
  for(int i=1;i<=n;i++)
  {
    splay(num[i],0);
    int ans=size[son[root][0]];cout<<ans<<" ";
    if(i==n)  break;
    int st,ed,l,r;
    st=i;ed=ans+2;
    if(st>ed)  swap(st,ed);
    l=find(st);r=find(ed);
    splay(l,0);splay(r,root);
    tag[son[son[root][1]][0]]^=1;
  }
  return 0;
}

 

posted @ 2017-03-20 19:18  AseanA  阅读(238)  评论(0编辑  收藏  举报