HDU 1890 UVaLive 3961 - Robotic Sort (Splay)

来源:互联网 发布:java message service 编辑:程序博客网 时间:2024/04/28 05:00

Robotic Sort

Time Limit: 6000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 2478    Accepted Submission(s): 1105



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)
 

Recommend
linle   |   We have carefully selected several similar problems for you:  1882 1883 1887 1889 1888 
 

Statistic | Submit | Discuss | Note





Splay简单应用

但是WA了好久

后来对拍了好久才发现排序有问题(因为题意不清楚)




#include <cstdio>#include <iostream>#include <vector>#include <algorithm>#include <cstring>#include <string>#include <map>#include <cmath>#include <queue>#include <set>using namespace std;//#define WIN#ifdef WINtypedef __int64 LL;#define iform "%I64d"#define oform "%I64d\n"#define oform1 "%I64d"#elsetypedef long long LL;#define iform "%lld"#define oform "%lld\n"#define oform1 "%lld"#endif#define S64I(a) scanf(iform, &(a))#define P64I(a) printf(oform, (a))#define P64I1(a) printf(oform1, (a))#define REP(i, n) for(int (i)=0; (i)<n; (i)++)#define REP1(i, n) for(int (i)=1; (i)<=(n); (i)++)#define FOR(i, s, t) for(int (i)=(s); (i)<=(t); (i)++)#define keyTree (ch[ch[root][1]][0])const int INF = 0x3f3f3f3f;const double eps = 1e-9;const double PI = (4.0*atan(1.0));const int maxn = 100000 + 20;int ch[maxn][2], val[maxn], pre[maxn], size[maxn], S[maxn];int root, top1, top2;int rev[maxn];int A[maxn];struct Node {    int v, id, nid;    bool operator < (const Node b) const {        if(v != b.v) return v < b.v;        return id < b.id;    }} B[maxn];// debugvoid Treaval(int x) {    if(x) {        Treaval(ch[x][0]);        printf("结点%2d : 左儿子 %2d 右儿子 %2d 父结点 %2d size = %2d ,val = %2d\n",x,ch[x][0],ch[x][1],pre[x],size[x],val[x]);        Treaval(ch[x][1]);    }}void debug() {    printf("root=%d\n",root);    Treaval(root);}void New(int & r, int fa, int v) {    if(top2) r = S[--top2];    else r = ++top1;    pre[r] = fa;    size[r] = 1;    ch[r][0] = ch[r][1] = 0;    rev[r] = 0;}void Reversed(int r) {    if(!r) return ;    rev[r] ^= 1;    swap(ch[r][0], ch[r][1]);}void push_up(int r) {    size[r] = size[ch[r][0]] + size[ch[r][1]] + 1;}void push_down(int r) {    if(rev[r]) {        Reversed(ch[r][0]);        Reversed(ch[r][1]);        rev[r] = 0;    }}void Rotate(int x, int d) {    int y = pre[x];    push_down(x);    push_down(y);    ch[y][!d] = ch[x][d];    pre[ch[x][d]] = y;    if(pre[y]) ch[pre[y]][ch[pre[y]][1] == y] = x;    pre[x] = pre[y];    ch[x][d] = y;    pre[y] = x;    push_up(y);}void Splay(int x, int goal) {    push_down(x);    while(pre[x] != goal) {        push_down(pre[pre[x]]);        push_down(pre[x]);        push_down(x);        if(pre[pre[x]] == goal) {            Rotate(x, ch[pre[x]][0] == x);        } else {            int y = pre[x];            int d = ch[pre[y]][0] == y;            if(ch[y][d] == x) {                Rotate(x, !d);                Rotate(x, d);            } else {                Rotate(y, d);                Rotate(x, d);            }        }    }    push_up(x);    if(goal == 0) root = x;}void RotateTo(int k, int goal) {    int r = root;    push_down(r);    while(size[ch[r][0]] != k) {        if(k < size[ch[r][0]]) r = ch[r][0];        else {            k -= size[ch[r][0]] + 1;            r = ch[r][1];        }        push_down(r);    }    Splay(r, goal);}int que[maxn];void erase(int x) {    int y = pre[x];    int head = 0, tail = 0;    for(que[tail++] = x; head < tail; head++) {        int r = que[head];        S[top2++] = r;        if(ch[r][0]) que[tail++] = ch[r][0];        if(ch[r][1]) que[tail++] = ch[r][1];    }    ch[y][ch[y][1] == x] = 0;    push_up(y);}void build(int & r, int L, int R, int fa) {    if(L > R) return ;    int M = (L + R) / 2;    New(r, fa, A[M]);    B[M].nid = r;    if(L < M) build(ch[r][0], L, M-1, r);    if(M < R) build(ch[r][1], M+1, R, r);    push_up(r);}void init(int n) {    root = top1 = top2 = 0;    ch[0][0] = ch[0][1] = size[0] = pre[0] = 0;    New(root, 0, -1);    New(ch[root][1], root, -1);    size[root] = 2;    build(keyTree, 1, n, ch[root][1]);    push_up(ch[root][1]);    push_up(root);}int getPre(int r) {    int t = ch[r][0];    if(!t) return INF;    while(ch[t][1]) t = ch[t][1];    return val[t];}int getNext(int r) {    int t = ch[r][1];    if(!t) return INF;    while(ch[t][0]) t = ch[t][0];    return val[t];}int main() {    int n;    while(scanf("%d", &n) != EOF && n) {        for(int i=1; i<=n; i++) {            scanf("%d", &A[i]);        }        init(n);        for(int i=1; i<=n; i++) {            B[i].v = A[i];            B[i].id = i;        }        sort(B+1, B+n+1);        for(int i=1; i<=n; i++) {            int nid = B[i].nid;            Splay(nid, 0);            int ans = size[ch[root][0]];            RotateTo(i-1, 0);            RotateTo(ans+1, root);            Reversed(keyTree);            push_down(keyTree);            if(i > 1) putchar(' ');            printf("%d", ans);        }        putchar('\n');    }    return 0;}





0 0
原创粉丝点击