hdu4585 shaolin【 Treap模板题】

来源:互联网 发布:形容闪亮的网络词语 编辑:程序博客网 时间:2024/05/17 18:17

Problem Description
Shaolin temple is very famous for its Kongfu monks.A lot of young men go to Shaolin temple every year, trying to be a monk there. The master of Shaolin evaluates a young man mainly by his talent on understanding the Buddism scripture, but fighting skill is also taken into account.
When a young man passes all the tests and is declared a new monk of Shaolin, there will be a fight , as a part of the welcome party. Every monk has an unique id and a unique fighting grade, which are all integers. The new monk must fight with a old monk whose fighting grade is closest to his fighting grade. If there are two old monks satisfying that condition, the new monk will take the one whose fighting grade is less than his.
The master is the first monk in Shaolin, his id is 1,and his fighting grade is 1,000,000,000.He just lost the fighting records. But he still remembers who joined Shaolin earlier, who joined later. Please recover the fighting records for him.
 

Input
There are several test cases.
In each test case:
The first line is a integer n (0 <n <=100,000),meaning the number of monks who joined Shaolin after the master did.(The master is not included).Then n lines follow. Each line has two integer k and g, meaning a monk's id and his fighting grade.( 0<= k ,g<=5,000,000)
The monks are listed by ascending order of jointing time.In other words, monks who joined Shaolin earlier come first.
The input ends with n = 0.
 

Output
A fight can be described as two ids of the monks who make that fight. For each test case, output all fights by the ascending order of happening time. Each fight in a line. For each fight, print the new monk's id first ,then the old monk's id.
 

Sample Input
32 13 34 20
 

Sample Output
2 13 24 2
 

Source
2013ACM-ICPC杭州赛区全国邀请赛

一个模板题卡了将近一上午,忧伤

题意:少林的和尚入学,依次入学,每人有一个独一无二的id和独一无二的水平值(由于这句,可以不用修改模板),到校之后与跟他水平相近的干仗,老方丈的id=1,水平是1,000,000,000(把这个数insert你就废了==),已知入学顺序和水平值,问每个人跟谁干仗。

研究半天treap模板,值满足二叉搜索树,优先级满足大根堆,优先级可以用系统随机数然后unique的id/grade来映射,也可以把id带进去略改模板。重点是一定不要把老方丈的值insert,下午坐下的时候突然想起来为啥把看似没问题的500001代入会有错,因为与最大值里的太近就有可能选上了啊。反正为啥要给自己没事找事呢。只有第一个人会跟老方丈干仗啊==

改模板id当做优先级的写法:

#include <iostream>#include <string.h>#include <stdlib.h>#include <stdio.h>using namespace std;struct Treap///值小的在左边。值大的在右边{    int size;    int key,fix;    Treap *ch[2];    Treap(int key,int f)    {        size=1;        fix=f;        this->key=key;        ch[0]=ch[1]=NULL;    }    int compare(int x) const    {        if(x==key) return -1;        return x<key? 0:1;    }    void Maintain()    {        size=1;        if(ch[0]!=NULL) size+=ch[0]->size;        if(ch[1]!=NULL) size+=ch[1]->size;    }};void Rotate(Treap* &t,int d){    Treap *k=t->ch[d^1];    t->ch[d^1]=k->ch[d];    k->ch[d]=t;    t->Maintain();  //必须先维护t,再维护k,因为此时t是k的子节点    k->Maintain();    t=k;}void Insert(Treap* &t,int k,int f){    if(t==NULL) t=new Treap(k,f);    else    {        //int d=t->compare(x);   //如果值相等的元素只插入一个        int d=k < t->key ? 0:1;  //如果值相等的元素都插入        Insert(t->ch[d],k,f);        if(t->ch[d]->fix > t->fix)//大根堆            Rotate(t,d^1);    }    t->Maintain();}int Kthid(Treap *t,int k,int &key)///返回的是值id 引用传递回key{    if(t==NULL||k<=0||k>t->size)        return -1;    if(t->ch[0]==NULL&&k==1)    {        key=t->key;        return t->fix;    }    if(t->ch[0]==NULL)        return Kthid(t->ch[1],k-1,key);    if(t->ch[0]->size>=k)        return Kthid(t->ch[0],k,key);    if(t->ch[0]->size+1==k)    {        key=t->key;        return t->fix;    }    return Kthid(t->ch[1],k-1-t->ch[0]->size,key);}int Rank(Treap *t,int x){    int r;    if(t->ch[0]==NULL) r=0;    else  r=t->ch[0]->size;    if(x==t->key) return r+1;    if(x<t->key)        return Rank(t->ch[0],x);    return r+1+Rank(t->ch[1],x);}void DeleteTreap(Treap* &t){    if(t==NULL) return;    if(t->ch[0]!=NULL) DeleteTreap(t->ch[0]);    if(t->ch[1]!=NULL) DeleteTreap(t->ch[1]);    delete t;    t=NULL;}void Print(Treap *t){    if(t==NULL) return;    Print(t->ch[0]);    cout<<t->key<<endl;    Print(t->ch[1]);}int main(){ //   freopen("cin.txt","r",stdin);    int n,x,k;    while(~scanf("%d",&n))    {        if(n==0) break;        int index=1;        Treap *root=NULL;     //   Insert(root,5000001,1);      //  int c=Rank(root,5000001);        int keya,keyb;//        int a=Kthid(root,c-1,keya);//        int b=Kthid(root,c+1,keyb);//        printf("a=%d b=%d c=%d keya=%d keyb=%d\n",a,b,c,keya,keyb);scanf("%d%d",&x,&k);//id grade            Insert(root,k,x);printf("%d %d\n",x,1);        for(int i=2;i<=n;i++)        {            scanf("%d%d",&x,&k);//id grade            Insert(root,k,x);            index++;            int c=Rank(root,k);            int a=Kthid(root,c-1,keya);            int b=Kthid(root,c+1,keyb);          ///  printf("a=%d b=%d c=%d keya=%d keyb=%d\n",a,b,c,keya,keyb);            if(a==-1)printf("%d %d\n",x,b);            else if(b==-1)printf("%d %d\n",x,a);            else            {                if(keyb-k>k-keya) printf("%d %d\n",x,a);                else if(keyb-k<k-keya) printf("%d %d\n",x,b);                else printf("%d %d\n",x,a);            }        }       // Print(root);        DeleteTreap(root);    }    return 0;}

正常人的做法

/***********hdu45852016.4.4***********/#include<iostream>#include<cmath>#include<cstring>#include<ctime>#include<cstdlib>#include<cstdio>#include<queue>using namespace std;struct Treap{    int size;    int key,fix;    Treap *ch[2];    Treap(int key)    {        size=1;        fix=rand();        this->key=key;        ch[0]=ch[1]=NULL;    }    int compare(int x) const    {        if(x==key) return -1;        return x<key? 0:1;    }    void Maintain()    {        size=1;        if(ch[0]!=NULL) size+=ch[0]->size;        if(ch[1]!=NULL) size+=ch[1]->size;    }};void Rotate(Treap* &t,int d){    Treap *k=t->ch[d^1];    t->ch[d^1]=k->ch[d];    k->ch[d]=t;    t->Maintain();  //必须先维护t,再维护k,因为此时t是k的子节点    k->Maintain();    t=k;}void Insert(Treap* &t,int x){    if(t==NULL)        t=new Treap(x);    else    {        int d=t->compare(x);   //如果值相等的元素只插入一个        //int d=x < t->key ? 0:1;  //如果值相等的元素都插入        Insert(t->ch[d],x);        if(t->ch[d]->fix > t->fix)            Rotate(t,d^1);    }    t->Maintain();}//一般来说,在调用删除函数之前要先用Find()函数判断该元素是否存在void Delete(Treap* &t,int x){    int d=t->compare(x);    if(d==-1)    {        Treap *tmp=t;        if(t->ch[0]==NULL)        {            t=t->ch[1];            delete tmp;            tmp=NULL;        }        else if(t->ch[1]==NULL)        {            t=t->ch[0];            delete tmp;            tmp=NULL;        }        else        {            int k=t->ch[0]->fix > t->ch[1]->fix ? 1:0;            Rotate(t,k);            Delete(t->ch[k],x);        }    }    else Delete(t->ch[d],x);    if(t!=NULL) t->Maintain();}bool Find(Treap *t,int x){    while(t!=NULL)    {        int d=t->compare(x);        if(d==-1) return true;        t=t->ch[d];    }    return false;}int Kth(Treap *t,int k)//给排名求键值key{    if(t==NULL||k<=0||k>t->size)        return -1;    if(t->ch[0]==NULL&&k==1)        return t->key;    if(t->ch[0]==NULL)        return Kth(t->ch[1],k-1);    if(t->ch[0]->size>=k)        return Kth(t->ch[0],k);    if(t->ch[0]->size+1==k)        return t->key;    return Kth(t->ch[1],k-1-t->ch[0]->size);}int Rank(Treap *t,int x)//给定key值求排名{    int r;    if(t->ch[0]==NULL) r=0;    else  r=t->ch[0]->size;    if(x==t->key) return r+1;    if(x<t->key)        return Rank(t->ch[0],x);    return r+1+Rank(t->ch[1],x);}void DeleteTreap(Treap* &t){    if(t==NULL) return;    if(t->ch[0]!=NULL) DeleteTreap(t->ch[0]);    if(t->ch[1]!=NULL) DeleteTreap(t->ch[1]);    delete t;    t=NULL;}void Print(Treap *t){    if(t==NULL) return;    Print(t->ch[0]);    cout<<t->key<<endl;    Print(t->ch[1]);}int val[5000005];int main(){    //freopen("cin.txt","r",stdin);    int n,x,k;    while(~scanf("%d",&n))    {        if(n==0) break;        memset(val,-1,sizeof(val));        Treap *root=NULL;   //     Insert(root,5000001);       // val[5000001]=1;       // int c=Rank(root,5000001);        int keya,keyb;//        int a=Kthid(root,c-1,keya);//        int b=Kthid(root,c+1,keyb);//        printf("a=%d b=%d c=%d keya=%d keyb=%d\n",a,b,c,keya,keyb);        scanf("%d%d",&x,&k);//id grade        Insert(root,k);        val[k]=x;        printf("%d %d\n",x,1);        for(int i=2;i<=n;i++)        {            scanf("%d%d",&x,&k);//id grade            Insert(root,k);            val[k]=x;            int c=Rank(root,k);            int a=Kth(root,c-1);            int b=Kth(root,c+1);          //  printf("a=%d b=%d c=%d \n",a,b,c);            if(a==-1)printf("%d %d\n",x,val[b]);            else if(b==-1)printf("%d %d\n",x,val[a]);            else            {                if(b-k>k-a) printf("%d %d\n",x,val[a]);                else if(b-k<k-a) printf("%d %d\n",x,val[b]);                else printf("%d %d\n",x,val[a]);            }        }       // Print(root);        DeleteTreap(root);    }    return 0;}


0 0
原创粉丝点击