Shaolin (Treap树)

Shaolin

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


 

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
 

Source
/*
*@Author:   GuoJinlong
*@Language: C++
*/
//#include <bits/stdc++.h>
#include<iostream>
#include<cstdio>
#include<string>
#include<queue>
#include<stack>
#include<map>
#include<vector>
#include<list>
#include<set>
#include<iomanip>
#include<cstring>
#include<cctype>
#include<cmath>
#include<cstdlib>
#include<ctime>
#include<cassert>
#include<sstream>
#include<algorithm>
using namespace std;
const int mod=1e9+7;
typedef long long  ll;
#define ls (p<<1)
#define rs (p<<1|1)
#define mid (l+r)/2
#define over(i,s,t) for(register long long i=s;i<=t;++i)
#define lver(i,t,s) for(register long long i=t;i>=s;--i)
const int MAXN = 305;
const int INF = 0x3f3f3f3f;
const int N=5e4+7;
const int maxn=1e5+5;
const double EPS=1e-10;
const double Pi=3.1415926535897;
//inline double max(double a,double b){
//    return a>b?a:b;
//}
//inline double min(double a,double b){
//    return a<b?a:b;
//}
 
int xd[8] = {0, 1, 0, -1, 1, 1, -1, -1};
int yd[8] = {1, 0, -1, 0, -1, 1, -1, 1};
 
//void Fire(){
//    queue<node> p;
//    p.push({fx,fy,0});
//    memset(fire, -1, sizeof(fire));
//    fire[fx][fy]=0;
//    while(!p.empty()){
//        node temp=p.front();
//        p.pop();
//        for(int i=0;i<8;i++){
//            int x=temp.x+xd[i];
//            int y=temp.y+yd[i];
//            if(x<0||x>=n||y<0||y>=m||fire[x][y]!=-1){
//                continue;
//            }
//            fire[x][y]=temp.val+1;
//            p.push({x,y,temp.val+1});
//        }
//    }
//}
//int bfs(){
//    queue<node> p;
//    memset(vis, 0, sizeof(vis));
//    p.push({sx,sy,0});
//    while (!p.empty()) {
//        node temp=p.front();
//        vis[temp.x][temp.y]=1;
//        p.pop();
//        for(int i=0;i<4;i++){
//            int x=temp.x+xd[i];
//            int y=temp.y+yd[i];
//            if(x<0||x>=n||y<0||y>=m)  continue;
//            if(x==ex&&y==ey&&temp.val+1<=fire[x][y]) return temp.val+1;
//            if(vis[x][y]||temp.val+1>=fire[x][y]||a[x][y]=='#') continue;
//            p.push({x,y,temp.val+1});
//        }
//    }
//    return -1;
//}

//一维哈希
//int n;
//string s;
//int bas=131;
//typedef unsigned long long ull;
//const ull mod1=100001651;
//ull a[100010];
//ull Hash(string s){
//    ll ans=0;
//    for(int i=0;i<s.size();i++){
//        ans*=bas;
//        ans+=int(s[i]);
//        ans%=mod1;
//    }
//    return ans;
//}

//二维哈希
//using lom=unsigned long long ;
//const lom bas1=131,bas2=233;
//const int M=505;
//int n,m;
//char a[M][M];
//lom _hash[M][M];
//lom p1[M],p2[M];
//
//
//void init(){
//    p1[0]=1;
//    p2[0]=1;
//    for(int i=1;i<=505;i++){
//        p1[i]=p1[i-1]*bas1;
//        p2[i]=p2[i-1]*bas2;
//
//    }
//}
//void Hash(){
//    _hash[0][0]=_hash[0][1]=_hash[1][0]=0;
//    for(int i=1;i<=n;i++){    //前缀和
//        for(int j=1;j<=m;j++){
//            _hash[i][j]=_hash[i][j-1]*bas1+a[i][j]-'a';
//        }
//    }
//    for(int i=1;i<=n;i++){   //二维前缀和
//        for(int j=1;j<=m;j++){
//            _hash[i][j]+=_hash[i-1][j]*bas2;
//        }
//    }
//
//}


//int pre[1010];
//int in[1010];
//int post[1010];
//int k;
//struct node{
//    int value;
//    node *l,*r;
//    node (int value=0,node *l=NULL,node *r=NULL):value(value),l(l),r(r){}
//};
//void builttree(int l,int r,int &t,node * &root){
//    int flag=-1;
//    for(int i=l;i<=r;i++){
//        if(in[i]==pre[t]){
//            flag=i;
//            break;
//        }
//    }
//    if(flag==-1) return;
//    root=new node(in[flag]);
//    t++;
//    if(flag>l) builttree(l,flag-1,t,root->l);
//    if(flag<r) builttree(flag+1,r,t,root->r);
//
//}
//void preorder(node *root){
//    if(root!=NULL)
//    {
//        post[k++]=root->value;
//        preorder(root->l);
//        preorder(root->r);
//
//    }
//}
//void inorder(node *root){
//    if(root!=NULL)
//    {
//        inorder(root->l);
//        post[k++]=root->value;
//        inorder(root->r);
//
//    }
//}
//void postorder(node *root){
//    if(root!=NULL)
//    {
//        postorder(root->l);
//        postorder(root->r);
//        post[k++]=root->value;
//    }
//}

int id[60000010];
struct node{
    int rank;  //优先级
    int size; //该结点为根的子树上的结点
    int key;  
    node * son[2]; //0是左儿子 1是右儿子
    bool operator <(const node &a) const{ return rank<a.rank;} //按照优先级排序
    int cmp(int x)const{    //选择左右子树
        if(key==x) return -1;
        return x<key?0:1;
    }
    void update(){ //维护size
        int size=1;
        if(son[0]!=NULL) size+=son[0]->size;
        if(son[1]!=NULL) size+=son[1]->size;
    }
};
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 insert(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);
        insert(o->son[d],x);
        o->update();
        if(o<o->son[d])
            rotate(o,d^1);
    }
}
int kth(node * o,int k){ //查找第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-1-s);
}
int find(node *o,int k){ //查找k的优先级名次
    if(o==NULL) return -1;
    int d=o->cmp(k);
    if(d==-1){
        return o->son[1]==NULL?1:o->son[1]->size+1;
    }
    else if(d==1){
        return find(o->son[1],k);
    }
    else{
        int tmp=find(o->son[d],k);
        if(tmp==-1) return -1;
        else return o->son[1]==NULL?tmp+1:tmp+1+o->son[1]->size;
    }
}
int main(){
    int n;
    while (cin>>n&&n) {
        srand(time(NULL));
        int k,g;
        cin>>k>>g;
        node *root=new node();
        root->son[0]=root->son[1]=NULL;
        root->rank=rand();
        root->key=g;
        root->size=1;
        id[g]=k;
        cout<<k<<" "<<1<<endl;
        for(int i=2;i<=n;i++){
            cin>>k>>g;
            id[g]=k;
            insert(root,g);
            int t=find(root,g);
            int ans,ans1,ans2;
            ans1=kth(root,t-1);
            ans2=kth(root,t+1);
            if(ans1!=-1&&ans2!=-1)
                ans=ans1-g>=g-ans2?ans2:ans1;
            else if(ans1==-1){
                ans=ans2;
            }
            else ans=ans1;
            cout<<k<<" "<<id[ans]<<endl;
        }
        
        
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
这是Treap的代码: ``` #include <bits/stdc++.h> using namespace std; struct Node { int key, priority; Node *left, *right; Node(int key, int priority) : key(key), priority(priority), left(NULL), right(NULL) {} }; struct Treap { Node *root; Treap() : root(NULL) {} void rotateRight(Node *&p) { Node *q = p->left; p->left = q->right; q->right = p; p = q; } void rotateLeft(Node *&p) { Node *q = p->right; p->right = q->left; q->left = p; p = q; } void insert(Node *&p, int key, int priority) { if (p == NULL) { p = new Node(key, priority); return; } if (key < p->key) { insert(p->left, key, priority); if (p->priority < p->left->priority) { rotateRight(p); } } else { insert(p->right, key, priority); if (p->priority < p->right->priority) { rotateLeft(p); } } } void remove(Node *&p, int key) { if (p == NULL) { return; } if (key == p->key) { if (p->left == NULL || p->right == NULL) { Node *q = p; if (p->left == NULL) { p = p->right; } else { p = p->left; } delete q; } else { if (p->left->priority > p->right->priority) { rotateRight(p); remove(p->right, key); } else { rotateLeft(p); remove(p->left, key); } } } else if (key < p->key) { remove(p->left, key); } else { remove(p->right, key); } } bool search(Node *p, int key) { if (p == NULL) { return false; } if (key == p->key) { return true; } if (key < p->key) { return search(p->left, key); } else { return search(p->right, key); } } }; int main() { Treap t; t.insert(t.root, 5, rand()); t.insert(t.root, 3, rand()); t.insert(t.root, 8, rand()); t.insert(t.root, 1, rand()); t.insert(t.root, 4, rand()); t.insert(t.root, 6, rand()); t.insert(t.root, 9, rand());

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

郭晋龙

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值