青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品

雪竹的天空

theorix

  C++博客 :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理 ::
  34 隨筆 :: 0 文章 :: 20 評論 :: 0 Trackbacks
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
 12static int n, input[MAXN], ipermut[MAXN], permut[MAXN];
 13
 14struct node
 15{
 16int reverse, size, present;
 17struct node *l, *r, *f;
 18}
;
 19
 20static struct node path_nodes[MAXN];
 21static void
 22dump_tree (struct node *t, int reverse)
 23{
 24struct node *l, *r;
 25
 26reverse ^= t->reverse;
 27
 28= reverse ? t->r : t->l;
 29= reverse ? t->l : t->r;
 30
 31if (l)
 32    dump_tree (l, reverse);
 33
 34if (t->present)
 35    printf ("%d ", (int) (t - path_nodes));
 36
 37if (r)
 38    dump_tree (r, reverse);
 39}

 40
 41static void
 42dump_path (void)
 43{
 44int i;
 45
 46printf ("\n");
 47for (i = 0; i < n; i++)
 48    if (!path_nodes[i].f)
 49      {
 50printf ("--- ");
 51dump_tree (&path_nodes[i], 0);
 52      }

 53
 54printf (" ---\n");
 55}

 56
 57static void
 58clean_reverse (struct node *tree)
 59{
 60struct node *t;
 61
 62if (!tree->reverse)
 63    return;
 64
 65= tree->l; tree->= tree->r; tree->= t;
 66if (tree->l)
 67    tree->l->reverse = !tree->l->reverse;
 68if (tree->r)
 69    tree->r->reverse = !tree->r->reverse;
 70tree->reverse = 0;
 71}

 72
 73static void
 74fix_size (struct node *t)
 75{
 76int lsize = t->? t->l->size : 0;
 77int rsize = t->? t->r->size : 0;
 78
 79t->size = lsize + rsize + t->present;
 80}

 81
 82static void
 83rotate (struct node *t, struct node *f)
 84{
 85struct node **ftlink, **alink, *a;
 86
 87if (f->== t)
 88    {
 89      ftlink = &f->r;
 90      alink = &t->l;
 91    }

 92else
 93    {
 94      ftlink = &f->l;
 95      alink = &t->r;
 96    }

 97
 98t->= f->f;
 99if (f->f)
100    {
101      if (f->f->== f)
102f->f->= t;
103      else
104f->f->= t;
105    }

106
107= *alink;
108*ftlink = a;
109if (a)
110    a->= f;
111
112*alink = f;
113f->= t;
114
115fix_size (f);
116fix_size (t);
117}

118
119static void
120zigzig (struct node *t, struct node *f, struct node *gf)
121{
122struct node **gfflink, **ftlink, **blink, **clink, *b, *c;
123if (gf->== f)
124    {
125      gfflink = &gf->r;
126      ftlink = &f->r;
127      blink = &f->l;
128      clink = &t->l;
129    }

130else
131    {
132      gfflink = &gf->l;
133      ftlink = &f->l;
134      blink = &f->r;
135      clink = &t->r;
136    }

137
138= *blink;
139= *clink;
140
141t->= gf->f;
142if (gf->f)
143    {
144      if (gf->f->== gf)
145gf->f->= t;
146      else
147gf->f->= t;
148    }

149
150*clink = f;
151f->= t;
152
153*blink = gf;
154gf->= f;
155
156*gfflink = b;
157if (b)
158    b->= gf;
159
160*ftlink = c;
161if (c)
162    c->= f;
163
164fix_size (gf);
165fix_size (f);
166fix_size (t);
167}

168
169static void
170zigzag (struct node *t, struct node *f, struct node *gf)
171{
172struct node **gfflink, **ftlink, **blink, **clink, *b, *c;
173if (gf->== f)
174    {
175      gfflink = &gf->l;
176      ftlink = &f->r;
177      blink = &t->l;
178      clink = &t->r;
179    }

180else
181    {
182      gfflink = &gf->r;
183      ftlink = &f->l;
184      blink = &t->r;
185      clink = &t->l;
186    }

187
188= *blink;
189= *clink;
190
191t->= gf->f;
192if (gf->f)
193    {
194      if (gf->f->== gf)
195gf->f->= t;
196      else
197gf->f->= t;
198    }

199
200*clink = gf;
201gf->= t;
202
203*blink = f;
204f->= t;
205
206*gfflink = c;
207if (c)
208    c->= gf;
209
210*ftlink = b;
211if (b)
212    b->= f;
213
214fix_size (gf);
215fix_size (f);
216fix_size (t);
217}

218
219static void
220double_rotate (struct node *t, struct node *f, struct node *gf)
221{
222if ((gf->== f) == (f->== t))
223    zigzig (t, f, gf);
224else
225    zigzag (t, f, gf);
226}

227
228static void
229splay (struct node *t)
230{
231struct node *f, *gf;
232
233while (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
252clean_reverse (t);
253}

254
255static int
256rotfirst_and_delete (struct node *t)
257{
258int pos;
259
260splay (t);
261/*printf ("After splay: ");
262dump_path();
263printf ("\n");*/

264if (t->l)
265    {
266      t->l->reverse = !t->l->reverse;
267      pos = t->l->size;
268    }

269else
270    pos = 0;
271
272t->present = 0;
273t->size--;
274
275return pos;
276}

277
278static int
279cmp_input (const void *a, const void *b)
280{
281int va = *(int *) a;
282int vb = *(int *) b;
283
284if (input[va] != input[vb])
285    return input[va] - input[vb];
286
287return va - vb;
288}

289
290int
291main (void)
292{
293int i;
294
295while (1)
296    {
297      scanf ("%d"&n);
298      if (!n)
299return 0;
300
301      for (i = 0; i < n; i++)
302scanf ("%d"&input[i]);
303
304      for (i = 0; i < n; i++)
305ipermut[i] = i;
306
307      qsort (ipermut, n, sizeof (int), cmp_input);
308
309      for (i = 0; i < n; i++)
310permut[ipermut[i]] = i;
311
312/*      for (i = 0; i < n; i++)
313printf ("%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
posted on 2008-08-31 00:49 雪竹的天空( theorix ) 閱讀(551) 評論(0)  編輯 收藏 引用

只有注冊用戶登錄后才能發表評論。
網站導航: 博客園   IT新聞   BlogJava   博問   Chat2DB   管理


青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品
  • <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>
            久色成人在线| 欧美日韩在线大尺度| 国产视频不卡| 欧美一级在线播放| 亚洲欧美在线磁力| 国产性做久久久久久| 久久黄金**| 久久久天天操| 亚洲人成网在线播放| 99re亚洲国产精品| 国产精品日韩在线一区| 久久欧美肥婆一二区| 欧美sm视频| 亚洲视频大全| 欧美一区二区视频免费观看| 亚洲盗摄视频| 正在播放欧美一区| 狠狠色狠狠色综合日日91app| 欧美福利视频在线| 欧美日韩在线视频观看| 久久久精彩视频| 欧美国产激情二区三区| 亚洲欧美日韩在线一区| 久久久国产精品亚洲一区| 亚洲人成毛片在线播放女女| 亚洲在线免费视频| 日韩一区二区精品葵司在线| 国产一区999| 这里只有精品视频在线| 国产日韩一区二区三区在线| 欧美成人中文字幕在线| 欧美三级午夜理伦三级中文幕 | 久久先锋影音| 国产精品99久久不卡二区| 午夜欧美大尺度福利影院在线看| 91久久极品少妇xxxxⅹ软件| 亚洲一区激情| 亚洲六月丁香色婷婷综合久久| 亚洲一区二区三区乱码aⅴ蜜桃女 亚洲一区二区三区乱码aⅴ | 久久综合九色欧美综合狠狠| 欧美日韩精品中文字幕| 久久蜜桃精品| 国产精品亚洲精品| 亚洲精品久久| 136国产福利精品导航网址| 在线亚洲成人| 亚洲美女性视频| 久久久久九九视频| 久久国产99| 乱码第一页成人| 国产精品福利在线观看| 亚洲第一福利在线观看| 狠狠色2019综合网| 欧美一级大片在线观看| 亚洲嫩草精品久久| 欧美日韩精品久久久| 亚洲国内欧美| 亚洲国产高潮在线观看| 久久精品麻豆| 久久精品国产久精国产思思| 国产精品乱码妇女bbbb| 亚洲视频每日更新| 国产精品99久久久久久宅男| 麻豆精品91| 欧美激情无毛| 亚洲精品乱码久久久久久久久| 久久久久久久网| 美女尤物久久精品| 亚洲黄一区二区三区| 久热re这里精品视频在线6| 久久伊人精品天天| 精品成人一区二区三区| 久久综合网色—综合色88| 老司机午夜精品视频在线观看| 黄色精品一区二区| 久久综合九色综合欧美狠狠| 欧美韩日一区二区| 一区二区欧美精品| 欧美日韩一区二区三区在线观看免 | 亚洲人成毛片在线播放| 欧美高清自拍一区| 一区二区三区四区蜜桃| 性欧美办公室18xxxxhd| 国产婷婷色综合av蜜臀av| 久久高清国产| 亚洲国产精品成人综合色在线婷婷| 亚洲经典一区| 欧美体内she精视频在线观看| 亚洲小说春色综合另类电影| 久久久久欧美精品| 亚洲区第一页| 国产精品成人一区二区网站软件 | 亚久久调教视频| 裸体一区二区| aa国产精品| 国产欧美日韩三级| 久久裸体视频| 日韩一级网站| 久久久久在线观看| 亚洲六月丁香色婷婷综合久久| 欧美三级在线播放| 久久人人精品| 亚洲天堂av图片| 麻豆久久婷婷| 亚洲男女自偷自拍图片另类| 黄色精品一区二区| 欧美香蕉大胸在线视频观看| 欧美影院成年免费版| 妖精成人www高清在线观看| 久久天天躁狠狠躁夜夜爽蜜月| 夜夜嗨av一区二区三区四区| 国产一区二区三区在线观看网站| 欧美国产欧美亚洲国产日韩mv天天看完整| 亚洲视频在线一区| 亚洲国产精品久久久久秋霞不卡| 欧美亚洲色图校园春色| 日韩一级在线观看| 国内精品久久久久影院 日本资源| 欧美精品国产精品日韩精品| 久久精品动漫| 亚洲欧美国产视频| 99精品视频一区| 欧美成人国产一区二区| 久久电影一区| 亚洲欧美综合另类中字| 亚洲精品一区二区三区四区高清 | 亚洲免费不卡| 黄色国产精品一区二区三区| 国产精品久线观看视频| 欧美日韩三区四区| 欧美jizz19性欧美| 久久免费少妇高潮久久精品99| 亚洲在线观看| 亚洲素人在线| 99国产精品久久久久久久成人热 | 欧美成人有码| 欧美gay视频激情| 狂野欧美激情性xxxx| 久久狠狠亚洲综合| 欧美在线视频一区二区| 亚洲欧美国产高清va在线播| 一区二区日本视频| 夜夜狂射影院欧美极品| 亚洲伦理自拍| 亚洲精品少妇30p| 亚洲免费av观看| 夜夜嗨av色一区二区不卡| 亚洲精品乱码久久久久| 亚洲激情一区二区| 日韩午夜视频在线观看| 亚洲美女区一区| 99精品黄色片免费大全| av72成人在线| 亚洲欧美日韩国产成人| 欧美亚洲一区二区在线| 久久久久久久999| 久久影院亚洲| 亚洲国产成人精品久久| 亚洲日本免费| 亚洲图片欧美一区| 99国内精品| 日韩视频在线观看国产| 欧美综合国产精品久久丁香| 小嫩嫩精品导航| 国产精品任我爽爆在线播放| 亚洲一区二区三区四区在线观看 | 亚洲欧美日韩国产另类专区| 午夜一区在线| 久久免费国产精品| 欧美成年人视频| 亚洲日本中文字幕区| 在线一区二区三区做爰视频网站| 亚洲欧美日韩中文在线制服| 久久国产精品久久国产精品| 免费观看一级特黄欧美大片| 欧美日韩中文字幕| 国产综合第一页| 亚洲美女视频在线观看| 日韩午夜在线电影| 亚洲一区亚洲| 亚洲电影免费观看高清完整版在线观看 | 国产乱码精品一区二区三区不卡 | 国产日韩欧美在线一区| 136国产福利精品导航| 亚洲一区二区日本| 久久婷婷av| 夜夜嗨av一区二区三区四区| 欧美亚洲视频在线看网址| 欧美精品国产一区二区| 国产午夜亚洲精品不卡| 亚洲精品美女在线| 久久久精品999| 99热这里只有精品8| 久久久久国色av免费看影院| 欧美偷拍一区二区| 亚洲国产欧美一区| 久久国产欧美| 一区二区三欧美| 欧美成人午夜激情视频| 国产亚洲精品福利|