• HDU 1890 Robotic Sort


    Robotic Sort

    Time Limit: 2000ms
    Memory Limit: 32768KB
    This problem will be judged on HDU. Original ID: 1890
    64-bit integer IO format: %I64d      Java class name: Main
     
    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

     
    解题:splay
      1 #include <bits/stdc++.h>
      2 using namespace std;
      3 typedef pair<int,int> pii;
      4 const int maxn = 200010;
      5 struct SplayTree{
      6     int ch[maxn][2],fa[maxn],sz[maxn],root,tot;
      7     int val[maxn],id[maxn],flip[maxn],mp[maxn];
      8     inline void pushdown(int x){
      9         if(flip[x]){
     10             flip[ch[x][0]] ^= 1;
     11             flip[ch[x][1]] ^= 1;
     12             swap(ch[x][0],ch[x][1]);
     13             flip[x] = 0;
     14         }
     15     }
     16     inline void pushup(int x){
     17         sz[x] = sz[ch[x][0]] + sz[ch[x][1]] + 1;
     18     }
     19     void newnode(int &x,int key,int f){
     20         x = ++tot;
     21         flip[x] = ch[x][0] = ch[x][1] = 0;
     22         fa[x] = f;
     23         sz[x] = 1;
     24         val[x] = key;
     25     }
     26     void rotate(int x,int kd){
     27         int y = fa[x];
     28         pushdown(y);
     29         pushdown(x);
     30         ch[y][kd^1] = ch[x][kd];
     31         fa[ch[x][kd]] = y;
     32         fa[x] = fa[y];
     33         fa[y] = x;
     34         ch[x][kd] = y;
     35         if(fa[x]) ch[fa[x]][y == ch[fa[x]][1]] = x;
     36         pushup(y);
     37     }
     38     void splay(int x,int goal){
     39         pushdown(x);
     40         while(fa[x] != goal){
     41             pushdown(fa[fa[x]]);
     42             pushdown(fa[x]);
     43             pushdown(x);
     44             if(fa[fa[x]] == goal) rotate(x,x == ch[fa[x]][0]);
     45             else{
     46                 int y = fa[x],z = fa[y],s = (y == ch[z][0]);
     47                 if(x == ch[y][s]){
     48                     rotate(x,s^1);
     49                     rotate(x,s);
     50                 }else{
     51                     rotate(y,s);
     52                     rotate(x,s);
     53                 }
     54             }
     55         }
     56         if(!goal) root = x;
     57         pushup(x);
     58     }
     59     void select(int k,int goal){
     60         int x = root;
     61         pushdown(x);
     62         while(sz[ch[x][0]] + 1 != k){
     63             if(k < sz[ch[x][0]] + 1) x = ch[x][0];
     64             else{
     65                 k -= sz[ch[x][0]] + 1;
     66                 x = ch[x][1];
     67             }
     68             pushdown(x);
     69         }
     70         splay(x,goal);
     71     }
     72     void build(int &x,int L,int R,int f){
     73         if(L > R) return;
     74         int mid = (L + R)>>1;
     75         newnode(x,num[id[mid]].first,f);
     76         mp[id[mid]] = x;
     77         build(ch[x][0],L,mid-1,x);
     78         build(ch[x][1],mid + 1,R,x);
     79         pushup(x);
     80     }
     81     void init(int n){
     82         val[0] = fa[0] = sz[0] = tot = 0;
     83         ch[0][0] = ch[0][1] = flip[0] = 0;
     84         for(int i = 1; i <= n; ++i){
     85             scanf("%d",&num[i].first);
     86             num[i].second = i;
     87         }
     88         sort(num + 1,num + n + 1);
     89         for(int i = 1; i <= n; ++i) id[num[i].second] = i;
     90         build(root,1,n,0);
     91     }
     92     void removeRoot(){
     93         int x = root;
     94         pushdown(x);
     95         if(ch[root][1]){
     96             root = ch[root][1];
     97             select(1,0);
     98             ch[root][0] = ch[x][0];
     99             if(ch[root][0]) fa[ch[root][0]] = root;
    100         }else root = ch[root][0];
    101         fa[root] = 0;
    102         pushup(root);
    103     }
    104     void solve(int n){
    105         for(int i = 1; i <= n; ++i){
    106             splay(mp[i],0);
    107             printf("%d%c",i + sz[ch[root][0]],i == n?'
    ':' ');
    108             flip[ch[root][0]] ^= 1;
    109             removeRoot();
    110         }
    111     }
    112     pii num[maxn];
    113 }spt;
    114 int main(){
    115     int n;
    116     while(scanf("%d",&n),n){
    117         spt.init(n);
    118         spt.solve(n);
    119     }
    120     return 0;
    121 }
    View Code
  • 相关阅读:
    数据结构实验之栈与队列四:括号匹配(SDUT 2134)
    从 s 点到 t 点的最短路(简单模板)(迪杰斯特拉)
    畅通工程续(HDU 1874)(简单最短路)
    Til the Cows Come Home ( POJ 2387) (简单最短路 Dijkstra)
    顺序表应用7:最大子段和之分治递归法(SDUT 3664)
    Java面向对象4(P~U)
    House Lawn Kattis
    Jumbled String (Kattis
    队列详解及java实现
    栈详解及java实现
  • 原文地址:https://www.cnblogs.com/crackpotisback/p/4874642.html
Copyright © 2020-2023  润新知