對應HDU題目:點選打開連結
Robotic Sort
Time Limit: 6000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2854 Accepted Submission(s): 1245
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.

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 2008 “Shun Yu Cup” Zhejiang Collegiate Programming Contest - Warm Up(2)
題意:
給n個數,每次将第i(1 <= i <= n)個位置到第i小的數所在位置之間的數進行翻轉,輸出的是第i小的數所在的位置
思路:
首先按元素升序對下标排序:
排序前
下标:1 2 3 4 5 6
元素:3 4 5 1 6 2
排序後
元素:1 2 3 4 5 6
下标:4 6 1 2 3 5
伸展數記錄下标的值,初始時中序周遊是1~n(其實數組型伸展樹的結點号就是下标),這樣按順序每次把排序後的下标伸展到根,然後給左子樹加個翻轉标志,輸出sz[T] + i(sz[T]表示以T為根的子樹的結點數,i從1開始),最後把根删除掉就行了(删除方法是把T(T為整棵樹的根)的左子樹右下角的結點翻轉到左子樹的根,并成為新的T,這時右子樹肯定為空,接上原T的右子樹)
注意數組型伸展操作splay(x, T)是可以直接定位結點位置的,是以不能直接向上伸展,因為上面可能有翻轉标志沒有下傳,是以要先沿x走到T,并記錄路徑,再從T沿路徑把可能有的标記下傳到x後才可以做伸展。
C++可AC,G++就RE的代碼:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define N 100005
#define nil (0x7fffffff)
typedef struct
{
int id, val;
}Node;
Node a[N], t[N];
int Left[N];
int Right[N];
int fa[N];
int sz[N]; //以i為根的子樹的結點數
bool flag[N];
int next[N]; //記錄從待翻轉結點到根的路徑
void Push_down(int T)
{
if(nil == T) return;
if(flag[T]){
int tmp = Right[T];
Right[T] = Left[T];
Left[T] = tmp;
flag[T] = 0;
if(nil != Left[T]) flag[Left[T]] ^= 1;
if(nil != Right[T]) flag[Right[T]] ^= 1;
}
}
//初始為一條鍊
void Init(int &T, int n)
{
bool k = 0;
int cur, pre;
for(cur = n; cur > 0; cur--){
fa[cur] = Left[cur] = Right[cur] = nil;
sz[cur] = 1;
flag[cur] = 0;
if(k){
Right[cur] = pre;
fa[pre] = cur;
sz[cur] = sz[pre] + 1;
}
if(!k) k = 1;
pre = cur;
}
T = cur + 1;
}
void R_rotate(const int x)
{
const int y = fa[x];
const int z = fa[y];
const int k = Right[x];
int sx = sz[x], sy = sz[y], sk = 0;
if(nil != k) sk = sz[k];
Left[y] = k;
Right[x] = y;
if(nil != z){
if(y == Left[z]) Left[z] = x;
else Right[z] = x;
}
if(nil != k) fa[k] = y;
fa[y] = x;
fa[x] = z;
sz[y] = sy - sx + sk;
sz[x] = sx - sk + sz[y];
}
void L_rotate(const int x)
{
const int y = fa[x];
const int z = fa[y];
const int k = Left[x];
int sx = sz[x], sy = sz[y], sk = 0;
if(nil != k) sk = sz[k];
Right[y] = k;
Left[x] = y;
if(nil != z){
if(y == Right[z]) Right[z] = x;
else Left[z] = x;
}
if(nil != k) fa[k] = y;
fa[y] = x;
fa[x] = z;
sz[y] = sy - sx + sk;
sz[x] = sx - sk + sz[y];
}
void Splay(int x, int &T)
{
if(nil == T) return;
int p, end;
end = fa[T];
p = x;
while(T != p){ //記錄從x到T的路徑
next[fa[p]] = p;
p = fa[p];
}
for(p = T; ; p = next[p]){ //沿路徑下傳翻轉标記
Push_down(p);
if(p == x) break;
}
while(end != fa[x])
{
p = fa[x];
if(end == fa[p]){ //p是根結點
if(x == Left[p]) R_rotate(x);
else L_rotate(x);
break;
}
//p不是根結點
if(x == Left[p]){
if(p == Left[fa[p]]){
R_rotate(p); //LL
R_rotate(x); //LL
}
else{
R_rotate(x); //RL
L_rotate(x);
}
}
else{
if(p == Right[fa[p]]){ //RR
L_rotate(p);
L_rotate(x);
}
else{ //LR
L_rotate(x);
R_rotate(x);
}
}
}
T = x;
}
//歸并排序
void Merge_sort(Node *A, int l, int r, Node *T) //[l, r)
{
if(r - l < 2) return;
int m = l + (r - l) / 2;
Merge_sort(A, l, m, T);
Merge_sort(A, m, r, T);
int p = l, i = l, q = m;
while(p < m || q < r)
{
if(q >= r || (p < m && A[p].val <= A[q].val)){
T[i].val = A[p].val;
T[i++].id = A[p++].id;
}
else{
T[i].val = A[q].val;
T[i++].id = A[q++].id;
}
}
for(i = l; i < r; i++){
A[i].val = T[i].val;
A[i].id = T[i].id;
}
}
void Delete(int &T)
{
Push_down(T);
int l, r, x;
l = Left[T];
r = Right[T];
if(nil == l && nil == r){
T = nil;
return;
}
if(nil != l) fa[l] = nil;
if(nil != r) fa[r] = nil;
if(nil == l){ //沒有左兒子
T = r;
return;
}
x = l;
Push_down(x);
while(nil != Right[x]){
x = Right[x]; //x為T的左子樹的中序最大值結點
Push_down(x);
}
Splay(x, l); //把x伸展到左子樹的根結點
T = l; //把左子樹的根作為整棵樹的根,此時T沒有右子樹
//接上右子樹
Right[T] = r;
sz[T] += sz[r];//更新結點數
if(nil != r) fa[r] = T;
}
int main()
{
//freopen("in.txt","r",stdin);
int n, i;
int T;
while(scanf("%d", &n), n)
{
T = nil;
Init(T, n);
for(i = 1; i <= n; i++){
scanf("%d", &a[i].val);
a[i].id = i;
}
Merge_sort(a, 1, n + 1, t);
for(i = 1; i <= n; i++){
Splay(a[i].id, T);
if(nil != Left[T])
flag[Left[T]] ^= 1; //翻轉左子樹
printf("%d", sz[Left[T]] + i); //輸出位置
if(i < n) printf(" ");
else printf("\n");
Delete(T); //删除根結點
}
}
return 0;
}