# [BZOJ 1078][SCOI2008]斜堆（可并堆）

Description

Solution

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
using namespace std;
int n,root,ans[150];
struct Node
{
int lch,rch,father;
Node():lch(-1),rch(-1),father(-1){}
}heap[150];
int solve()
{
int p=root;
while(heap[p].rch!=-1)p=heap[p].lch;
if(heap[p].lch!=-1&&heap[heap[p].lch].lch==-1&&heap[heap[p].lch].rch==-1)
p=heap[p].lch;
int t=p;
if(p==root)
{
root=heap[p].lch;
heap[heap[p].lch].father=-1;
return p;
}
if(heap[p].lch!=-1)
heap[heap[p].lch].father=heap[p].father,heap[heap[p].father].lch=heap[p].lch;
else heap[heap[p].father].lch=-1;
while(heap[t].father!=-1)
{
t=heap[t].father;
swap(heap[t].lch,heap[t].rch);
}
return p;
}
int main()
{
scanf("%d",&n);
for(int i=1;i<=n;i++)
{
int x;
scanf("%d",&x);
if(x>=100)heap[x-100].rch=i,heap[i].father=x-100;
else heap[x].lch=i,heap[i].father=x;
}
root=0;
for(int i=n;i>=0;i--)
ans[i]=solve();
for(int i=0;i<=n;i++)
printf("%d ",ans[i]);
return 0;
} 

posted @ 2017-05-20 19:59  Zars19  阅读(189)  评论(0编辑  收藏  举报