题目:
每个人有个唯一的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;
}