POJ - 2182 Lost Cows【树状数组(正解) || Treap乱搞】

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/niiick/article/details/81987416

Time limit 1000 ms
Memory limit 65536 kB
N (2 <= N <= 8,000) cows have unique brands in the range 1..N. In a spectacular display of poor judgment, they visited the neighborhood ‘watering hole’ and drank a few too many beers before dinner. When it was time to line up for their evening meal, they did not line up in the required ascending numerical order of their brands.

Regrettably, FJ does not have a way to sort them. Furthermore, he’s not very good at observing problems. Instead of writing down each cow’s brand, he determined a rather silly statistic: For each cow in line, he knows the number of cows that precede that cow in line that do, in fact, have smaller brands than that cow.

Given this data, tell FJ the exact ordering of the cows.

Input

  • Line 1: A single integer, N

  • Lines 2..N: These N-1 lines describe the number of cows that precede a given cow in line and have brands smaller than that cow. Of course, no cows precede the first cow in line, so she is not listed. Line 2 of the input describes the number of preceding cows whose brands are smaller than the cow in slot #2; line 3 describes the number of preceding cows whose brands are smaller than the cow in slot #3; and so on.

Output

  • Lines 1..N: Each of the N lines of output tells the brand of a cow in line. Line #1 of the output tells the brand of the first cow in line; line 2 tells the brand of the second cow; and so on.

题目分析

方法一:树状数组+二分(正解)

用数组b[] 记录当前每个编号是否被占用
b[i]=1表示没被占用,0表示已被占用

a[i]记录第i头牛前面有多少编号比他小
也就是题目给出的条件

的首先我们可以特判第n头牛的编号为a[n]+1,并令 b [ a [ n ] + 1 ] = 0
然后从后向前遍历每头牛
第i头牛的编号就是b数组中第a[i]+1个1的位置
只要每次找到这个位置,再将这个位置的值变成0即可

对于查询位置,我们可以用树状数组+二分解决
用树状数组记录b的前缀和
二分一个位置mid检查其前缀和是否为a[n]+1即可

最后第1头牛的编号是最后剩下的一个数


然而!!!

作为一个每次写二分都死循环的蒟蒻
果断放弃了这种写法而准备Treap乱搞


方法二:Treap乱搞

思路其实和上述差不多

一开始向Treap中插入1~n
特判 a n s [ n ] = a [ n ] + 1 并从Treap中删除 a [ n ] + 1

从后向前找每头牛的答案
第i头牛的编号为Treap中第 a [ i ] + 1 小的数
找到这个数后再将其从Treap中删除

最后第1头牛的编号就是最后剩下的一个数


#include<iostream>
#include<vector>
#include<algorithm>
#include<queue>
#include<cstring>
#include<cstdio>
using namespace std;

int read()
{
    int f=1,x=0;
    char ss=getchar();
    while(ss<'0'||ss>'9'){if(ss=='-')f=-1;ss=getchar();}
    while(ss>='0'&&ss<='9'){x=x*10+ss-'0';ss=getchar();}
    return x*f;
}

const int maxn=10010;
int n;
int a[maxn],ans[maxn];
struct node
{
    node* ch[2];
    int v,r,sum;
    node(int v) :v(v) {r=rand();sum=1;ch[0]=ch[1]=NULL;}
    int cmp(int x){if(x==v)return -1;return x<v?0:1;}
    void update()
    {
        sum=1;
        if(ch[0])sum+=ch[0]->sum;
        if(ch[1])sum+=ch[1]->sum;
    }
};
node* rt=NULL;

void rotate(node* &p,int d)
{
    node* k=p->ch[d^1];
    p->ch[d^1]=k->ch[d];
    k->ch[d]=p;
    p->update(); k->update();
    p=k; 
}

void ins(node* &p,int x)
{
    if(p==NULL){p=new node(x);return;}
    int d=p->cmp(x);
    ins(p->ch[d],x);
    if(p->ch[d]->r < p->r) rotate(p,d^1);
    p->update();
}

void del(node* &p,int x)
{
    if(p==NULL) return;
    if(x==p->v)
    {
        if(p->ch[0]==NULL){node* k=p; p=p->ch[1]; delete(k);}
        else if(p->ch[1]==NULL){node* k=p; p=p->ch[0]; delete(k);}
        else
        {
            int dd=p->ch[0]->r < p->ch[1]->r ?1:0;
            rotate(p,dd); del(p->ch[dd],x);
        }
    }
    else if(x<p->v) del(p->ch[0],x);
    else del(p->ch[1],x);
    if(p!=NULL) p->update();
}

int kth(node* p,int k)
{
    int ss=p->ch[0]==NULL?0:p->ch[0]->sum;
    if(k==ss+1) return p->v;
    else if(k<=ss) return kth(p->ch[0],k);
    else return kth(p->ch[1],k-ss-1); 
}

int main()
{
    n=read(); ins(rt,1);
    for(int i=2;i<=n;++i) 
    a[i]=read(),ins(rt,i);

    ans[n]=a[n]+1; del(rt,a[n]+1);
    for(int i=n-1;i>1;--i)
    {
        int tt=kth(rt,a[i]+1);
        ans[i]=tt; del(rt,tt);
    }
    ans[1]=rt->v;
    for(int i=1;i<=n;++i)
    printf("%d\n",ans[i]);
    return 0;
}

猜你喜欢

转载自blog.csdn.net/niiick/article/details/81987416