poj 2182 Lost Cows (树状数组 || 线段树)

Lost Cows

Time Limit: 1000MSMemory Limit: 65536K
Total Submissions: 18745Accepted: 11547

Description

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.

Sample Input

5
1
2
1
0

Sample Output

2
4
5
3
1

题目简单理解:有编号1~n的n个数字,2<=n<=8000,乱序排列,顺序是未知的。对于每个位置的数字,指导排在它前面比它小的数字有多少个。求这个乱序数列的顺序。

树状数组:

#include<iostream>
#include<stdio.h>
using namespace std;
#define lowbit(x) ((-x)&(x))
const int inf = 10000;
int n , m ;
int tree[inf] , pre[inf] , ans[inf];
void add(int i , int k)//单点修改
{
    for(;i<inf;i+=lowbit(i))
        tree[i]+=k;
}

int sum(int i)//求前缀和
{
    int ans = 0;
    for(;i>0;i-=lowbit(i))
        ans+=tree[i];
    return ans;
}

int findpos(int x)
{
    int l=1,r=n;
    while(l<r) //二分法查找
    {
        int mid = (l+r)>>1;//除以2
        if(sum(mid)<x)
            l=mid+1;
        else
            r=mid;
    }
    return r;
}

int main()
{
    ios::sync_with_stdio(false);
    cin>>n;
    pre[1]=0;
    for(int i=2;i<=n;i++)
        cin>>pre[i];
    for(int i=1;i<=n;i++)
        tree[i]=lowbit(i); //等同于add(i,1);
    for(int i=n;i>0;i--) //从后往前计算
    {
        int x = findpos(pre[i]+1); //第pre[i]+1个数字就是ans[i]
        add(x,-1); //标记
        ans[i]=x;
    }
    for(int i=1;i<=n;i++)
//        printf("%d\n",ans[i]);
        cout<<ans[i]<<endl;
    return 0;
}

结构体线段树:代码来自于《算法竞赛从入门到进阶》 简单易理解

#include<iostream>
using namespace std;
#define lowbit(x) ((-x)&(x))
typedef long long ll;
const int inf = 10010;
int n , m ;
struct {
    int l;
    int r;
    int len;
}tree[4*inf];
int pre[inf] , ans[inf];
void buildtree(int l , int r , int u) //类似于二叉树中序遍历
{
    tree[u].l = l;
    tree[u].r = r;
    tree[u].len = r-l+1;
    if(l==r)//到了最下面的子节点时l==r
        return ;
    int mid = l+r>>1;
    buildtree( l , mid , u<<1 ); //递归左子树
    buildtree( mid+1 , r , (u<<1)+1 ); //递归右子树
}
int query(int u , int num)
{
    tree[u].len--;
    if(tree[u].l == tree[u].r)
        return tree[u].l;
    //情况1:左子区间内牛的个数不够,则查询右子区间中坐骑第num-tree[u<<1].len个元素
    if(tree[u<<1].len < num)
        return query((u<<1)+1,num-tree[u<<1].len);
    //情况2:左子区间内牛的个数足够,依旧查询左子区间中左起第num个元素
    if(tree[u<<1].len >= num)
        return query(u<<1,num);
}

int main()
{
    ios::sync_with_stdio(false);
    cin>>n;
    pre[1]=0;
    for(int i=2;i<=n;i++) //从后往前遍历
        cin>>pre[i];
    buildtree(1,n,1);
    for(int i=n;i>=1;i--)
        ans[i]=query(1,pre[i]+1);//
    for(int i=1;i<=n;i++)
        cout<<ans[i]<<endl;
    return 0;
}

完全二叉树实现线段树:代码来自于《算法竞赛从入门到进阶》 简单易理解,写法较结构体法更简单

#include<iostream>
#include<math.h>
using namespace std;
#define lowbit(x) ((-x)&(x))
typedef long long ll;
const int inf = 10010;
int n , m ;
int pre[inf] , tree[4*inf] , ans[inf];
//tree[] 用来实现完全二叉树 需要四倍空间
void buildtree(int last_left) //用完全二叉树建一个线段树
{
    for( int i = last_left ; i < last_left + n ;i ++)
        //给二叉树的最后一行赋值,左边n个结点是n头牛
        tree[i] = 1 ;
    while(last_left != 1 )//从二叉树的最后一行倒推到根节点,根结点的值是牛的总数
    {
        for(int i = last_left / 2;i < last_left ;i ++)//树的每一层都计算左右孩子的和 //由下往上计算
            tree[i] = tree[i*2] + tree[i*2+1];
        last_left = last_left / 2;
    }
}
int query(int u , int num , int last_left)
{
    //查询+ 维护,关键的一点是所求值为当前区间中左起第num个元素
    tree[u] --;//对访问到的区间维护剩下的牛的个数
    if(tree[u] == 0 && u >= last_left)
        return u;
    //情况1:左子区间的数字个数不够,则查询右子区间中左起第num-tree[u<<1]个元素
    if(tree[u<<1] < num)
        return query( (u<<1) + 1 , num - tree[u<<1], last_left);
    //情况2:左子区间的数字个数足够,则查询左子区间中左起第num个元素
    if(tree[u<<1] >= num )
        return query(u<<1 , num , last_left);
}

int main()
{
    ios::sync_with_stdio(false);
    cin>>n;
    pre[1]=0;
    int last_left = 1 << ( int(log(n) / log(2)) + 1) ;
    //二叉树最后一行的最左边一个 计算方法是找离n最近的2的指数,例如3->4 , 4->4 , 5->8
    for(int i=2;i<=n;i++) //从后往前遍历
        cin>>pre[i];
    buildtree(last_left);
    for(int i=n;i>=1;i--)
        ans[i]=query(1 , pre[i]+1 , last_left) - last_left + 1;//
    for(int i=1;i<=n;i++)
        cout<<ans[i]<<endl;
    return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值