hdu4585(Treap)

Shaolin

Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)
Total Submission(s): 2349    Accepted Submission(s): 999


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
  
  
3 2 1 3 3 4 2 0
 

Sample Output
  
  
2 1 3 2 4 2
 
这个题学长要求用treap,,那就是模板了,找了个模板弄了弄然后就a了,,继续去写splay的方式了,没啥需要注意的。
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <time.h>
#include <stdlib.h>
#include <math.h>
using namespace std;
int vis[5000000+5];
struct Node
{
    int size;
    int rank;
    int key;
    Node *son[2];
    bool operator < (const Node &a)const
    {
        return rank<a.rank;
    }
    int cmp(int x)const
    {
        if(x==key) return -1;
        return x<key?0:1;
    }
    void update()
    {
        size=1+son[0]->size+son[1]->size;
    }
};
struct Treap
{
    Node *root;
    Node *null=new Node();
    void init()
    {
        srand(time(NULL));
        root=null;
    }
    void rotate(Node* &o,int d)
    {
        Node *k=o->son[d^1];
        o->son[d^1]=k->son[d];
        k->son[d]=o;
        o->update();
        k->update();
        o=k;
    }
    void build(Node* &o,int x)
    {
        if(o==null)
        {
            o=new Node();
            o->son[0]=o->son[1]=null;
            o->rank=rand();
            o->key=x;
            o->size=1;
        }
        else
        {
            int d=o->cmp(x);
            build(o->son[d],x);
            o->update();
            if(o<o->son[d])
                rotate(o,d^1);
        }
    }
    int kth(Node* o,int k)
    {
        if(o==null||k<=0||k>o->size) return -1;
        int s=o->son[1]==null?0:o->son[1]->size;
        if(k==s+1) return o->key;
        else if(k<=s) return kth(o->son[1],k);
        else return kth(o->son[0],k-s-1);
    }
    int find(Node* o,int k)
    {
        if(o==null) return -1;
        int d=o->cmp(k);
        if(d==-1) return o->son[1]->size+1;
        else if(d==1) return find(o->son[d],k);
        else
        {
            int tmp=find(o->son[d],k);
            if(tmp==-1) return -1;
            else return tmp+1+o->son[1]->size;
        }
    }
} treap;
int main()
{
    int n;
    while(~scanf("%d",&n)&&n)
    {
        treap.init();
        int x,y;
        scanf("%d%d",&x,&y);
        treap.build(treap.root,y);
        vis[y]=x;
        printf("%d %d\n",x,1);
        for(int i=2;i<=n;i++)
        {
            scanf("%d%d",&x,&y);
            vis[y]=x;
            treap.build(treap.root,y);
            int t=treap.find(treap.root,y);
            int ans1,ans2,ans;
            ans1=treap.kth(treap.root,t-1);
            ans2=treap.kth(treap.root,t+1);
            if(ans1!=-1&&ans2!=-1)
            {
                ans=ans1-y>=y-ans2?ans2:ans1;
            }
            else if(ans1==-1) ans=ans2;
            else ans=ans1;
            printf("%d %d\n",x,vis[ans]);
        }
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值