Robotic Sort
Time limit: 2 Seconds Memory limit: 32768K
Total Submit: 12 Accepted Submit: 4
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 P2 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, P2, . . .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
Problem Source: The 2007 ACM Central Europe Programming Contest
1
/**//*
2
* Sample solution to the Sort problem.
3
* Central Europe Regional Contest 2007.
4
*
5
* Zdenek Dvorak, 2007
6
*/
7
8
#include <stdio.h>
9
#include <stdlib.h>
10
11
#define MAXN 200000
12
static int n, input[MAXN], ipermut[MAXN], permut[MAXN];
13
14
struct node
15

{
16
int reverse, size, present;
17
struct node *l, *r, *f;
18
};
19
20
static struct node path_nodes[MAXN];
21
static void
22
dump_tree (struct node *t, int reverse)
23

{
24
struct node *l, *r;
25
26
reverse ^= t->reverse;
27
28
l = reverse ? t->r : t->l;
29
r = reverse ? t->l : t->r;
30
31
if (l)
32
dump_tree (l, reverse);
33
34
if (t->present)
35
printf ("%d ", (int) (t - path_nodes));
36
37
if (r)
38
dump_tree (r, reverse);
39
}
40
41
static void
42
dump_path (void)
43

{
44
int i;
45
46
printf ("\n");
47
for (i = 0; i < n; i++)
48
if (!path_nodes[i].f)
49
{
50
printf ("--- ");
51
dump_tree (&path_nodes[i], 0);
52
}
53
54
printf (" ---\n");
55
}
56
57
static void
58
clean_reverse (struct node *tree)
59

{
60
struct node *t;
61
62
if (!tree->reverse)
63
return;
64
65
t = tree->l; tree->l = tree->r; tree->r = t;
66
if (tree->l)
67
tree->l->reverse = !tree->l->reverse;
68
if (tree->r)
69
tree->r->reverse = !tree->r->reverse;
70
tree->reverse = 0;
71
}
72
73
static void
74
fix_size (struct node *t)
75

{
76
int lsize = t->l ? t->l->size : 0;
77
int rsize = t->r ? t->r->size : 0;
78
79
t->size = lsize + rsize + t->present;
80
}
81
82
static void
83
rotate (struct node *t, struct node *f)
84

{
85
struct node **ftlink, **alink, *a;
86
87
if (f->r == t)
88
{
89
ftlink = &f->r;
90
alink = &t->l;
91
}
92
else
93
{
94
ftlink = &f->l;
95
alink = &t->r;
96
}
97
98
t->f = f->f;
99
if (f->f)
100
{
101
if (f->f->l == f)
102
f->f->l = t;
103
else
104
f->f->r = t;
105
}
106
107
a = *alink;
108
*ftlink = a;
109
if (a)
110
a->f = f;
111
112
*alink = f;
113
f->f = t;
114
115
fix_size (f);
116
fix_size (t);
117
}
118
119
static void
120
zigzig (struct node *t, struct node *f, struct node *gf)
121

{
122
struct node **gfflink, **ftlink, **blink, **clink, *b, *c;
123
if (gf->r == f)
124
{
125
gfflink = &gf->r;
126
ftlink = &f->r;
127
blink = &f->l;
128
clink = &t->l;
129
}
130
else
131
{
132
gfflink = &gf->l;
133
ftlink = &f->l;
134
blink = &f->r;
135
clink = &t->r;
136
}
137
138
b = *blink;
139
c = *clink;
140
141
t->f = gf->f;
142
if (gf->f)
143
{
144
if (gf->f->l == gf)
145
gf->f->l = t;
146
else
147
gf->f->r = t;
148
}
149
150
*clink = f;
151
f->f = t;
152
153
*blink = gf;
154
gf->f = f;
155
156
*gfflink = b;
157
if (b)
158
b->f = gf;
159
160
*ftlink = c;
161
if (c)
162
c->f = f;
163
164
fix_size (gf);
165
fix_size (f);
166
fix_size (t);
167
}
168
169
static void
170
zigzag (struct node *t, struct node *f, struct node *gf)
171

{
172
struct node **gfflink, **ftlink, **blink, **clink, *b, *c;
173
if (gf->l == f)
174
{
175
gfflink = &gf->l;
176
ftlink = &f->r;
177
blink = &t->l;
178
clink = &t->r;
179
}
180
else
181
{
182
gfflink = &gf->r;
183
ftlink = &f->l;
184
blink = &t->r;
185
clink = &t->l;
186
}
187
188
b = *blink;
189
c = *clink;
190
191
t->f = gf->f;
192
if (gf->f)
193
{
194
if (gf->f->l == gf)
195
gf->f->l = t;
196
else
197
gf->f->r = t;
198
}
199
200
*clink = gf;
201
gf->f = t;
202
203
*blink = f;
204
f->f = t;
205
206
*gfflink = c;
207
if (c)
208
c->f = gf;
209
210
*ftlink = b;
211
if (b)
212
b->f = f;
213
214
fix_size (gf);
215
fix_size (f);
216
fix_size (t);
217
}
218
219
static void
220
double_rotate (struct node *t, struct node *f, struct node *gf)
221

{
222
if ((gf->r == f) == (f->r == t))
223
zigzig (t, f, gf);
224
else
225
zigzag (t, f, gf);
226
}
227
228
static void
229
splay (struct node *t)
230

{
231
struct node *f, *gf;
232
233
while (t->f)
234
{
235
f = t->f;
236
237
gf = f->f;
238
if (!gf)
239

{
240
clean_reverse (f);
241
clean_reverse (t);
242
rotate (t, f);
243
return;
244
}
245
clean_reverse (gf);
246
clean_reverse (f);
247
clean_reverse (t);
248
249
double_rotate (t, f, gf);
250
}
251
252
clean_reverse (t);
253
}
254
255
static int
256
rotfirst_and_delete (struct node *t)
257

{
258
int pos;
259
260
splay (t);
261
/**//*printf ("After splay: ");
262
dump_path();
263
printf ("\n");*/
264
if (t->l)
265
{
266
t->l->reverse = !t->l->reverse;
267
pos = t->l->size;
268
}
269
else
270
pos = 0;
271
272
t->present = 0;
273
t->size--;
274
275
return pos;
276
}
277
278
static int
279
cmp_input (const void *a, const void *b)
280

{
281
int va = *(int *) a;
282
int vb = *(int *) b;
283
284
if (input[va] != input[vb])
285
return input[va] - input[vb];
286
287
return va - vb;
288
}
289
290
int
291
main (void)
292

{
293
int i;
294
295
while (1)
296
{
297
scanf ("%d", &n);
298
if (!n)
299
return 0;
300
301
for (i = 0; i < n; i++)
302
scanf ("%d", &input[i]);
303
304
for (i = 0; i < n; i++)
305
ipermut[i] = i;
306
307
qsort (ipermut, n, sizeof (int), cmp_input);
308
309
for (i = 0; i < n; i++)
310
permut[ipermut[i]] = i;
311
312
/**//* for (i = 0; i < n; i++)
313
printf ("%d ", permut[i]);
314
printf ("\n");*/
315
for (i = 0; i < n; i++)
316

{
317
path_nodes[permut[i]].reverse = 0;
318
path_nodes[permut[i]].size = i + 1;
319
path_nodes[permut[i]].present = 1;
320
path_nodes[permut[i]].l = i > 0 ? &path_nodes[permut[i-1]] : NULL;
321
path_nodes[permut[i]].r = NULL;
322
path_nodes[permut[i]].f = i < n - 1 ? &path_nodes[permut[i+1]] : NULL;
323
}
324
325
for (i = 0; i < n; i++)
326

{
327
/**//*dump_path ();*/
328
printf ("%d%c", rotfirst_and_delete (&path_nodes[i]) + i + 1, i+1 < n ? ' ' : '\n');
329
}
330
}
331
}
332