[线段树][Splay][树状数组]JZOJ 3292 发牌

Description

在一些扑克游戏里,如德州扑克,发牌是有讲究的。一般称呼专业的发牌手为荷官。荷官在发牌前,先要销牌(burn card)。所谓销牌,就是把当前在牌库顶的那一张牌移动到牌库底,它用来防止玩家猜牌而影响游戏。

假设一开始,荷官拿出了一副新牌,这副牌有N张不同的牌,编号依次为1到N。由于是新牌,所以牌是按照顺序排好的,从牌库顶开始,依次为1, 2,……直到N,N号牌在牌库底。为了发完所有的牌,荷官会进行N次发牌操作,在第i次发牌之前,他会连续进行Ri次销牌操作,Ri由输入给定。请问最后玩家拿到这副牌的顺序是什么样的?

举个例子,假设N = 4,则一开始的时候,牌库中牌的构成顺序为{1, 2, 3, 4}。

假设R1=2,则荷官应该连销两次牌,将1和2放入牌库底,再将3发给玩家。目前牌库中的牌顺序为{4, 1, 2}。

假设R2=0,荷官不需要销牌,直接将4发给玩家,目前牌库中的牌顺序为{1,2}。

假设R3=3,则荷官依次销去了1, 2, 1,再将2发给了玩家。目前牌库仅剩下一张牌1。

假设R4=2,荷官在重复销去两次1之后,还是将1发给了玩家,这是因为1是牌库中唯一的一张牌。
 

Input

第 1行,一个整数 N,表示牌的数量。

第 2行到第 N + 1行,在第 i + 1行,有一个整数Ri, 0≤Ri<N

Output

第 1行到第N行:第 i行只有一个整数,表示玩家收到的第 i张牌的编号 。
 

Sample Input

4
2
0
3
2

Sample Output

3
4
2
1
 

Data Constraint

分析

这题就是要求,每次查找并弹出第k大元素

$k=\sum r_i$%剩余元素数量 +1

然后这种操作不难想到平衡树权值线段树

删点乱搞一下就行了

顺便说比赛的时候写了平衡树,结果没看出来可以%转变,直接用平衡树模拟了放牌……更不可思议的是这么久没写了居然写出来的东西能出正确答案

这题出题人很阴险啊,Splay加了快读和氧气才90,然后脑子一抽想用树状数组……更慢

 

线段树 100pts

#include <iostream>
#include <cstdio>
using namespace std;
const int N=7e5+10;
struct Seg {
    int c[2],sz;
}t[4*N];
int rt=1,n,r;

void Update(int x) {
    t[x].sz=t[t[x].c[0]].sz+t[t[x].c[1]].sz;
}

void Build(int x,int l,int r) {
    if (l==r) {
        t[x].sz=1;
        return;
    }
    int mid=l+r>>1;
    Build(t[x].c[0]=x<<1,l,mid);
    Build(t[x].c[1]=(x<<1)+1,mid+1,r);
    Update(x);
}

void Find_Del(int x,int l,int r,int k,int from) {
    if (l==r) {
        printf("%d\n",l);
        t[x>>1].c[from]=0;
        return;
    }
    int mid=l+r>>1;
    if (t[x].c[0]&&t[t[x].c[0]].sz>=k) Find_Del(t[x].c[0],l,mid,k,0);
    else if (t[x].c[1]&&t[t[x].c[1]].sz>=k-t[t[x].c[0]].sz) Find_Del(t[x].c[1],mid+1,r,k-t[t[x].c[0]].sz,1);
    Update(x);
}

int main() {
    scanf("%d",&n);
    Build(rt,1,n);
    for (int i=1;i<=n;i++) {
        int R;
        scanf("%d",&R);r=(R+r)%(n-i+1);
        Find_Del(rt,1,n,r+1,666);
    }
}
View Code

Splay O(2) 90pts

#pragma GCC optimize(2)
#include <iostream>
#include <cstdio>
using namespace std;
const int N=7e5+10;
struct Node {
    int c[2],f,id,sz;
}t[N];
int rt,cnt;
int n,r;
bool a[N];

int Read(){
    int x=0,f=1;
    char ch=getchar();
    while(ch<'0'||ch>'9'){
        if(ch=='-')
            f=-1;
        ch=getchar();
    }
    while(ch>='0'&&ch<='9'){
        x=(x<<1)+(x<<3)+(ch^48);
        ch=getchar();
    }
    return x*f;
}

bool Witch(int x) {return t[t[x].f].c[1]==x;}

void Update(int x) {
    t[x].sz=1+t[t[x].c[0]].sz+t[t[x].c[1]].sz;
}

void Rotate(int x) {
    int f=t[x].f,gf=t[f].f,lr=Witch(x);
    t[x].f=gf;if (gf) t[gf].c[Witch(f)]=x;
    t[t[f].c[lr]=t[x].c[lr^1]].f=f;
    t[t[x].c[lr^1]=f].f=x;
    Update(f);Update(x);
}

void Splay(int x,int goal) {
    for (;t[x].f!=goal;Rotate(x))
        if (t[t[x].f].f!=goal) Rotate(Witch(t[x].f)==Witch(x)?t[x].f:x);
    if (!goal) rt=x;
}

void Build(int &x,int l,int r) {
    int mid=l+r>>1;
    if (a[mid]) return;
    if (!x) x=++cnt;
    t[x].id=mid;t[x].sz=1;a[mid]=1;
    Build(t[x].c[0],l,mid);
    Build(t[x].c[1],mid+1,r);
    t[x].sz+=t[t[x].c[0]].sz+t[t[x].c[1]].sz;
    if (t[x].c[0]) t[t[x].c[0]].f=x;
    if (t[x].c[1]) t[t[x].c[1]].f=x;
}

int Get_Kth(int x,int k) {
    if (t[t[x].c[0]].sz+1>k) return Get_Kth(t[x].c[0],k);
    else {
        if (t[t[x].c[0]].sz+1==k) return x;
        return Get_Kth(t[x].c[1],k-t[t[x].c[0]].sz-1);
    }
}

void Del(int x) {
    Splay(x,0);
    int oldrt=rt;
    printf("%d\n",t[x].id);
    if (!t[x].c[0]&&!t[x].c[1]) {
        rt=0;return;
    }
    if (!t[x].c[0]) {
        rt=t[x].c[1];t[rt].f=0;t[oldrt].c[1]=0;
        return;
    }
    if (!t[x].c[1]) {
        rt=t[x].c[0];t[rt].f=0;t[oldrt].c[0]=0;
        return;
    }
    int p=t[x].c[0];
    while (t[p].c[1]) p=t[p].c[1];
    Splay(p,0);
    rt=p;t[t[rt].c[1]=t[oldrt].c[1]].f=rt;
    t[oldrt].f=t[oldrt].c[1]=0;
    Update(rt);
}

int main() {
    n=Read();
    Build(rt,1,n);
    for (int i=1;i<=n;i++) {
        int R;
        R=Read();(r+=R)%=cnt;
        Del(Get_Kth(rt,r+1));cnt--;
    }
    cnt++;
}
View Code

树状数组 O(2) 约60pts

#include <iostream>
#include <cstdio>
using namespace std;
#define lowbit(x) x&-x
const int N=7e5+10;
int t[N];
int n,R;

void Add(int x) {
    for (;x<=n;x+=lowbit(x)) t[x]++;
}

void Dec(int x) {
    for (;x<=n;x+=lowbit(x)) t[x]--;
}

int Sum(int x) {
    int ans=0;
    for (;x;x-=lowbit(x)) ans+=t[x];
    return ans;
}

int main() {
    scanf("%d",&n);
    for (int i=1;i<=n;i++) Add(i);
    for (int i=1;i<=n;i++) {
        int r;
        scanf("%d",&r);R=(R+r)%(n-i+1);
        int j=R+1;
        while (Sum(j)!=R+1) j+=R+1-Sum(j);
        printf("%d\n",j);
        Dec(j);
    }
}
View Code

 

posted @ 2019-07-02 21:19  Vagari  阅读(170)  评论(0编辑  收藏  举报