hdu 1890 Splay区间最小值、区间翻转

来源:互联网 发布:nginx apache ssl配置 编辑:程序博客网 时间:2024/04/29 13:18

Robotic Sort

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


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
63 4 5 1 6 243 3 2 10
 

Sample Output
4 6 4 5 6 64 2 4 4
 

Source
2008 “Shun Yu Cup” Zhejiang Collegiate Programming Contest - Warm Up(2)
 

 序列区间翻转操作,用Splay可以很方便的实现。这里翻转还是保持原来的相对顺序,所以记录最小值的时候要比较值和序号。

#include <iostream>#include <cstring>#include <string>#include <cstdio>#include <algorithm>#include <vector>#include <set>#include <map>#include <stack>#include <queue>using namespace std;#define inf 0x3f3f3f3f#define key_rt ch[ch[root][1]][0]#define maxn 100010#define lc ch[x][0]#define rc ch[x][1]struct Splay{    int ch[maxn][2],par[maxn],size[maxn],key[maxn],mini[maxn], order[maxn];    bool lazy[maxn];    int root, tot;    int a[maxn];    int cmp(int a, int b)    {        if(key[a]!=key[b])            return key[a]<key[b]? a:b;        else return order[a]<order[b]?a:b;    }    void push_up(int x)    {        size[x]=1+size[lc]+size[rc];        mini[x]=cmp(cmp(mini[lc], mini[rc]),x);    }    void update_rev(int x)    {        if(x==0) return;        lazy[x]=1-lazy[x];        swap(ch[x][0], ch[x][1]);    }    void push_down(int x)    {        if(lazy[x]){            update_rev(lc);            update_rev(rc);            lazy[x]=0;        }    }    void newnode(int &rt, int p, int k)    {        tot++;        rt=tot;        par[rt]=p;        ch[rt][0]=ch[rt][1]=0;        key[rt]=k;        mini[rt]=rt;        size[rt]=1;        lazy[rt]=0;    }    void build(int &rt, int l, int r, int p)    {        if(l>r){            rt=0;            return;        }        int m=(l+r)>>1;        newnode(rt, p, a[m]);        order[rt]=m;        build(ch[rt][0], l, m-1, rt);        build(ch[rt][1], m+1, r, rt);        push_up(rt);    }    void init(int n)    {        for(int i=1; i<=n; i++) scanf("%d", a+i);        size[0]=par[0]=ch[0][0]=ch[0][1]=0;        key[0]=inf; mini[0]=0;        tot=0;        newnode(root, 0, inf);        newnode(ch[root][1], root, inf);        build(key_rt, 1, n, ch[root][1]);        push_up(ch[root][1]);        push_up(root);    }    void rotate(int x, int k)    {        if(x==root) return;        int y=par[x];        push_down(y);        push_down(x);        ch[y][!k]=ch[x][k];        par[ch[x][k]]=y;        ch[x][k]=y;        if(par[y])            ch[par[y]][ch[par[y]][1]==y]=x;        par[x]=par[y];        par[y]=x;        if(y==root)            root=x;        push_up(y);    }    void splay(int x, int t)    {        int y,yy;        while(par[x]!=t){            y=par[x]; yy=par[y];            if(yy==t){                push_down(y);                rotate(x, ch[y][0]==x);            }            else{                push_down(yy); push_down(y);                int k= (ch[yy][0]==y);                if(ch[y][k]==x){                    rotate(x,!k);                    rotate(x, k);                }                else{                    rotate(y, k);                    rotate(x, k);                }            }        }        push_up(x);    }    int get_kth(int k)    {        int x=root;        push_down(x);        while(size[lc]!=k-1){            if(size[lc]>k-1)                x=lc;            else{                k-=size[lc]+1;                x=rc;            }            push_down(x);        }        return x;    }    void get(int a, int b)    {        splay(get_kth(a), 0);        splay(get_kth(b+2), root);    }    int get_min(int x)    {        int k=0;        int t=mini[x];        push_down(x);        while(x!=t){            if(mini[lc]==mini[x]){                x=lc;            }            else{                k+=1+size[lc];                x=rc;            }            push_down(x);        }        k+=size[lc]+1;        return k;    }    void solve(int n)    {        init(n);        for(int i=1; i<=n; i++){          //  print(root); cout<<endl;            get(i, n);            int k=get_min(key_rt);            k+=size[ch[root][0]];            a[i]=k;            get(i, k);            update_rev(key_rt);         //  print(root); cout<<endl<<endl;        }        for(int i=1; i<=n; i++){            printf("%d", a[i]);            if(i!=n)                printf(" ");            else puts("");        }    }};Splay tree;int main(){    int n;    while(scanf("%d", &n)==1 && n){        tree.solve(n);    }    return 0;}


0 0
原创粉丝点击