HDOJ 4585/2013年杭州邀请赛J题 - Shaolin Treap水~

          题目:

                    每个人有个唯一的ID与唯一的武力....人依次进入..每次和他前面与其武力最相近的交手...当有两个和他武力差相同..选择武力比他小的..现在请输出每轮情况

          题目:

                    用treap维护就好了.....找对手时..logn的复杂度找到比他武功小中小最大的..比他武功大中最小的.比较出结构...然后再丢进去..


Program:

#include<iostream>
#include<stdio.h>
#include<string.h>
#include<cmath>
#include<queue>
#include<stack>
#include<set>
#include<time.h>
#include<map>
#include<algorithm>
#define ll long long
#define eps 1e-5
#define oo 1000000007
#define pi acos(-1.0)
#define MAXN 200005
using namespace std; 
struct node
{
       int l,r,key,fix,size;
};
struct treap
{
       node h[MAXN];
       int root,num;
       void initial() { srand((int)time(0)),num=root=0; }
       void rot_l(int &x)
       {
               int R=h[x].r,L=h[x].l; 
               h[x].size=h[x].size-h[R].size+h[h[R].l].size;
               h[R].size+=h[L].size+1;
               h[x].r=h[R].l,h[R].l=x;
               x=R;       
       }
       void rot_r(int &x)
       {
               int L=h[x].l,R=h[x].r;
               h[x].size=h[x].size-h[L].size+h[h[L].r].size;
               h[L].size+=h[R].size+1;
               h[x].l=h[L].r,h[L].r=x;
               x=L; 
       }
       bool insert(int &k,int key)
       {
               if (!k)
               {
                      k=++num;
                      h[k].l=h[k].r=0,h[k].size=1;
                      h[k].key=key,h[k].fix=rand();
                      return true;
               }
               if (h[k].key==key) return false;
               if (h[k].key>key)
               {
                      if (!insert(h[k].l,key)) return false;
                      h[k].size++;
                      if (h[h[k].l].fix>h[k].fix) rot_r(k); 
                      return true;
               }else
               {
                      if (!insert(h[k].r,key)) return false;
                      h[k].size++;
                      if (h[h[k].r].fix>h[k].fix) rot_l(k); 
                      return true;
               }
       }
       int count(int key)
       { 
               int g=0,k=root;
               while (k)
               {
                      if (h[k].key>key) k=h[k].l;
                         else g+=h[h[k].l].size+1,k=h[k].r;
               }
               return g;
       }
       int k_th(int kth)
       { 
               int g=0,k=root;
               if (h[root].size<kth) return -1; 
               while (h[h[k].l].size+g+1!=kth)
               {
                       if (h[h[k].l].size+g+1>=kth) k=h[k].l;
                         else g+=h[h[k].l].size+1,k=h[k].r;
               }     
               return h[k].key;          
       }
       bool del(int &k,int key)
       { 
               if (!k) return false;
               if (h[k].key>key) 
               {
                       if (!del(h[k].l,key)) return false;
                       h[k].size--; 
               }
               else 
               if (h[k].key<key) 
               {
                       if (!del(h[k].r,key)) return false;
                       h[k].size--;  
               }
               else
               {
                       if (!h[k].l && !h[k].r) k=0;
                          else
                       if (!h[k].l) k=h[k].r; 
                          else
                       if (!h[k].r) k=h[k].l;
                       else
                       {
                                if (h[h[k].l].fix<h[h[k].r].fix)
                                {
                                        rot_l(k);
                                        if (!del(h[k].l,key)) return false;
                                        h[k].size--;
                                }  
			                    else
		                        {
			                            rot_r(k);
                                        if (!del(h[k].r,key)) return false;
                                        h[k].size--;
		                        }
                       } 
               }
               return true;
       }       
}mytreap;
int id[5000005];
int main()
{              
       int n,x,p;   
       while (~scanf("%d",&n) && n)
       {
              mytreap.initial(); 
              int t,x;
              scanf("%d%d",&t,&x);
              mytreap.insert(mytreap.root,x);
              id[x]=t;
              printf("%d %d\n",t,1);
              for (p=2;p<=n;p++)
              {
                      int t,x,k,h,hh;
                      scanf("%d%d",&t,&x);
                      id[x]=t;  
                      k=mytreap.count(x);
                      if (!k) h=mytreap.k_th(1);
                      else
                      if (k==p-1) h=mytreap.k_th(p-1);
                      else
                      {
                             h=mytreap.k_th(k);
                             hh=mytreap.k_th(k+1);
                             if (x-h>hh-x) h=hh;                  
                      }  
                      printf("%d %d\n",t,id[h]);      
                      mytreap.insert(mytreap.root,x);              
              } 
       }
       return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值