Problem 2100 排队
Accept: 16 Submit: 160
Time Limit: 1000 mSec Memory Limit : 32768 KB
Problem Description
一些人在排队买车票,假设每个人都有不同的买票紧急程度,如果某个人的紧急程度比排在他前一个的人的紧急程度更大,则这个人可以和前一个人调换位置,并且假设每个人都有一个耐心值,若某人的耐心值为x,则他最多可以向前调换x次,当然前提是他之前x个人的紧急程度都比他小,若遇到前面一个人紧急程度比他大的他将停止往前调换。现假设有N个人,按顺序一个个进入队列,并且每进去一个人立即按照紧急程度和其耐心值向前进行调换,调换停止后下一个人才进入队列。给出这N个人的紧急程度和耐心值,问最终的排队情况。
Input
第一行输入一个整数T,表示数据组数。接下来T组数据,对于每组数据,第一行输入一个整数n (1<=n<=10^5),接下来第2到n+1行每行输入两个整数a[i],c[i] (1<=a[i]<=n,0<=c[i]<=n),分别表示第i个人的紧急程度在n个人种排第几及其耐心值,a[i]越大表示紧急程度越大。
Output
对于每组数据,请输出一行n个数,以一个空格隔开,表示最终队列的排队情况,第i个数表示最终排在第i个位置的人是第几个进入队列的。
Sample Input
152 31 44 33 15 2
Sample Output
3 1 5 4 2
Source
FOJ有奖月赛-2012年11月
这题因为把转移中的maxv打成v而Wa了一天(一天啊!)
言归正传,这题是要维护平衡树子树最大值,Treap的rotate是参考刘汝佳的写法。
就是insert时要根据左右子树的s(size)和maxv比大小
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<functional>
#include<algorithm>
using namespace std;
#define MAXN (100000+10)
int t,n;
struct node
{
int v,r,s,maxv,i;
node *ch[2];
node(int _v,int _i):s(1),r(rand()),v(_v),maxv(_v),i(_i){ch[0]=ch[1]=NULL;}
bool operator<(const node &b){return r<b.r;}
void maintain()
{
s=1;maxv=v;
if (ch[0]!=NULL) {s+=ch[0]->s;maxv=max(maxv,ch[0]->maxv);}
if (ch[1]!=NULL) {s+=ch[1]->s;maxv=max(maxv,ch[1]->maxv);}
}
}*root=NULL;
void rotate(node *&o,int d)
{
node *k=o->ch[d^1];o->ch[d^1]=k->ch[d];k->ch[d]=o;
o->maintain();k->maintain();o=k;
}
void del(node *&o)
{
if (o==NULL) return;
del(o->ch[0]);
del(o->ch[1]);
delete o;
o=NULL;
}
bool flag=1;
void print(node *&o)
{
if (o->ch[0]!=NULL) print(o->ch[0]);
if (flag) printf(" ");else flag=1;
printf("%d",o->i);
if (o->ch[1]!=NULL) print(o->ch[1]);
}
void insert(node *&o,int x,int k,int i)
{
if (o==NULL){o=new node(x,i);return; }
int d;
if (o->ch[1]==NULL)
{
if (k&&o->v<x) d=0;else d=1;
}
else if (o->ch[1]->s>=k||o->ch[1]->maxv>x||o->v>x) d=1;
else d=0;
if (d==0)
{
if (o->ch[1]!=NULL) k-=o->ch[1]->s;
k--;
}
insert(o->ch[d],x,k,i);o->maintain();
if (o->ch[d]<o) rotate(o,d^1);o->maintain();
}
int main()
{
// freopen("fzu2100.in","r",stdin);
// freopen("fzu2100.out","w",stdout);
scanf("%d",&t);
while (t--)
{
del(root);root=NULL;
scanf("%d",&n);
for (int i=1;i<=n;i++)
{
int x,y;
scanf("%d%d",&x,&y);
insert(root,x,y,i);
}
flag=0;print(root);printf("\n");
}
return 0;
}