hdu 1890 splay树

Robotic Sort

Time Limit: 6000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 3340    Accepted Submission(s): 1423

Problem Description
Somewhere deep in the Czech Technical University buildings, there are laboratories for examining mechanical and electrical properties of various materials. In one of yesterday’s presentations, you have seen how was one of the laboratories changed into a new
multimedia lab. But there are still others, serving to their original purposes.

In this task, you are to write software for a robot that handles samples in such a laboratory. Imagine there are material samples lined up on a running belt. The samples have different heights, which may cause troubles to the next processing unit. To eliminate
such troubles, we need to sort the samples by their height into the ascending order.

Reordering is done by a mechanical robot arm, which is able to pick up any number of consecutive samples and turn them round, such that their mutual order is reversed. In other words, one robot operation can reverse the order of samples on positions between
A and B.

A possible way to sort the samples is to find the position of the smallest one (P1) and reverse the order between positions 1 and P1, which causes the smallest sample to become first. Then we find the second one on position P and reverse the order between 2
and P2. Then the third sample is located etc.

hdu 1890  splay树

The picture shows a simple example of 6 samples. The smallest one is on the 4th position, therefore, the robot arm reverses the first 4 samples. The second smallest sample is the last one, so the next robot operation will reverse the order of five samples on
positions 2–6. The third step will be to reverse the samples 3–4, etc.

Your task is to find the correct sequence of reversal operations that will sort the samples using the above algorithm. If there are more samples with the same height, their mutual order must be preserved: the one that was given first in the initial order must
be placed before the others in the final order too.

 
Input
The input consists of several scenarios. Each scenario is described by two lines. The first line contains one integer number N , the number of samples, 1 ≤ N ≤ 100 000. The second line lists exactly N space-separated positive integers, they specify the heights
of individual samples and their initial order.

The last scenario is followed by a line containing zero.

 
Output
For each scenario, output one line with exactly N integers P1 , P1 , . . . PN ,separated by a space.
Each Pi must be an integer (1 ≤ Pi ≤ N ) giving the position of the i-th sample just before the i-th reversal operation.

Note that if a sample is already on its correct position Pi , you should output the number Pi anyway, indicating that the “interval between Pi and Pi ” (a single sample) should be reversed. 

 
Sample Input
6
3 4 5 1 6 2
4
3 3 2 1
0
 
Sample Output
4 6 4 5 6 6
4 2 4 4
 
Source
 
/*
hdu-1890 splay树g
开始是用伸展树保存的值,然后发现如果有相同的进行了交换,然后输出的值就有问题
3 3 2 1 -> 4 2 4 4 但我的是4 2 4 3,因为我每次是去找的某个值的位置,相同值的话就可能找到较小那个值去了
假设排序后b[3] = b[4]= 3,我先处理b[3],然后b[3],b[4]交换,处理b[4]时就出现了bug,然后GG
准确说是对题意的理解上出现了,果然英语弱O__O "… 后来改用数组坐标建树,对于翻转get_kth找到其中第k个位置,然后get_next找出排序后第i大的后面那个,然后打标记即可.
至于求位置,直接把这个点旋转到根,然后计算左儿子的大小即可 hhh-2016-02-21 01:10:21
*/ #include <functional>
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <map>
#include <cmath>
using namespace std;
typedef long long ll;
typedef long double ld;
#define key_value ch[ch[root][1]][0]
const int maxn = 300010; int ch[maxn][2];
int pre[maxn],siz[maxn],num[maxn];
int rev[maxn];
int root,tot,cnt,n;
struct Node
{
int val,id;
}node[maxn]; bool cmp(Node a,Node b)
{
if(a.val != b.val) return a.val < b.val;
else return a.id < b.id;
} void push_up(int r)
{
int lson = ch[r][0],rson = ch[r][1];
siz[r] = siz[lson] + siz[rson] + 1;
} void update_rev(int r)
{
if(!r)return ;
swap(ch[r][0],ch[r][1]);
rev[r] ^= 1;
} void push_down(int r)
{
if(rev[r])
{
update_rev(ch[r][0]);
update_rev(ch[r][1]);
rev[r] = 0;
}
} void NewNode(int &r,int far,int k)
{
r = k;
pre[r] = far;
ch[r][0] = ch[r][1] = 0;
siz[r] = 1;
rev[r] = 0;
} void rotat(int x,int kind)
{
int y = pre[x];
push_down(y);
push_down(x);
ch[y][!kind] = ch[x][kind];
pre[ch[x][kind]] = y;
if(pre[y])
ch[pre[y]][ch[pre[y]][1]==y] = x;
pre[x] = pre[y];
ch[x][kind] = y;
pre[y] = x;
push_up(y);
} void build(int &x,int l,int r,int far)
{
if(l > r) return ;
int mid = (l+r) >>1;
NewNode(x,far,mid);
build(ch[x][0],l,mid-1,x);
build(ch[x][1],mid+1,r,x);
push_up(x);
} void splay(int r,int goal)
{
push_down(r);
while(pre[r] != goal)
{
if(pre[pre[r]] == goal)
{
push_down(pre[r]);
push_down(r);
rotat(r,ch[pre[r]][0] == r);
}
else
{
push_down(pre[pre[r]]);
push_down(pre[r]);
push_down(r);
int y = pre[r];
int kind = ch[pre[y]][0] == y;
if(ch[y][kind] == r)
{
rotat(r,!kind);
rotat(r,kind);
}
else
{
rotat(y,kind);
rotat(r,kind);
}
}
}
push_up(r);
if(goal == 0)
root = r;
} int get_kth(int r,int k)
{
push_down(r);
int t = siz[ch[r][0]] + 1;
if(k == t)return r;
if(t > k) return get_kth(ch[r][0],k);
else return get_kth(ch[r][1],k-t);
} int get_next(int r)
{
push_down(r);
if(ch[r][1] == 0)return -1;
r = ch[r][1];
while(ch[r][0])
{
r = ch[r][0];
push_down(r);
}
return r;
} void ini(int n)
{
root = 0;
ch[root][0] = ch[root][1] = pre[root] = siz[root] = num[root] = 0 ;
NewNode(root,0,n+1);
NewNode(ch[root][1],root,n+2);
build(key_value,1,n,ch[root][1]); push_up(ch[root][1]);
push_up(root);
} int main()
{
int q,T;
int cas =1;
while(scanf("%d",&n) != EOF)
{
if(!n)
break;
for(int i=1; i <= n; i++)
{
scanf("%d",&node[i].val);
node[i].id = i;
}
sort(node+1,node+n+1,cmp);
ini(n);
for(int i = 1; i <= n; i++)
{
splay(node[i].id,0);
printf("%d",siz[ch[root][0]]);
if(i != n) printf(" ");
else printf("\n");
splay(get_kth(root,i),0);
splay(get_next(node[i].id),root);
update_rev(key_value);
}
}
return 0;
}

  

上一篇:基于Docker搭建大数据集群(二)基础组件配置


下一篇:Mysql性能优化:什么是索引下推?