程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> C++入門知識 >> HDOJ 1890 Robotic Sort

HDOJ 1890 Robotic Sort

編輯:C++入門知識


Splay.... 每次把第i大的數旋轉到根結點。刪除這個點然後旋轉左區間


Robotic Sort

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


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 pZ喎?http://www.Bkjia.com/kf/ware/vc/" target="_blank" class="keylink">vc2l0aW9uLCB0aGVyZWZvcmUsIHRoZSByb2JvdCBhcm0gcmV2ZXJzZXMgdGhlIGZpcnN0IDQgc2FtcGxlcy4gVGhlIHNlY29uZCBzbWFsbGVzdCBzYW1wbGUgaXMgdGhlIGxhc3Qgb25lLCBzbyB0aGUgbmV4dCByb2JvdCBvcGVyYXRpb24gd2lsbCByZXZlcnNlIHRoZSBvcmRlciBvZiBmaXZlIHNhbXBsZXMgb24KIHBvc2l0aW9ucyAyqEM2LiBUa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pPdXRwdXQKCkZvciBlYWNoIHNjZW5hcmlvLCBvdXRwdXQgb25lIGxpbmUgd2l0aCBleGFjdGx5IE4gaW50ZWdlcnMgUDEgLCBQMSAsIC4gLiAuIFBOICxzZXBhcmF0ZWQgYnkgYSBzcGFjZS48YnI+CkVhY2ggUGkgbXVzdCBiZSBhbiBpbnRlZ2VyICgxIKHcIFBpIKHcIE4gKSBnaXZpbmcgdGhlIHBvc2l0aW9uIG9mIHRoZSBpLXRoIHNhbXBsZSBqdXN0IGJlZm9yZSB0aGUgaS10aCByZXZlcnNhbCBvcGVyYXRpb24uIDxicj4KPGJyPgpOb3RlIHRoYXQgaWYgYSBzYW1wbGUgaXMgYWxyZWFkeSBvbiBpdHMgY29ycmVjdCBwb3NpdGlvbiBQaSAsIHlvdSBzaG91bGQgb3V0cHV0IHRoZSBudW1iZXIgUGkgYW55d2F5LCBpbmRpY2F0aW5nIHRoYXQgdGhlIKGwaW50ZXJ2YWwgYmV0d2VlbiBQaSBhbmQgUGkgobEgKGEgc2luZ2xlIHNhbXBsZSkgc2hvdWxkIGJlIHJldmVyc2VkLiA8YnI+CgoKIAo8YnI+CgpTYW1wbGUgSW5wdXQKCjxwcmUgY2xhc3M9"brush:java;">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 2008 “Shun Yu Cup” Zhejiang Collegiate Programming Contest - Warm Up(2)

#include 
#include 
#include 
#include 

using namespace std;

const int maxn=100100;

int root,sz[maxn],ch[maxn][2],pre[maxn],rev[maxn];
int n;

void NewNode(int& r,int fa,int k)
{
    r=k;
    pre[r]=fa;
    ch[r][0]=ch[r][1]=0;
    sz[r]=1; rev[r]=0;
}

void Push_Up(int x)
{
    sz[x]=sz[ch[x][0]]+sz[ch[x][1]]+1;
}

void Upd_Rev(int r)
{
    if(r)
    {
        swap(ch[r][0],ch[r][1]);
        rev[r]^=1;
    }
}

void Push_Down(int r)
{
    if(rev[r])
    {
        Upd_Rev(ch[r][0]);
        Upd_Rev(ch[r][1]);
        rev[r]=0;
    }
}

void Build(int& x,int l,int r,int fa)
{
    if(l>r) return;
    int mid=(l+r)/2;
    NewNode(x,fa,mid);
    Build(ch[x][0],l,mid-1,x);
    Build(ch[x][1],mid+1,r,x);
    Push_Up(x);
}

void Rotate(int x,int kind)
{
    int y=pre[x];

    Push_Down(y);
    Push_Down(x);

    ch[y][!kind]=ch[x][kind];
    pre[ch[x][kind]]=y;

    if(pre[y])
        ch[pre[y]][ch[pre[y]][1]==y]=x;
    pre[x]=pre[y];
    pre[y]=x;
    ch[x][kind]=y;

    Push_Up(y);
}

void Splay(int r,int goal)
{
    while(pre[r]!=goal)
    {
        if(pre[pre[r]]==goal)
        {
            Push_Down(pre[r]);
            Push_Down(r);

            Rotate(r,ch[pre[r]][0]==r);
        }
        else
        {
            Push_Down(pre[pre[r]]);
            Push_Down(pre[r]);
            Push_Down(r);

            int y=pre[r];
            int kind=(ch[pre[y]][0]==y);

            if(ch[y][kind]==r) Rotate(r,!kind);
            else Rotate(y,kind);
            Rotate(r,kind);
        }
    }
    Push_Up(r);
    if(goal==0) root=r;
}

void init()
{
    root=0;
    ch[root][1]=ch[root][0]=pre[root]=rev[root]=sz[root]=0;
    Build(root,1,n,0);
}

int Get_Max(int r)
{
    Push_Down(r);
    while(ch[r][1])
    {
        r=ch[r][1];
        Push_Down(r);
    }
    return r;
}

void Remove()
{
    Push_Down(root);
    if(ch[root][0]==0)
    {
        root=ch[root][1];
        pre[root]=0;
    }
    else
    {
        int m=Get_Max(ch[root][0]);
        Splay(m,root);
        ch[m][1]=ch[root][1];
        pre[m]=0; pre[ch[root][1]]=m;
        root=m;
        Push_Up(root);
    }
}

/************Debug***************/
void showit(int x)
{
    if(x)
    {
        Push_Down(x);
        showit(ch[x][0]);
        printf("結點: %2d 左兒子: %2d 右兒子: %2d 父結點: %2d size: %2d rev: %2d \n",
               x,ch[x][0],ch[x][1],pre[x],sz[x],rev[x]);
        showit(ch[x][1]);
    }
}

void Debug()
{
    printf("-------------------------------\n");
    printf("root: %d\n",root);
    showit(root);
}

/**************BLOCK*****************/
struct BLOCK
{
    int id,num;
}block[maxn];

bool cmp(BLOCK a,BLOCK b)
{
    if(a.num!=b.num) return a.num



  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved