码迷,mamicode.com
首页 > 其他好文 > 详细

HDU 1890 splay区间翻转

时间:2014-11-01 16:19:04      阅读:222      评论:0      收藏:0      [点我收藏+]

标签:des   style   blog   http   io   color   os   ar   java   

Robotic Sort

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


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.

bubuko.com,布布扣


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


代码:

/* ***********************************************
Author :rabbit
Created Time :2014/10/30 20:47:45
File Name :1.cpp
************************************************ */
#pragma comment(linker, "/STACK:102400000,102400000")
#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <sstream>
#include <stdlib.h>
#include <string.h>
#include <limits.h>
#include <string>
#include <time.h>
#include <math.h>
#include <queue>
#include <stack>
#include <set>
#include <map>
using namespace std;
#define INF 0x3f3f3f3f
#define eps 1e-8
#define pi acos(-1.0)
typedef long long ll;
const int maxn=100100;
struct Node;
Node *null;
struct Node{
    Node *ch[2],*fa;
    int size;
    int rev;
    Node(){
        ch[0]=ch[1]=fa=null;
        rev=0;
    }
    inline void push_up(){
        if(this==null)return;
        size=ch[0]->size+ch[1]->size+1;
    }
    inline void setc(Node *p,int d){
        ch[d]=p;
        p->fa=this;
    }
    inline bool d(){
        return fa->ch[1]==this;
    }
    void clear(){
        size=1;
        ch[0]=ch[1]=fa=null;
        rev=0;
    }
    void Update_Rev(){
        if(this==null)return;
        swap(ch[0],ch[1]);
        rev^=1;
    }
    inline void push_down(){
        if(this==null)return;
        if(rev){
            ch[0]->Update_Rev();
            ch[1]->Update_Rev();
            rev=0;
        }
    }
    inline bool isroot(){
        return fa==null||this!=fa->ch[0]&&this!=fa->ch[1];
    }
};
inline void rotate(Node *x){
    Node *f=x->fa,*ff=x->fa->fa;
    f->push_down();
    x->push_down();
    int c=x->d(),cc=f->d();
    f->setc(x->ch[!c],c);
    x->setc(f,!c);
    if(ff->ch[cc]==f)ff->setc(x,cc);
    else x->fa=ff;
    f->push_up();
}
void splay(Node* &root,Node *x,Node *goal){
    while(x->fa!=goal){
        if(x->fa->fa==goal)rotate(x);
        else{
            x->fa->fa->push_down();
            x->fa->push_down();
            x->push_down();
            bool f=x->fa->d();
            x->d()==f?rotate(x->fa):rotate(x);
            rotate(x);
        }
    }
    x->push_up();
    if(goal==null)root=x;
}
Node *get_kth(Node *r,int k){
    Node *x=r;
    x->push_down();
    while(x->ch[0]->size+1!=k){
        if(k<x->ch[0]->size+1)x=x->ch[0];
        else{
            k-=x->ch[0]->size+1;
            x=x->ch[1];
        }
        x->push_down();
    }
    return x;
}
Node *get_next(Node *p){
    p->push_down();
    p=p->ch[1];
    p->push_down();
    while(p->ch[0]!=null){
        p=p->ch[0];
        p->push_down();
    }
    return p;
}
Node pool[maxn],*tail;
Node *node[maxn],*root;
void build(Node *&x,int l,int r,Node *fa){
    if(l>r)return;
    int mid=(l+r)>>1;
    x=tail++;
    x->clear();
    x->fa=fa;
    node[mid]=x;
    build(x->ch[0],l,mid-1,x);
    build(x->ch[1],mid+1,r,x);
    x->push_up();
}
void init(int n){
    tail=pool;
    null=tail++;
    null->fa=null->ch[0]=null->ch[1]=null;
    null->size=0;null->rev=0;
    Node *p=tail++;
    p->clear();
    root=p;
    p=tail++;
    p->clear();
    root->setc(p,1);
    build(root->ch[1]->ch[0],1,n,root->ch[1]);
    root->ch[1]->push_up();
    root->push_up();
}
int a[maxn],b[maxn];
bool cmp(int i,int j){
    if(a[i]!=a[j])return a[i]<a[j];
    return i<j;
}
int main()
{
     //freopen("data.in","r",stdin);
     //freopen("data.out","w",stdout);
     int n;
     while(~scanf("%d",&n)&&n){
         for(int i=1;i<=n;i++){
             scanf("%d",&a[i]);
             b[i]=i;
         }
         init(n);
         sort(b+1,b+n+1,cmp);
         for(int i=1;i<=n;i++){
             splay(root,node[b[i]],null);
             int sz=root->ch[0]->size;
             printf("%d",root->ch[0]->size);
             if(i==n)puts("");else printf(" ");
             splay(root,get_kth(root,i),null);
             splay(root,get_kth(root,sz+2),root);
             root->ch[1]->ch[0]->Update_Rev();
         }
     }
     return 0;
}


HDU 1890 splay区间翻转

标签:des   style   blog   http   io   color   os   ar   java   

原文地址:http://blog.csdn.net/xianxingwuguan1/article/details/40681055

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!